Skip to content

Commit 7479b84

Browse files
左程云左程云
左程云
authored and
左程云
committed
first commit
0 parents  commit 7479b84

File tree

354 files changed

+6594
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

354 files changed

+6594
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
705 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
653 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package topinterviewquestions;
2+
3+
import java.util.HashMap;
4+
5+
public class Problem_0001_TwoSum {
6+
7+
public static int[] twoSum(int[] nums, int target) {
8+
HashMap<Integer, Integer> map = new HashMap<>();
9+
for (int i = 0; i < nums.length; i++) {
10+
if (map.containsKey(target - nums[i])) {
11+
return new int[] { map.get(target - nums[i]), i };
12+
}
13+
map.put(nums[i], i);
14+
}
15+
return new int[] { -1, -1 };
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package topinterviewquestions;
2+
3+
public class Problem_0002_AddTwoNumbers {
4+
5+
// 不要提交这个类描述
6+
public static class ListNode {
7+
public int val;
8+
public ListNode next;
9+
10+
public ListNode(int value) {
11+
this.val = value;
12+
}
13+
}
14+
15+
public static ListNode addTwoNumbers(ListNode head1, ListNode head2) {
16+
int ca = 0;
17+
int n1 = 0;
18+
int n2 = 0;
19+
int n = 0;
20+
ListNode c1 = head1;
21+
ListNode c2 = head2;
22+
ListNode node = null;
23+
ListNode pre = null;
24+
while (c1 != null || c2 != null) {
25+
n1 = c1 != null ? c1.val : 0;
26+
n2 = c2 != null ? c2.val : 0;
27+
n = n1 + n2 + ca;
28+
pre = node;
29+
node = new ListNode(n % 10);
30+
node.next = pre;
31+
ca = n / 10;
32+
c1 = c1 != null ? c1.next : null;
33+
c2 = c2 != null ? c2.next : null;
34+
}
35+
if (ca == 1) {
36+
pre = node;
37+
node = new ListNode(1);
38+
node.next = pre;
39+
}
40+
return reverseList(node);
41+
}
42+
43+
public static ListNode reverseList(ListNode head) {
44+
ListNode pre = null;
45+
ListNode next = null;
46+
while (head != null) {
47+
next = head.next;
48+
head.next = pre;
49+
pre = head;
50+
head = next;
51+
}
52+
return pre;
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package topinterviewquestions;
2+
3+
public class Problem_0003_LongestSubstringWithoutRepeatingCharacters {
4+
5+
public static int lengthOfLongestSubstring(String str) {
6+
if (str == null || str.equals("")) {
7+
return 0;
8+
}
9+
char[] chas = str.toCharArray();
10+
int[] map = new int[256];
11+
for (int i = 0; i < 256; i++) {
12+
map[i] = -1;
13+
}
14+
int len = 0;
15+
int pre = -1;
16+
int cur = 0;
17+
for (int i = 0; i != chas.length; i++) {
18+
pre = Math.max(pre, map[chas[i]]);
19+
cur = i - pre;
20+
len = Math.max(len, cur);
21+
map[chas[i]] = i;
22+
}
23+
return len;
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package topinterviewquestions;
2+
3+
public class Problem_0004_MedianOfTwoSortedArrays {
4+
5+
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
6+
int size = nums1.length + nums2.length;
7+
boolean even = (size & 1) == 0;
8+
if (nums1.length != 0 && nums2.length != 0) {
9+
if (even) {
10+
return (double) (findKthNum(nums1, nums2, size / 2) + findKthNum(nums1, nums2, size / 2 + 1)) / 2D;
11+
} else {
12+
return findKthNum(nums1, nums2, size / 2 + 1);
13+
}
14+
} else if (nums1.length != 0) {
15+
if (even) {
16+
return (double) (nums1[(size - 1) / 2] + nums1[size / 2]) / 2;
17+
} else {
18+
return nums1[size / 2];
19+
}
20+
} else if (nums2.length != 0) {
21+
if (even) {
22+
return (double) (nums2[(size - 1) / 2] + nums2[size / 2]) / 2;
23+
} else {
24+
return nums2[size / 2];
25+
}
26+
} else {
27+
return 0;
28+
}
29+
}
30+
31+
public static int findKthNum(int[] arr1, int[] arr2, int kth) {
32+
int[] longs = arr1.length >= arr2.length ? arr1 : arr2;
33+
int[] shorts = arr1.length < arr2.length ? arr1 : arr2;
34+
int l = longs.length;
35+
int s = shorts.length;
36+
if (kth <= s) {
37+
return getUpMedian(shorts, 0, kth - 1, longs, 0, kth - 1);
38+
}
39+
if (kth > l) {
40+
if (shorts[kth - l - 1] >= longs[l - 1]) {
41+
return shorts[kth - l - 1];
42+
}
43+
if (longs[kth - s - 1] >= shorts[s - 1]) {
44+
return longs[kth - s - 1];
45+
}
46+
return getUpMedian(shorts, kth - l, s - 1, longs, kth - s, l - 1);
47+
}
48+
if (longs[kth - s - 1] >= shorts[s - 1]) {
49+
return longs[kth - s - 1];
50+
}
51+
return getUpMedian(shorts, 0, s - 1, longs, kth - s, kth - 1);
52+
}
53+
54+
public static int getUpMedian(int[] a1, int s1, int e1, int[] a2, int s2, int e2) {
55+
int mid1 = 0;
56+
int mid2 = 0;
57+
int offset = 0;
58+
while (s1 < e1) {
59+
mid1 = (s1 + e1) / 2;
60+
mid2 = (s2 + e2) / 2;
61+
offset = ((e1 - s1 + 1) & 1) ^ 1;
62+
if (a1[mid1] > a2[mid2]) {
63+
e1 = mid1;
64+
s2 = mid2 + offset;
65+
} else if (a1[mid1] < a2[mid2]) {
66+
s1 = mid1 + offset;
67+
e2 = mid2;
68+
} else {
69+
return a1[mid1];
70+
}
71+
}
72+
return Math.min(a1[s1], a2[s2]);
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package topinterviewquestions;
2+
3+
public class Problem_0005_LongestPalindromicSubstring {
4+
5+
public static String longestPalindrome(String str) {
6+
if (str == null || str.length() == 0) {
7+
return "";
8+
}
9+
char[] charArr = manacherString(str);
10+
int[] pArr = new int[charArr.length];
11+
int index = -1;
12+
int pR = -1;
13+
int max = Integer.MIN_VALUE;
14+
int mid = 0;
15+
for (int i = 0; i != charArr.length; i++) {
16+
pArr[i] = pR > i ? Math.min(pArr[2 * index - i], pR - i) : 1;
17+
while (i + pArr[i] < charArr.length && i - pArr[i] > -1) {
18+
if (charArr[i + pArr[i]] == charArr[i - pArr[i]])
19+
pArr[i]++;
20+
else {
21+
break;
22+
}
23+
}
24+
if (i + pArr[i] > pR) {
25+
pR = i + pArr[i];
26+
index = i;
27+
}
28+
if (max < pArr[i]) {
29+
max = pArr[i];
30+
mid = i;
31+
}
32+
}
33+
mid = (mid - 1) / 2;
34+
max = max - 1;
35+
return str.substring((max & 1) == 0 ? mid - (max / 2) + 1 : mid - (max / 2), mid + (max / 2) + 1);
36+
}
37+
38+
public static char[] manacherString(String str) {
39+
char[] charArr = str.toCharArray();
40+
char[] res = new char[str.length() * 2 + 1];
41+
int index = 0;
42+
for (int i = 0; i != res.length; i++) {
43+
res[i] = (i & 1) == 0 ? '#' : charArr[index++];
44+
}
45+
return res;
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package topinterviewquestions;
2+
3+
public class Problem_0007_ReverseInteger {
4+
5+
public static int reverse(int x) {
6+
boolean neg = ((x >>> 31) & 1) == 1;
7+
x = neg ? x : -x;
8+
int m = Integer.MIN_VALUE / 10;
9+
int o = Integer.MIN_VALUE % 10;
10+
int res = 0;
11+
while (x != 0) {
12+
if (res < m || (res == m && x % 10 < o)) {
13+
return 0;
14+
}
15+
res = res * 10 + x % 10;
16+
x /= 10;
17+
}
18+
return neg ? res : Math.abs(res);
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package topinterviewquestions;
2+
3+
public class Problem_0008_StringToInteger {
4+
5+
public static int myAtoi(String str) {
6+
if (str == null || str.equals("")) {
7+
return 0;
8+
}
9+
str = removeHeadZero(str.trim());
10+
if (str == null || str.equals("")) {
11+
return 0;
12+
}
13+
char[] chas = str.toCharArray();
14+
if (!isValid(chas)) {
15+
return 0;
16+
}
17+
boolean posi = chas[0] == '-' ? false : true;
18+
int minq = Integer.MIN_VALUE / 10;
19+
int minr = Integer.MIN_VALUE % 10;
20+
int res = 0;
21+
int cur = 0;
22+
for (int i = (chas[0] == '-' || chas[0] == '+') ? 1 : 0; i < chas.length; i++) {
23+
cur = '0' - chas[i];
24+
if ((res < minq) || (res == minq && cur < minr)) {
25+
return posi ? Integer.MAX_VALUE : Integer.MIN_VALUE;
26+
}
27+
res = res * 10 + cur;
28+
}
29+
if (posi && res == Integer.MIN_VALUE) {
30+
return Integer.MAX_VALUE;
31+
}
32+
return posi ? -res : res;
33+
}
34+
35+
public static String removeHeadZero(String str) {
36+
boolean r = (str.startsWith("+") || str.startsWith("-"));
37+
int s = r ? 1 : 0;
38+
for (; s < str.length(); s++) {
39+
if (str.charAt(s) != '0') {
40+
break;
41+
}
42+
}
43+
int e = -1;
44+
for (int i = str.length() - 1; i >= (r ? 1 : 0); i--) {
45+
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
46+
e = i;
47+
}
48+
}
49+
return (r ? String.valueOf(str.charAt(0)) : "") + str.substring(s, e == -1 ? str.length() : e);
50+
}
51+
52+
public static boolean isValid(char[] chas) {
53+
if (chas[0] != '-' && chas[0] != '+' && (chas[0] < '0' || chas[0] > '9')) {
54+
return false;
55+
}
56+
if ((chas[0] == '-' || chas[0] == '+') && chas.length == 1) {
57+
return false;
58+
}
59+
for (int i = 1; i < chas.length; i++) {
60+
if (chas[i] < '0' || chas[i] > '9') {
61+
return false;
62+
}
63+
}
64+
return true;
65+
}
66+
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package topinterviewquestions;
2+
3+
public class Problem_0010_RegularExpressionMatching {
4+
5+
public static boolean isMatch(String str, String exp) {
6+
if (str == null || exp == null) {
7+
return false;
8+
}
9+
char[] s = str.toCharArray();
10+
char[] e = exp.toCharArray();
11+
if (!isValid(s, e)) {
12+
return false;
13+
}
14+
boolean[][] dp = initDPMap(s, e);
15+
for (int i = s.length - 1; i > -1; i--) {
16+
for (int j = e.length - 2; j > -1; j--) {
17+
if (e[j + 1] != '*') {
18+
dp[i][j] = (s[i] == e[j] || e[j] == '.') && dp[i + 1][j + 1];
19+
} else {
20+
int si = i;
21+
while (si != s.length && (s[si] == e[j] || e[j] == '.')) {
22+
if (dp[si][j + 2]) {
23+
dp[i][j] = true;
24+
break;
25+
}
26+
si++;
27+
}
28+
if (dp[i][j] != true) {
29+
dp[i][j] = dp[si][j + 2];
30+
}
31+
}
32+
}
33+
}
34+
return dp[0][0];
35+
}
36+
37+
public static boolean isValid(char[] s, char[] e) {
38+
for (int i = 0; i < s.length; i++) {
39+
if (s[i] == '*' || s[i] == '.') {
40+
return false;
41+
}
42+
}
43+
for (int i = 0; i < e.length; i++) {
44+
if (e[i] == '*' && (i == 0 || e[i - 1] == '*')) {
45+
return false;
46+
}
47+
}
48+
return true;
49+
}
50+
51+
public static boolean[][] initDPMap(char[] s, char[] e) {
52+
int slen = s.length;
53+
int elen = e.length;
54+
boolean[][] dp = new boolean[slen + 1][elen + 1];
55+
dp[slen][elen] = true;
56+
for (int j = elen - 2; j > -1; j = j - 2) {
57+
if (e[j] != '*' && e[j + 1] == '*') {
58+
dp[slen][j] = true;
59+
} else {
60+
break;
61+
}
62+
}
63+
if (slen > 0 && elen > 0) {
64+
if ((e[elen - 1] == '.' || s[slen - 1] == e[elen - 1])) {
65+
dp[slen - 1][elen - 1] = true;
66+
}
67+
}
68+
return dp;
69+
}
70+
71+
}

0 commit comments

Comments
 (0)