Skip to content

Commit 228ede7

Browse files
committed
update
1 parent 2bd811a commit 228ede7

File tree

4 files changed

+134
-20
lines changed
  • codes/java/leetcodes/src/main/java/com/hit/basmath

4 files changed

+134
-20
lines changed

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/strings/_7.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,25 @@ public int reverse(int x) {
4242

4343
return result;
4444
}
45+
46+
public int reverse2(int x) {
47+
int rev = 0;
48+
49+
while (x != 0) {
50+
int pop = x % 10;
51+
x /= 10;
52+
53+
if (rev > Integer.MAX_VALUE / 10 ||
54+
(rev == Integer.MAX_VALUE / 10 && pop > 7)) {
55+
return 0;
56+
}
57+
58+
if (rev < Integer.MIN_VALUE / 10 ||
59+
(rev == Integer.MIN_VALUE / 10 && pop < -8)) {
60+
return 0;
61+
}
62+
rev = rev * 10 + pop;
63+
}
64+
return rev;
65+
}
4566
}

codes/java/leetcodes/src/main/java/com/hit/basmath/learn/hash_table/_3.java

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.hit.basmath.learn.hash_table;
22

33
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
47

58
/**
69
* 3. Longest Substring Without Repeating Characters
@@ -28,17 +31,77 @@
2831
* Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
2932
*/
3033
public class _3 {
34+
/**
35+
* Solution 1: Violence method
36+
*
37+
* @param s
38+
* @return
39+
*/
3140
public int lengthOfLongestSubstring(String s) {
32-
if (s.length() == 0) return 0;
33-
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
34-
int max = 0;
35-
for (int i = 0, j = 0; i < s.length(); ++i) {
36-
if (map.containsKey(s.charAt(i))) {
37-
j = Math.max(j, map.get(s.charAt(i)) + 1);
41+
int n = s.length();
42+
int ans = 0;
43+
for (int i = 0; i < n; i++) {
44+
for (int j = i + 1; j <= n; j++) {
45+
if (allUnique(s, i, j)) {
46+
ans = Math.max(ans, j - i);
47+
}
3848
}
39-
map.put(s.charAt(i), i);
40-
max = Math.max(max, i - j + 1);
4149
}
42-
return max;
50+
return ans;
51+
}
52+
53+
private boolean allUnique(String s, int start, int end) {
54+
Set<Character> aSet = new HashSet<>();
55+
for (int i = start; i < end; i++) {
56+
if (aSet.contains(s.charAt(i))) {
57+
return false;
58+
} else {
59+
aSet.add(s.charAt(i));
60+
}
61+
}
62+
return true;
63+
}
64+
65+
/**
66+
* Solution 2: Slide window method
67+
*
68+
* @param s
69+
* @return
70+
*/
71+
public int lengthOfLongestSubstring2(String s) {
72+
int n = s.length();
73+
Set<Character> set = new HashSet<>();
74+
int ans = 0, i = 0, j = 0;
75+
while (i < n && j < n) {
76+
// try to extend the range [i, j]
77+
if (!set.contains(s.charAt(j))) {
78+
set.add(s.charAt(j++));
79+
ans = Math.max(ans, j - i);
80+
} else {
81+
set.remove(s.charAt(i++));
82+
}
83+
}
84+
return ans;
85+
}
86+
87+
/**
88+
* Solution 3: Slide window method of optimize
89+
*
90+
* @param s
91+
* @return
92+
*/
93+
public int lengthOfLongestSubstring3(String s) {
94+
int n = s.length(), ans = 0;
95+
// current index of character
96+
Map<Character, Integer> map = new HashMap<>();
97+
// try to extend the range [i, j]
98+
for (int j = 0, i = 0; j < n; j++) {
99+
if (map.containsKey(s.charAt(j))) {
100+
i = Math.max(map.get(s.charAt(j)), i);
101+
}
102+
ans = Math.max(ans, j - i + 1);
103+
map.put(s.charAt(j), j + 1);
104+
}
105+
return ans;
43106
}
44107
}

codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_35.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@
2929
*/
3030
public class _35 {
3131
public int searchInsert(int[] nums, int target) {
32-
int low = 0, high = nums.length - 1;
33-
while (low <= high) {
34-
int mid = (low + high) / 2;
35-
if (nums[mid] == target) return mid;
36-
else if (nums[mid] > target) high = mid - 1;
37-
else low = mid + 1;
32+
int left = 0, right = nums.length - 1;
33+
while (left <= right) {
34+
int mid = (left + right) / 2;
35+
if (nums[mid] == target) {
36+
return mid;
37+
} else if (nums[mid] < target) {
38+
left = mid + 1;
39+
} else {
40+
right = mid - 1;
41+
}
3842
}
39-
return low;
43+
return left;
4044
}
4145
}

codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_9.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,43 @@
2828
*/
2929
public class _9 {
3030
public boolean isPalindrome(int x) {
31+
String reversedStr = (new StringBuilder(x + "")).reverse().toString();
32+
return (x + "").equals(reversedStr);
33+
}
34+
35+
public boolean isPalindrome2(int x) {
36+
if (x < 0)
37+
return false;
38+
39+
int div = 1;
40+
41+
while (x / div >= 10)
42+
div *= 10;
43+
44+
while (x > 0) {
45+
int left = x/ div;
46+
int right = x % 10;
47+
if (left != right) {
48+
return false;
49+
}
50+
x = (x % div) / 10;
51+
div /= 100;
52+
}
53+
return true;
54+
}
55+
56+
public boolean isPalindrome3(int x) {
3157
// negative and 10 times numbers is not a palindrome
3258
if (x < 0 || (x != 0 && x % 10 == 0)) {
3359
return false;
3460
}
3561

36-
int rev = 0;
37-
while (x > rev) {
38-
rev = rev * 10 + x % 10;
62+
int revertedNumber = 0;
63+
while (x > revertedNumber) {
64+
revertedNumber = revertedNumber * 10 + x % 10;
3965
x = x / 10;
4066
}
4167

42-
return (x == rev || x == rev / 10);
68+
return (x == revertedNumber || x == revertedNumber / 10);
4369
}
4470
}

0 commit comments

Comments
 (0)