well! Here is CH who is learning programming.
Recently, I searched the Internet for answers to questions about LeetCode, and found that there were not many answers, so I came to do one. This is the first article. I hope you like it!
New arrivals, please take care!
I heard for the first time that LeetCode was in station b. at that time, in order to find some topics to consolidate my foundation and find a website for practice, I found this website which is almost a must for programmers——
Li Kou , leetcode CN com/? utm_ source=LCUS&utm_ medium=ip_ redirect&utm_ campaign=transfer2china
Start practicing above and start from the first question.
Let's get to the point: the requirements are as follows:
The first time was a mistake, so I won't show it to you.
The second time I thought, to output a one-dimensional array, I had to set one first
int[] nums2 = new int[2];
Then, to find two numbers nums[i] and nums[j] from nums so that the sum is exactly the target, set two-layer loops, fill in the qualified I and j in nums2, and finally output nums2 Therefore, the overall code of the second is as follows:
class Solution { public int[] twoSum(int[] nums, int target) { int[] nums2 = new int[2]; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums.length; j++) { if (nums[i] + nums[j] == target) { nums2[0] = i; nums2[1] = j; } } } return nums2; } }
But what I didn't think of in the end was that the two numbers to be output were reversed!
So we changed the Third Edition:
Change i and j this time:
class Solution { public int[] twoSum(int[] nums, int target) { int[] nums2 = new int[2]; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums.length; j++) { if ((nums[i] + nums[j] == target) && (i != j)) { nums2[0] = j; nums2[1] = i; } } } return nums2; } }
It passed smoothly this time!
But on closer inspection,
7%?
Change!
Version 4: I saw the official standard answer, which used HashMap, so I went directly: (using the quick query feature of HashMap)
class Solution { public int[] twoSum(int[] nums, int target) { int[] nums2 = new int[2]; Map<Integer,Integer> map1 = new HashMap<Integer,Integer>(); for(int i = 0; i < nums.length; i++) { if(map1.containsKey(target - nums[i])){ nums2[0] = map1.get(target - nums[i]); nums2[1] = i; } map1.put(nums[i],i); } return nums2; } }
This discovery is sure enough, and the running time is much smaller. It seems to be the problem of for.
But take a closer look:
47%?
Change again!
(be strict with yourself and escape) the fifth edition has nothing to say. It's also a mistake.
Version 6: completely abandon for and use while to shorten the time again:
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> map1 = new HashMap<Integer,Integer>(); int j = 0; while (j < nums.length) { if(map1.containsKey(target - nums[j])) { return new int[]{map1.get(target - nums[j]) , j}; } map1.put(nums[j] , j++); } return new int[0]; } }
Look again:
Finally! It's not comfortable until it's on the excellent line.
The process of six submissions is attached. The last version is the best version I changed to.
Attach another one 0ms (100%) on LeetCode website:
import java.util.HashMap; class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> map = new HashMap<>(); int left = 0; int right = nums.length - 1; while (left <= right) { if (map.containsKey(target - nums[left])) { return new int[]{map.get(target - nums[left]), left}; } map.put(nums[left], left++); if (map.containsKey(target - nums[right])) { return new int[]{map.get(target - nums[right]), right}; } map.put(nums[right], right--); } return new int[0]; } }
Learning programming, private letters from small partners who want to be together ~ thank you!