当前位置:网站首页>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 .
边栏推荐
- VHDL电话计费系统设计
- "Xiaodeng" in operation and maintenance monitors user login operations in real time
- "Xiaodeng" active directory batch user creation in operation and maintenance
- 解释PBR纹理贴图(texture-maps)
- 量子机器学习的基础和应用:一个简明文献综述
- Goahead webserver migration
- Verilog realizes serial communication and sends it to the nixie tube
- CAD assistant - 3D model format conversion tool
- Rsync 的简单应用与配置
- STL tutorial 6-deque, stack, queue, list container
猜你喜欢

Set up your own website (12)

VHDL电话计费系统设计

In depth good article | yolov5+deepsort multi-target tracking in-depth interpretation and testing (including source code)

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

计算成像前沿进展

THREEJS基础入门

直播预告 | PostgreSQL 内核解读系列第一讲:PostgreSQL 系统概述

How to evaluate iFLYTEK AI translation pen P20 series? Is it worth buying?

leetcode:370. 区间加法

Exercise 8 Chapter 8 Verilog finite state machine design -4 Verilog quartus Modelsim
随机推荐
报表交付工程师
STM32最小系统搭建(原理图)
我的创作纪念日
输入年份与月份,求该月共有多少天
炒股开户请问哪个券商佣金是最低最安全的
[advanced ROS] Lecture 3 ROS file system and distributed communication
In depth good article | yolov5+deepsort multi-target tracking in-depth interpretation and testing (including source code)
tmux设置
Design of VHDL telephone billing system
My creation anniversary
What are the mainstream brands of smart door locks? What characteristics should we pay attention to when purchasing door locks?
Implementing LDAP proxy service with haproxy + keepalive
[advanced ROS chapter] Lecture 4: duplicate names in ROS (nodes, topics and parameters)
Recruit | DBA Data Engineer every week with an annual salary of 35+. Dream of Kyushu and bright stars!
PostgreSQL每周新聞—6月22日
CAD assistant - 3D model format conversion tool
Enter the year and month to find the total number of days in the month
Shutter bottomnavigationbar with page switching example
STL tutorial 6-deque, stack, queue, list container
C#_ Convert camera images to bitmap format and draw Crosshairs