Actual algorithm (a) the sum of the two numbers

I. Introduction

  All along, the algorithm to learn to learn, but the real problem-solving when they use does not come up, when the project conding, and would not think of. In the final analysis, or lack the necessary training, now began to brush leetcode, from scratch, adhere to a daily problem.

II. Topic

  Title: Given an integer array nums and a target value target, and ask you to identify the target value of the two integers in the array, and return to their array subscript.

     You can assume that each input corresponds to only one answer. However, you can not re-use the same array element.

  Example: Given nums = [2, 7, 11, 15], target = 9, because nums [0] + nums [1] = 2 + 7 = 9, is returned [0, 1]

III. Problem-solving ideas

  1. Common idea: the problem is most likely to occur violent solution, two cycles, the first layer are removed in each element of the array, the second layer of elements taken sequentially after the first group of element layers, and comparing two numbers together with a target value target. code show as below:

 1 class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         for(int i = 0; i < nums.length; i++){
 4             for(int j = i + 1; j < nums.length; j++){
 5                 if(nums[i] + nums[j] == target){
 6                     return new int[] {i, j};
 7                 }
 8             }
 9         }
10         throw new IllegalArgumentException("No two sum solution");
11     }
12 }

    The solution space complexity is O (1), the time complexity is O (n ^ 2);

  2. The second idea is by solving the hash table, the first iteration of the first array all of the elements, and the elements corresponding to the stored index into the hash table, and then the second iteration, the array sequentially removed the elements of the element to calculate the difference between the target value of the target, to see whether the difference in the hash table, and if the index is not an element of this comparison (not reuse the same element) is present, which is returned and difference values ​​corresponding to the index. code show as below:

 1 class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         Map<Integer, Integer> hashMap = new HashMap();
 4         for(int i = 0; i < nums.length; i++){
 5             hashMap.put(nums[i], i);
 6         }
 7         for(int i = 0; i < nums.length; i++){
 8             int num = target - nums[i];
 9             if(hashMap.containsKey(num) && hashMap.get(num) != i){
10                 return new int[] {i, hashMap.get(num)};
11             }
12         }
13         throw new IllegalArgumentException("no two sum solution");
14     }
15 }

    The solution space complexity is O (n), the time complexity is also O (n);

  3. See above, when such a solution, some people might think that it can not reuse the same element, it is not able to be compared, then the hash table of elements into it? This is our third in solution, only one iteration. As before, to calculate the difference, then the hash table to find, if present, returns a result there is no discharge element put the hash table. code show as below:

 1 class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         Map<Integer, Integer> hashMap = new HashMap();
 4         for(int i = 0; i < nums.length; i++){
 5             int num = target - nums[i];
 6             if(hashMap.containsKey(num)){
 7                 return new int[] {i, hashMap.get(num)};
 8             }
 9             hashMap.put(nums[i], i);
10         }
11         throw new IllegalArgumentException("no two sum solution");
12     }
13 }

 

    

Guess you like

Origin www.cnblogs.com/litterCoder/p/11372519.html