Skip to content

Commit 1f63d60

Browse files
committed
20190105
1 parent 98c5b10 commit 1f63d60

File tree

4 files changed

+132
-3
lines changed

4 files changed

+132
-3
lines changed

code/lc148.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package code;
2+
/*
3+
* 148. Sort List
4+
* 题意:链表排序
5+
* 难度:Medium
6+
* 分类:Linked List, Sort
7+
* 思路:快慢指针把链表分成两半,在merge两个链表
8+
* Tips:空间复杂度不是O(1)的,但是几个高票答案都是这样写的,面试给出这样的代码应该也够了
9+
*/
10+
public class lc148 {
11+
public class ListNode {
12+
int val;
13+
ListNode next;
14+
ListNode(int x) { val = x; }
15+
}
16+
17+
public ListNode sortList(ListNode head) {
18+
if( head==null || head.next == null ){
19+
return head;
20+
}
21+
ListNode slow = head;
22+
ListNode fast = head.next;
23+
while( fast.next!=null && fast.next.next!=null ){ //把链表分成两半
24+
slow = slow.next;
25+
fast = fast.next.next;
26+
}
27+
ListNode l2 = sortList(slow.next);
28+
slow.next = null;
29+
ListNode l1 = sortList(head);
30+
return mergeList(l1, l2);
31+
}
32+
33+
public ListNode mergeList(ListNode l1, ListNode l2){
34+
ListNode res = new ListNode(0);
35+
ListNode head = res;
36+
while( l1!=null && l2!=null ){
37+
if(l1.val<l2.val){
38+
res.next = l1;
39+
l1 = l1.next;
40+
res = res.next;
41+
}else{
42+
res.next = l2;
43+
l2 = l2.next;
44+
res = res.next;
45+
}
46+
}
47+
if(l1!=null){
48+
res.next = l1;
49+
}
50+
if(l2!=null){
51+
res.next = l2;
52+
}
53+
return head.next;
54+
}
55+
}

code/lc152.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package code;
2+
/*
3+
* 152. Maximum Product Subarray
4+
* 题意:连续子序列最大乘积
5+
* 难度:Medium
6+
* 分类:Array, Dynamic Programming
7+
* 思路:保存最大,最小值,因为负负得正。dp不用保存数组,空间可以压缩。
8+
* Tips:Product是乘的意思
9+
*/
10+
public class lc152 {
11+
public static void main(String[] args) {
12+
int[] nums = {-4,-3,-2};
13+
System.out.println(maxProduct(nums));
14+
}
15+
public static int maxProduct(int[] nums) {
16+
int max = nums[0], min = nums[0], res = nums[0];
17+
for (int i = 1; i < nums.length ; i++) {
18+
if(nums[i]>0){
19+
max = Math.max(nums[i],nums[i]*max);
20+
min = Math.min(nums[i],nums[i]*min);
21+
}else{
22+
int temp = max; //注意maxh会被替换,先保存下
23+
max = Math.max(nums[i],nums[i]*min);
24+
min = Math.min(nums[i],nums[i]*temp);
25+
}
26+
res = Math.max(max, res);
27+
}
28+
return res;
29+
}
30+
}

code/lc155.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package code;
2+
/*
3+
* 155. Min Stack
4+
* 题意:设计一个栈,这个栈有一个getmin方法
5+
* 难度:Easy
6+
* 分类:Stack, Design
7+
* 思路:每次替换最小值时,把当前最小值入栈用以记录,以便之后更新最小值
8+
* Tips:
9+
*/
10+
import java.util.Stack;
11+
12+
public class lc155 {
13+
class MinStack {
14+
Stack<Integer> st;
15+
int min = Integer.MAX_VALUE;
16+
/** initialize your data structure here. */
17+
public MinStack() {
18+
this.st = new Stack<Integer>();
19+
}
20+
21+
public void push(int x) {
22+
if(x<=min){ //别忘了=
23+
st.push(this.min);
24+
this.min = x;
25+
}
26+
st.push(x);
27+
}
28+
29+
public void pop() {
30+
int x = st.pop();
31+
if(x==this.min){
32+
this.min = st.pop();
33+
}
34+
}
35+
36+
public int top() {
37+
return st.peek();
38+
}
39+
40+
public int getMin() {
41+
return this.min;
42+
}
43+
}
44+
}

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ Welcome to improve this project with me.*
7676
| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | Easy | [Java](./code/lc141.java)
7777
| 142 | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) |Medium| [Java](./code/lc142.java)
7878
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | Hard | [Java](./code/lc146.java)
79-
| 148 | [Sort List](https://leetcode.com/problems/sort-list/) |Medium| [Java]
80-
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) |Medium| [Java]
81-
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | Easy | [Java]
79+
| 148 | [Sort List](https://leetcode.com/problems/sort-list/) |Medium| [Java](./code/lc148.java)
80+
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) |Medium| [Java](./code/lc152.java)
81+
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | Easy | [Java](./code/lc155.java)
8282
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | Easy | [Java]
8383
| 169 | [Majority Element](https://leetcode.com/problems/majority-element/) | Easy | [Java]
8484
| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | Easy | [Java]

0 commit comments

Comments
 (0)