- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
Latest commit
executable file
·124 lines (114 loc) · 3.74 KB
/
Copy pathSolution.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
importjava.util.Arrays;
importjava.util.HashMap;
publicclassSolution {
publicint[] twoSum(int[] nums, inttarget) {
int[] result = newint[2];
HashMap<Integer, Integer> map = newHashMap<>();
for (inti = 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);
}
returnresult;
}
publicintlengthOfLongestSubstring(Strings) {
intresult = 0;
intbeginIndex = -1;
HashMap<Character, Integer> map = newHashMap<>();
for (inti = 0; i < s.length(); i++) {
charch = s.charAt(i);
if (map.containsKey(ch)) {
beginIndex = Math.max(beginIndex, map.get(ch));
}
map.put(ch, i);
result = Math.max(result, i - beginIndex);
}
returnresult;
}
publicdoublefindMedianSortedArrays(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);
returnnums1.length % 2 == 0
? (nums1[(nums1.length) / 2] + nums1[(nums1.length - 1) / 2]) / 2.0 : nums1[(nums1.length) / 2];
}
publicintreverse(intx) {
intresult = 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) {
return0;
}
result = result * 10 + x % 10;
x /= 10;
}
returnresult;
}
publicintmyAtoi(Stringstr) {
str = str.trim();
Stringresult = "";
for (inti = 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 {
returnInteger.parseInt(result);
} catch (NumberFormatExceptione) {
if (result.length() > 1) {
if (result.charAt(0) == '-') {
returnInteger.MIN_VALUE;
}
returnInteger.MAX_VALUE;
}
return0;
}
}
publicbooleanisNumber(Strings) {
s = s.trim();
try {
Double.parseDouble(s);
switch (s.charAt(s.length() - 1)) {
case'D':
case'f':
returnfalse;
default:
returntrue;
}
} catch (RuntimeExceptione) {
returnfalse;
}
}
publicintsingleNumber(int[] nums) {
Arrays.sort(nums);
if (nums.length == 1 || nums[0] != nums[1]) {
returnnums[0];
}
for (inti = 2; i < nums.length - 1; i++) {
if (nums[i] != nums[i - 1] && nums[i] != nums[i + 1]) {
returnnums[i];
}
}
returnnums[nums.length - 1];
}
publicintaddDigits(intnum) {
intresult = 0;
while (num > 0) {
result += num % 10;
num /= 10;
}
returnresult > 9 ? addDigits(result) : result;
}
publicbooleancanWinNim(intn) {
returnn % 4 != 0;
}
}