Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 18a6ddc

Browse files
新增面试题
1 parent cfbce0e commit 18a6ddc

File tree

37 files changed

+1192
-174
lines changed

37 files changed

+1192
-174
lines changed

‎README.md‎

Lines changed: 36 additions & 20 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.hit.basinfo.base_data_structure;
1+
package com.hit.basinfo.data_structure;
22

33
import com.hit.common.ListNode;
44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.hit.basinfo.base_data_structure;
1+
package com.hit.basinfo.data_structure;
22

33
import com.hit.common.ListNode;
44

‎codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/array/_122.java‎

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,47 +35,6 @@
3535
*/
3636
public class _122 {
3737
public int maxProfit(int[] prices) {
38-
return calculate(prices, 0);
39-
}
40-
41-
private int calculate(int prices[], int s) {
42-
if (s >= prices.length)
43-
return 0;
44-
int max = 0;
45-
for (int start = s; start < prices.length; start++) {
46-
int maxprofit = 0;
47-
for (int i = start + 1; i < prices.length; i++) {
48-
if (prices[start] < prices[i]) {
49-
int profit = calculate(prices, i + 1) + prices[i] - prices[start];
50-
if (profit > maxprofit)
51-
maxprofit = profit;
52-
}
53-
}
54-
if (maxprofit > max)
55-
max = maxprofit;
56-
}
57-
return max;
58-
}
59-
60-
public int maxProfit2(int[] prices) {
61-
if (prices == null || prices.length == 0) return 0;
62-
int i = 0;
63-
int valley = prices[0];
64-
int peak = prices[0];
65-
int maxprofit = 0;
66-
while (i < prices.length - 1) {
67-
while (i < prices.length - 1 && prices[i] >= prices[i + 1])
68-
i++;
69-
valley = prices[i];
70-
while (i < prices.length - 1 && prices[i] <= prices[i + 1])
71-
i++;
72-
peak = prices[i];
73-
maxprofit += peak - valley;
74-
}
75-
return maxprofit;
76-
}
77-
78-
public int maxProfit3(int[] prices) {
7938
int maxProfit = 0;
8039
for (int i = 1; i < prices.length; i++) {
8140
if (prices[i] > prices[i - 1]) {

‎codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/hard_collection/array_and_strings/_128.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,17 @@
1919
* Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
2020
*/
2121
public class _128 {
22-
2322
public int longestConsecutive(int[] nums) {
2423
int longestStreak = 0;
25-
2624
for (int num : nums) {
2725
int currentNum = num;
2826
int currentStreak = 1;
29-
3027
while (arrayContains(nums, currentNum + 1)) {
3128
currentNum += 1;
3229
currentStreak += 1;
3330
}
34-
3531
longestStreak = Math.max(longestStreak, currentStreak);
3632
}
37-
3833
return longestStreak;
3934
}
4035

‎codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/hard_collection/design/_146.java‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ class DLinkedNode {
3939
}
4040

4141
private Hashtable<Integer, DLinkedNode> cache = new Hashtable<>();
42-
private int size;
43-
private int capacity;
42+
private int size, capacity;
4443
private DLinkedNode head, tail;
4544

4645
private void addNode(DLinkedNode node) {
@@ -49,7 +48,6 @@ private void addNode(DLinkedNode node) {
4948
*/
5049
node.prev = head;
5150
node.next = head.next;
52-
5351
head.next.prev = node;
5452
head.next = node;
5553
}
@@ -60,7 +58,6 @@ private void removeNode(DLinkedNode node) {
6058
*/
6159
DLinkedNode prev = node.prev;
6260
DLinkedNode next = node.next;
63-
6461
prev.next = next;
6562
next.prev = prev;
6663
}
@@ -77,9 +74,9 @@ private DLinkedNode popTail() {
7774
/**
7875
* Pop the current tail.
7976
*/
80-
DLinkedNode res = tail.prev;
81-
removeNode(res);
82-
return res;
77+
DLinkedNode tailNode = tail.prev;
78+
removeNode(tailNode);
79+
return tailNode;
8380
}
8481

8582
public LRUCache(int capacity) {

‎codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/hard_collection/linked_list/_148.java‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,18 @@
1919
*/
2020
public class _148 {
2121
public ListNode sortList(ListNode head) {
22-
if (head == null || head.next == null)
23-
return head;
24-
22+
if (head == null || head.next == null) return head;
2523
// step 1. cut the list to two halves
2624
ListNode prev = null, slow = head, fast = head;
27-
2825
while (fast != null && fast.next != null) {
2926
prev = slow;
3027
slow = slow.next;
3128
fast = fast.next.next;
3229
}
33-
3430
prev.next = null;
35-
3631
// step 2. sort each half
3732
ListNode l1 = sortList(head);
3833
ListNode l2 = sortList(slow);
39-
4034
// step 3. merge l1 and l2
4135
return merge(l1, l2);
4236
}
@@ -53,12 +47,8 @@ private ListNode merge(ListNode l1, ListNode l2) {
5347
}
5448
p = p.next;
5549
}
56-
if (l1 != null)
57-
p.next = l1;
58-
59-
if (l2 != null)
60-
p.next = l2;
61-
50+
if (l1 != null) p.next = l1;
51+
if (l2 != null) p.next = l2;
6252
return root.next;
6353
}
6454
}

‎codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/medium_collection/sorting_and_searching/_215.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,13 @@ private void swap(int[] A, int i, int j) {
6262
public int findKthLargest2(int[] nums, int k) {
6363
// init heap 'the smallest element first'
6464
PriorityQueue<Integer> heap = new PriorityQueue<>();
65-
6665
// keep k largest elements in the heap
6766
for (int n : nums) {
6867
heap.add(n);
69-
if (heap.size() > k)
68+
if (heap.size() > k) {
7069
heap.poll();
70+
}
7171
}
72-
7372
// output
7473
return heap.poll();
7574
}

‎codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/medium_collection/sorting_and_searching/_56.java‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@
2525
*/
2626
public class _56 {
2727
public int[][] merge(int[][] intervals) {
28-
if (intervals.length <= 1) return intervals;
29-
// Sort by ascending starting point
30-
Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0]));
31-
List<int[]> result = new ArrayList<>();
28+
if (intervals.length < 1) return intervals;
29+
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
30+
List<int[]> ans = new ArrayList<>();
3231
int[] newInterval = intervals[0];
33-
result.add(newInterval);
32+
ans.add(newInterval);
3433
for (int[] interval : intervals) {
35-
if (interval[0] <= newInterval[1]) {// Overlapping intervals, move the end if needed
36-
newInterval[1] = Math.max(newInterval[1], interval[1]);
37-
} else {// Disjoint intervals, add the new interval to the list
34+
if (interval[0] <= newInterval[1]) {
35+
newInterval[1] = Math.max(interval[1], newInterval[1]);
36+
} else {
3837
newInterval = interval;
39-
result.add(newInterval);
38+
ans.add(newInterval);
4039
}
4140
}
42-
return result.toArray(new int[result.size()][]);
41+
return ans.toArray(new int[ans.size()][]);
4342
}
4443
}

‎codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/medium_collection/trees_and_graphs/_103.java‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@
3131
*/
3232
public class _103 {
3333
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
34-
List<List<Integer>> sol = new ArrayList<>();
35-
travel(root, sol, 0);
36-
return sol;
34+
List<List<Integer>> ans = new ArrayList<>();
35+
helper(root, ans, 0);
36+
return ans;
3737
}
3838

39-
private void travel(TreeNode curr, List<List<Integer>> sol, int level) {
39+
private void helper(TreeNode curr, List<List<Integer>> ans, int level) {
4040
if (curr == null) return;
41-
42-
if (sol.size() <= level) {
41+
if (ans.size() <= level) {
4342
List<Integer> newLevel = new LinkedList<>();
44-
sol.add(newLevel);
43+
ans.add(newLevel);
4544
}
46-
47-
List<Integer> collection = sol.get(level);
48-
if (level % 2 == 0) collection.add(curr.val);
49-
else collection.add(0, curr.val);
50-
51-
travel(curr.left, sol, level + 1);
52-
travel(curr.right, sol, level + 1);
45+
List<Integer> collection = ans.get(level);
46+
if (level % 2 == 0) {
47+
collection.add(curr.val);
48+
} else {
49+
collection.add(0, curr.val);
50+
}
51+
helper(curr.left, ans, level + 1);
52+
helper(curr.right, ans, level + 1);
5353
}
5454
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /