1. 两数之和
提供了2种揭发
public class Solution1 { public int[] twoSum(int[] nums, int target) { int len=nums.length; int[] result = new int[2]; for(int i=0;i<len;i++) { for(int j=i+1;j<len;j++) { if(nums[i]==target-nums[j]) { result[0]=i; result[1]=j; } } } return result; } public int[] twoSum1(int[] nums, int target) { int len=nums.length; int[] result = new int[2]; HashMap<Integer, Integer> map = new HashMap<>(); for(int i=0;i<len;i++) { map.put(nums[i], i); } for(int i=0;i<len;i++) { if(map.containsKey(target-nums[i])) { if(i==map.get(target-nums[i])) { continue; } result[0]=i; result[1]=map.get(target-nums[i]); break; } } return result; } public static void main(String[] args) { // TODO Auto-generated method stub int[] nums = new int[] {2, 7, 11, 15}; Solution1 s = new Solution1(); for(int i:s.twoSum1(nums,9)) { System.err.println(i); } } }