-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode.java
More file actions
70 lines (58 loc) · 1.57 KB
/
Copy pathLeetcode.java
File metadata and controls
70 lines (58 loc) · 1.57 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
class Problems {
// Max Consecutive Ones
public int Problem_485(int[] nums) {
int maxFreq = 0, tempFreq = 0;
for (int ele : nums) {
if (ele == 0) {
tempFreq = 0;
} else {
tempFreq += 1;
if (tempFreq > maxFreq || maxFreq == 0) {
maxFreq = tempFreq;
}
}
}
return maxFreq;
}
// Reverse Integer
public int Problem_7(int x) {
long reversed = 0;
int remainder = 0;
while (x != 0) {
remainder = x % 10;
reversed = (reversed * 10) + remainder;
x /= 10;
}
if (reversed > Integer.MAX_VALUE || reversed < Integer.MIN_VALUE) {
return 0;
}
if (x < 0) {
return (int) (-1 * reversed);
}
return (int) reversed;
}
// Find the Duplicate Number
public int Problem_287(int[] nums) {
// Better O(3n) time and O(n) space
int max = nums[0];
for (int num : nums) {
if (num > max)
max = num;
}
int[] hash = new int[max + 1];
for (int num : nums) {
hash[num]++;
}
for (int idx = 0; idx < hash.length; idx++) {
if (hash[idx] > 1)
return idx;
}
return -1;
}
}
public class Leetcode {
public static void main(String[] args) {
Problems problems = new Problems();
System.out.println(problems.Problem_287(new int[] {3,3,3,3,3 }));
}
}