当前位置:网站首页>LeetCode 1. Sum of two numbers
LeetCode 1. Sum of two numbers
2022-06-29 21:02:00 【Grilled little fat sheep with charcoal...】
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 .
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:
Input :nums = [3,2,4], target = 6
Output :[1,2]
Example 3:
Input :nums = [3,3], target = 6
Output :[0,1]
Tips :
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
There will only be one valid answer
Advanced : You can come up with a time complexity less than O(n2) The algorithm of ?
Method 1 : Brute force
public static int[] twoSum1(int[] nums, int target){
int[] rs = 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){
rs[0] = i;
rs[1] = j;
return rs;
}
}
}
return rs;
}
Method 1 : Use Map aggregate
- First declare a Map aggregate , among key Storage nums[i] Value , value Storage nums[i] The subscript i.
- Traverse nums Array , Judge target - nums[i] Whether there is map Collection :
If not map Collection , Just put <nums[i], i> Deposit in map Collection ;
If in map Collection , that map.get(target - nums[i]) and i Is a set of subscripts required for this question .
public static int[] twoSum(int[] nums, int target){
int[] rs = new int[2];
if(nums == null || nums.length == 0){
return rs;
}
// Make a statement map aggregate
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = nums[i];
if(map.containsKey(target - temp)){
rs[0] = map.get(target - temp);
rs[1] = i;
return rs;
}
map.put(temp, i);
}
return rs;
}
source : Power button (LeetCode)
link :https://leetcode.cn/problems/two-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
边栏推荐
- [advanced ROS] Lecture 3 ROS file system and distributed communication
- What problems should be avoided when using the points mall games for marketing?
- PostgreSQL每周新闻—6月22日
- 【ROS进阶篇】第三讲 ROS文件系统与分布式通信
- Simple application and configuration of Rsync
- Db查询数据库合并两个不相关的表,新增不存在的字段,并赋予默认值
- 分析影响导电滑环传输信号的因素
- Verilog realizes serial communication and sends it to the nixie tube
- CORDIC based Signal Processor desgn
- The reason why the log analysis tool of "operation and maintenance" is used more and more frequently
猜你喜欢

导航 实验【微机原理】【实验】

leetcode:307. Area and retrieval - array modifiable

Advances in computational imaging

Stm32cubemx learning (6) external interrupt experiment
![[advanced ROS] Lecture 3 ROS file system and distributed communication](/img/9f/2c316f5b0aa36b63769672048da772.png)
[advanced ROS] Lecture 3 ROS file system and distributed communication

VoIP Push 在海外音视频业务中的应用

CORDIC based Signal Processor desgn

String字符串的存储原理

VHDL电话计费系统设计

如何评价科大讯飞AI翻译笔P20系列,值得买吗?
随机推荐
MySQL JSON data types & functions
计算成像前沿进展
我的创作纪念日
Analysis of the underlying architecture of spark storage system - spark business environment practice
Exit operation in project
路由汇总带来的三层环路-解决实验
Leading by 11%, Huawei cloud sky chip AI solver once again topped the international authoritative list
Initialization of global and static variables
In depth good article | yolov5+deepsort multi-target tracking in-depth interpretation and testing (including source code)
一颗新的北极星已经升起!
[a must for reptiles - > scrapy framework from black iron to king] first chapter - detailed explanation of 10000 character blog posts (recommended Collection)
Stm32cubemx learning (6) external interrupt experiment
Implementing LDAP proxy service with haproxy + keepalive
数字密码锁verilog设计+仿真+上板验证
项目中退出操作
"Xiaodeng" in operation and maintenance monitors user login operations in real time
Analysis of the factors affecting the transmission signal of the conductive slip ring
Rsync method of establishing multi directory module
习近平在湖北武汉考察时强调 把科技的命脉牢牢掌握在自己手中 不断提升我国发展独立性自主性安全性
LSF bsub command