-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
executable file
·42 lines (39 loc) · 1.21 KB
/
Solution.java
File metadata and controls
executable file
·42 lines (39 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package $016;
import java.util.Arrays;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 19:18 2018/4/3.
*/
public class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums == null || nums.length < 3){
return 0;
}
Arrays.sort(nums);
int closest = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++){
int left = i + 1, right = nums.length - 1;
while (left < right){
int sum = nums[i] + nums[left] + nums[right];
if (sum < target){
left++;
}else {
right--;
}
if (Math.abs(target - sum) < Math.abs(target - closest)){
closest = sum;
}
}
}
return closest;
}
public static void main(String[] args) {
int[] a = {0, 0, 0};
Solution solution = new Solution();
/// System.out.println(solution.threeSumClosest(a, 1));
String b = "你好吗";
String c = "nhm";
System.out.println(b.getBytes().length);
System.out.println(c.getBytes().length);
}
}