当前位置:网站首页>1. sum of two numbers (leetcode topic)
1. sum of two numbers (leetcode topic)
2022-06-26 10:09:00 【later_ rql】
1. Sum of two numbers
Title Description
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
Their thinking
Train of thought : Violent solution .
Judge the sum of every two elements in the array in turn target The size of the relationship , You can get the subscript of two equal elements , And back to .
Time complexity :O(N^2)
Spatial complexity :O(l)
Train of thought two : Hashtable .
Use the particularity of hash table to judge , lookup hash Whether the table has and target-nums[i] Equal elements , Add data every time hash In the table .
Time complexity :O(n)
Spatial complexity :O(n)
notes : For each of these x, We first query the hash table for the presence of target - x, And then x Insert into hash table , You can guarantee that you won't let x Match yourself .
Code
Solution 1 : Violent solution
public static int[] twoSum(int[] nums, int target) {
int[] arr=new int[2];
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++){
if((nums[i]+nums[j])== target){
arr[0]=i;
arr[1]=j;
}
}
}
return arr;
}
Solution 2 : Hashtable .
public static int[] twoSum_hash(int[] nums, int target){
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; ++i) {
if (hashtable.containsKey(target - nums[i])) {
return new int[]{
hashtable.get(target - nums[i]), i};
}
hashtable.put(nums[i], i);
}
return new int[0];
}
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/two-sum
边栏推荐
- Test instructions - common interface protocol analysis
- c语言语法基础之——指针(字符、一维数组) 学习
- Retrofit common request methods and comments, post, get heard file upload
- What is the web SSH service port of wgcloud
- Tensorflow dynamically allocates video memory
- JSP file syntax
- 如何更改微信小程序二维码物料颜色
- Differences between JVM, Dalvik and art
- Nested recyclerview in nestedscrollview automatically slides to the bottom after switching
- Use recursion or a while loop to get the name of the parent / child hierarchy
猜你喜欢

2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial

What you need to know to test -- URL, weak network, interface, automation

What should the preview do?

The basis of C language grammar -- learning of local variables and storage categories, global variables and storage categories, and macro definitions

全渠道、多场景、跨平台,App如何借助数据分析渠道流量

Automated testing -- Introduction and use of pytest itself and third-party modules

字符串常量池、class常量池和运行时常量池

The 100000 line transaction lock has opened your eyes.

jar版本冲突问题解决

118. 杨辉三角
随机推荐
c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
install ompl. sh
Develop current learning objectives and methods
做测试需要知道的内容——url、弱网、接口、自动化、
2021-11-12 vrep vision sensor configuration
Constraintlayout control uses full Raiders
How do technicians send notifications?
Dialog centered
SQL function
Day 3 array, pre post, character space, keyword and address pointer
Does the go compiled executable have dynamic library links?
c语言语法基础之——函数定义学习
Deep learning (tentsorflow2. version) three good student performance problems (1)
什么是僵尸网络
cmake / set 命令
String类intern()方法和字符串常量池
Win10安装tensorflow-quantum过程详解
How to find and install the dependent libraries of Debian system
What should the preview do?
druid数据源实现后台监控