当前位置:网站首页>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
边栏推荐
- Retrofit common request methods and comments, post, get heard file upload
- My creation anniversary
- druid数据源实现后台监控
- thymeleaf中抽取公共片段
- LSP 是什么
- 动态库连接 - 符号冲突 - 全局符号介入
- 自动化测试——关于unitest与pytest初始化共存问题
- LeetCode 958. 二叉树的完全性校验
- Cloud native essay using Hana expression database service on Google kubernetes cluster
- LeetCode 498. 对角线遍历
猜你喜欢
方法区里面有什么——class文件、class文件常量池、运行时常量池
逻辑英语结构【重点】
What you need to know to test -- URL, weak network, interface, automation
定制拦截器
[trajectory planning] testing of ruckig Library
install realsense2: The following packages have unmet dependencies: libgtk-3-dev
904. 水果成篮
The first batch of 12 enterprises settled in! Opening of the first time-honored product counter in Guangzhou
Redis notes (14) - persistence and data recovery (data persistence RDB and AOF, data recovery, mixed persistence)
Develop current learning objectives and methods
随机推荐
c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
US President signs community safety act to deal with gun issue
Specific implementation comparison between different programming languages
TensorFlow遇到的各种错误
SSM项目小例子,SSM整合图文详细教程
Logical English structure [key points]
Custom interceptor
Luogu 1146 coin flip
Redis notes (14) - persistence and data recovery (data persistence RDB and AOF, data recovery, mixed persistence)
爬虫相关文章收藏:pyppeteer 、Burpsuite
In the fragment, the input method is hidden after clicking the confirm cancel button in the alertdialog (this is valid after looking for it on the Internet for a long time)
Battery historian analyzes battery consumption
LeetCode 958. Completeness checking of binary tree
自动化测试——关于unitest与pytest初始化共存问题
国际化配置
logback
Automated testing -- Introduction and use of pytest itself and third-party modules
微软 Edge 浏览器 IE 模式标签页出现卡死情况,已通过回滚更新修复
A concise tutorial for getting started with go generics
Leetcode connected to rainwater series 42 (one dimension) 407 (2D)