当前位置:网站首页>1. Sum of two numbers
1. Sum of two numbers
2022-07-19 01:08:00 【_ ady】

Solutions 1
Violence law solves problems , Double loop to find that the sum of two numbers in the array is target The number of .
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if(nums[i]+nums[j]==target)
return new int[]{
i,j};
}
}
throw new IllegalArgumentException("no solution");
}
Solutions 2
Use hash Tabular Key-value structure , Save the numbers —> Index relationship . According to the target number, come to hash Find in table . Deposit first hash surface , And then query .
//2. Use Hash surface
public int[] twoSum1(int[] nums, int target) {
// newly build hashmap
HashMap<Integer, Integer> map = new HashMap<>();
//1. Save all data to map in
for (int i = 0; i < nums.length; i++) {
map.put(nums[i],i);
}
// Traversal array again , Find the number corresponding to each number
for (int i = 0; i < nums.length; i++) {
int targetNum = target-nums[i];
if(map.containsKey(targetNum)&& map.get(targetNum)!=i)
return new int[]{
i,map.get(targetNum)};
}
throw new IllegalArgumentException("no solution");
}
Solutions 3
Edge save to hash In the table , While searching in the table .
//3. The improved hashmap
public int[] twoSum2(int[] nums, int target) {
// newly build hashmap
HashMap<Integer, Integer> map = new HashMap<>();
//1. Save all data to map in
for (int i = 0; i < nums.length; i++) {
int targetNum = target-nums[i];
if(map.containsKey(targetNum)&& map.get(targetNum)!=i)
return new int[]{
map.get(targetNum),i};
map.put(nums[i],i);
}
throw new IllegalArgumentException("no solution");
}
边栏推荐
- Joint search set
- [MD5] quickly realize MD5 encryption and decryption (salt value)
- [gradle] quick configuration
- IDEA开发Servlet项目 如何右键创建servlet
- el-table动态添加可输入行的问题
- FactoryBean 使用场景
- MySQL常用命令
- NVIDIA's Jetson uses deepstream to accelerate common problem analysis, deepstream messages are sent and received externally, xvaier self startup program, and excellent blog summary (fine 2)
- 12. Integer to Roman numerals ●●
- Visual studio error: solution of c4996 error in strcpy
猜你喜欢

面试官:怎么不用定时任务实现关闭订单?

Prometheus+grafana visual real-time JVM monitoring tool

Solve the garbled code when inserting Chinese values in MySQL database

cli和vite通过代理实现跨域
![(manual) [sqli-labs62-65] limit injection times: blind injection](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
(manual) [sqli-labs62-65] limit injection times: blind injection

12. Integer to Roman numerals ●●

Matlab drawing heart
Go BitSet of one library per day

【着色器实现Wave效果_Shader效果第一篇】

【Gradle】快速配置
随机推荐
Julia初学者教程2022
[Shader implémente l'effet Wave Shader chapitre 1]
[natural language processing] [multimodal] multimodal overview: visual language pre training model
Go BitSet of one library per day
12. Integer to Roman numerals ●●
J9数字论:去中心化身份的主流化还有多久?
Flowable workflow (flowable database table structure)
Switch and router technology: Integrated Experiment of hot backup routing protocols HSRP, HSRP and spvstp
[set] common methods of operating ArrayList set
The difference between TCP and UDP
函数初认识-上
[Clickhouse] calculation of weeks
Problems in installing MySQL in CentOS
【集合】常见操作ArrayList集合的方法
[Linux] release jar package, dynamically view logs, view program progress, and end programs
12. 整数转罗马数字 ●●
[shader realizes wave effect _shader effect Chapter 1]
【Liunx】发布Jar包、日志动态查看、查看程序进程、结束程序
常见链表题及其 Go 实现
TCP和UDP的区别