一刷:
import java.util.Arrays;public class Solution1 { public int[] twoSum(int[] nums, int target) {int[] index=new int[2];int sum=0;for (int i = 0; i < nums.length; i++) {for (int j = i+1; j < nums.length; j++) {sum=nums[i]+nums[j];index[0]= i;index[1]=j;if (sum==target) {return index;}}}return index;} public static void main(String[] args) {Solution1 s=new Solution1();int[] nums={3,2,4};int target=6;System.out.println(Arrays.toString(s.twoSum(nums, target)));} }
import java.util.Arrays;public class Solution { public int[] twoSum(int[] nums, int target) {int[] index=new int[2];int a;for (int i = 0; i < nums.length; i++) {a=target;a-=nums[i];for (int j = i+1; j < nums.length; j++) {if (a==nums[j]) {index[0]= i;index[1]=j;return index;}}}return index;} public static void main(String[] args) {Solution s=new Solution();int[] nums={2,7,11,15};int target=9;System.out.println(Arrays.toString(s.twoSum(nums, target)));} }
这有点惨呐=.=!
2019-05-17 03:54:26