Title Description:
Given an array of integers, return indices of the both numbers such that they add-to a specific target.
You may assume this each input would has exactly one solution.
Problem Solving Ideas:
This problem is relatively simple, you can copy the array, according to the size of the order, find the two number of satisfied conditions after the backward push back their position in the original array. However, in the coding process, you should be careful to distinguish between the two found the same value, otherwise it will return two identical values.
1 Public classSolution {2 Public Static int[] Twosum (int[] Nums,inttarget) {3 int[] nums1=arrays.copyof (Nums, nums.length);4 Arrays.sort (NUMS1);5 int[] result =New int[2];6 intStart = 0;7 intEnd = Nums1.length-1;8 while(start<end) {9 if(nums1[start]+nums1[end]==target) {Ten //be careful to distinguish between the two values found equal to the case One if(nums1[start]!=Nums1[end]) { A for(inti=0;i<nums.length;i++){ - if(nums[i]==Nums1[start]) { -result[0]=i; the } - if(nums[i]==Nums1[end]) { -result[1]=i; - } + } - } + if(nums1[start]==Nums1[end]) { A BooleanFound=false; at for(inti=0;i<nums.length;i++){ - if(!found) { - if(nums[i]==Nums1[start]) { -result[0]=i; -Found=true; - } in } - Else{ to if(nums[i]==Nums1[end]) { + -result[1]=i; the Break; * } $ }Panax Notoginseng - } the } + returnresult; A } the Else if(nums1[start]+nums1[end]>target) { +end--; - } $ Else{ $start++; - } - } the returnresult; - }Wuyi}
1. The Sum of the