Contents
  1. 1. two Sum
    1. 1.1. input
    2. 1.2. output

做了场indeed的机试,惨不忍睹……算法不会,连语法都快不会了……
学妹救场,半小时随随便便就把CD两题过了……
不刷题不行啊。。。
不能再堕落下去了。
算法很烂,所以每天一题,从简单的开始。看能坚持多久吧。

two Sum

找出数组[a, b, c, d, e]中和为f的2个数,返回下标(0开始)。假设只有一组解。

input

[ 2,3,5 ]
8

output

[ 1,2 ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
for(int i = 0; i < nums.length -1; i++){
for(int j = i +1 ; j < nums.length; j++){
if((target - nums[i]) == nums[j]){
res[0] = i;
res[1] = j;
return res;
}
}
}
return null;
}
}
Contents
  1. 1. two Sum
    1. 1.1. input
    2. 1.2. output