-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
executable file
·124 lines (114 loc) · 3.74 KB
/
Solution.java
File metadata and controls
executable file
·124 lines (114 loc) · 3.74 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.Arrays;
import java.util.HashMap;
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
result[0] = map.get(target - nums[i]) + 1;
result[1] = i + 1;
break;
}
map.put(nums[i], i);
}
return result;
}
public int lengthOfLongestSubstring(String s) {
int result = 0;
int beginIndex = -1;
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (map.containsKey(ch)) {
beginIndex = Math.max(beginIndex, map.get(ch));
}
map.put(ch, i);
result = Math.max(result, i - beginIndex);
}
return result;
}
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
nums1 = Arrays.copyOf(nums1, nums1.length + nums2.length);
System.arraycopy(nums2, 0, nums1, nums1.length - nums2.length, nums2.length);
Arrays.sort(nums1);
return nums1.length % 2 == 0
? (nums1[(nums1.length) / 2] + nums1[(nums1.length - 1) / 2]) / 2.0 : nums1[(nums1.length) / 2];
}
public int reverse(int x) {
int result = 0;
while (x != 0) {
if (result > Integer.MAX_VALUE / 10 || result < Integer.MIN_VALUE / 10
|| result == Integer.MAX_VALUE / 10 && x % 10 > Integer.MAX_VALUE % 10
|| result == Integer.MIN_VALUE / 10 && x % 10 > Integer.MAX_VALUE % 10) {
return 0;
}
result = result * 10 + x % 10;
x /= 10;
}
return result;
}
public int myAtoi(String str) {
str = str.trim();
String result = "";
for (int i = 0; i < str.length(); i++) {
if (i == 0 && (str.charAt(0) == '+' || str.charAt(0) == '-')) {
result = result.concat(Character.toString(str.charAt(0)));
continue;
}
if (!Character.isDigit(str.charAt(i))) {
break;
}
result = result.concat(Character.toString(str.charAt(i)));
}
try {
return Integer.parseInt(result);
} catch (NumberFormatException e) {
if (result.length() > 1) {
if (result.charAt(0) == '-') {
return Integer.MIN_VALUE;
}
return Integer.MAX_VALUE;
}
return 0;
}
}
public boolean isNumber(String s) {
s = s.trim();
try {
Double.parseDouble(s);
switch (s.charAt(s.length() - 1)) {
case 'D':
case 'f':
return false;
default:
return true;
}
} catch (RuntimeException e) {
return false;
}
}
public int singleNumber(int[] nums) {
Arrays.sort(nums);
if (nums.length == 1 || nums[0] != nums[1]) {
return nums[0];
}
for (int i = 2; i < nums.length - 1; i++) {
if (nums[i] != nums[i - 1] && nums[i] != nums[i + 1]) {
return nums[i];
}
}
return nums[nums.length - 1];
}
public int addDigits(int num) {
int result = 0;
while (num > 0) {
result += num % 10;
num /= 10;
}
return result > 9 ? addDigits(result) : result;
}
public boolean canWinNim(int n) {
return n % 4 != 0;
}
}