Leetcode:1. Two Sum
public class TwoSum1 { public static void main(String[] args) { int[] nums = new int[]{2, 7, 11, 15}; int target = 9; int a[] = new int[2]; a = twoSum(nums, target); System.out.println(a[0]+"----------"+a[1]); } public static int[] twoSum(int[] nums,int target){ int[] a = new int[2]; for (int i = 0; i < nums.length; i++) { for (int j = i+1; j < nums.length; j++) { if(nums[i]+nums[j] == target){ a[0]=i; a[1]=j; return a; } } } return a; } }
posted on 2017-12-10 17:00 Michael2397 阅读(...) 评论(...) 编辑 收藏