LeetCode1---????????????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode1---????????????相关的知识,希望对你有一定的参考价值。
?????????++ pre int rgs get map return exception ??????
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
*???????????? :????????????
* @author lkr
* @date 2019/3/5
*/
public class Solution1 {
//?????????
public static int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
int sum = nums[i] + nums[j];
if(target == sum){
arr[0] = i;
arr[1] = j;
}
}
}
return arr;
}
//???????????????
public static int [] oneHashtable(int[] nums,int target){
Map<Integer,Integer> map = new HashMap();
for (int i=0;i<nums.length;i++){
int value = target - nums[i];
if(map.containsKey(value)){
/*int[] arr = {map.get(value),i};
return arr;*/
return new int[] {map.get(value),i};//?????????map.get(value)????????????
}
map.put(nums[i],i);
}
throw new IllegalArgumentException("NO TWO SUM SOLUTION");
}
public static void main(String[] args){
int [] nums = {2,7,11,15,9};
int target = 20;
//int [] arr = twoSum(nums,target);
int [] arr = oneHashtable(nums,target);
System.out.println(Arrays.toString(arr));
}
}
以上是关于LeetCode1---????????????的主要内容,如果未能解决你的问题,请参考以下文章