Skip to content

Commit 7ccd186

Browse files
committed
20190109
1 parent 5c467d5 commit 7ccd186

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

code/lc226.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package code;
2+
3+
import java.util.Stack;
4+
5+
/*
6+
* 226. Invert Binary Tree
7+
* 题意:反转二叉树
8+
* 难度:Easy
9+
* 分类:Tree
10+
* 思路:递归或迭代,两种思路,都很简单
11+
* Tips:
12+
*/
13+
public class lc226 {
14+
public class TreeNode {
15+
int val;
16+
TreeNode left;
17+
TreeNode right;
18+
TreeNode(int x) { val = x; }
19+
}
20+
public TreeNode invertTree(TreeNode root) {
21+
//递归
22+
if(root==null)
23+
return null;
24+
TreeNode temp = root.left;
25+
root.left = root.right;
26+
root.right = temp;
27+
invertTree(root.left);
28+
invertTree(root.right);
29+
return root;
30+
}
31+
32+
public TreeNode invertTree2(TreeNode root) {
33+
//迭代
34+
if(root==null)
35+
return null;
36+
Stack<TreeNode> st = new Stack();
37+
st.add(root);
38+
while(!st.isEmpty()){
39+
TreeNode tn = st.pop();
40+
TreeNode temp = tn.left;
41+
tn.left = tn.right;
42+
tn.right = temp;
43+
if(tn.left!=null)
44+
st.add(tn.left);
45+
if(tn.right!=null)
46+
st.add(tn.right);
47+
}
48+
return root;
49+
}
50+
}

code/lc234.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package code;
2+
/*
3+
* 234. Palindrome Linked List
4+
* 题意:判断链表是否是回文的
5+
* 难度:Easy
6+
* 分类:LinkedList, Two Pointers
7+
* 思路:反转一般就行了,避免了空间开销
8+
* Tips:很好的题,考了 Two Pointers, 还考了链表反转
9+
*/
10+
public class lc234 {
11+
public class ListNode {
12+
int val;
13+
ListNode next;
14+
ListNode(int x) { val = x; }
15+
}
16+
17+
public boolean isPalindrome(ListNode head) {
18+
if(head==null||head.next==null)
19+
return true;
20+
ListNode slow = head;
21+
ListNode fast = head;
22+
while (fast.next != null && fast.next.next != null) {
23+
slow = slow.next;
24+
fast = fast.next.next;
25+
}
26+
ListNode head2 = reverse(slow.next,null);
27+
slow.next = null; //注意截断一下
28+
while(head2!=null&&head!=null){
29+
if(head2.val!=head.val)
30+
return false;
31+
head = head.next;
32+
head2 = head2.next;
33+
}
34+
return true;
35+
}
36+
public ListNode reverse(ListNode head, ListNode pre){
37+
ListNode next = head.next;
38+
head.next = pre;
39+
if(next==null)
40+
return head;
41+
return reverse(next, head);
42+
}
43+
}

code/lc239.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package code;
2+
/*
3+
* 239. Sliding Window Maximum
4+
* 题意:滑动窗口最大值
5+
* 难度:Hard
6+
* 分类:Heap
7+
* 思路:用双向队列,保证队列里是递增的。单调队列,好好学习一下。
8+
* Tips:与lc84做比较,84是递增栈
9+
*/
10+
import java.util.ArrayDeque;
11+
import java.util.Deque;
12+
13+
public class lc239 {
14+
public static void main(String[] args) {
15+
int nums[] = {-95,92,-85,59,-59,-14,88,-39,2,92,94,79,78,-58,37,48,63,-91,91,74,-28,39,90,-9,-72,-88,-72,93,38,14,-83,-2,21,4,-75,-65,3,63,100,59,-48,43,35,-49,48,-36,-64,-13,-7,-29,87,34,56,-39,-5,-27,-28,10,-57,100,-43,-98,19,-59,78,-28,-91,67,41,-64,76,5,-58,-89,83,26,-7,-82,-32,-76,86,52,-6,84,20,51,-86,26,46,35,-23,30,-51,54,19,30,27,80,45,22};
16+
int k = 10;
17+
int[] res = maxSlidingWindow(nums,k);
18+
for (int i = 0; i < res.length ; i++) {
19+
System.out.print(res[i]);
20+
System.out.print(',');
21+
}
22+
}
23+
public static int[] maxSlidingWindow(int[] nums, int k) {
24+
if(nums.length==0)
25+
return new int[]{};
26+
int[] res = new int[nums.length-k+1];
27+
int cur = 0;
28+
Deque<Integer> dq = new ArrayDeque();
29+
for (int i = 0; i < nums.length ; i++) {
30+
if( !dq.isEmpty() && dq.peekFirst()<=i-k)
31+
dq.removeFirst();
32+
while( !dq.isEmpty() && nums[dq.peekLast()]<=nums[i]){// removeLast 不是 First。 自己写的时候这写错了,如果是First的话,有些Case也能过
33+
dq.removeLast();
34+
}
35+
dq.addLast(i);
36+
if(i>=k-1){
37+
res[cur] = nums[dq.peekFirst()];
38+
cur++;
39+
}
40+
}
41+
return res;
42+
}
43+
}

0 commit comments

Comments
 (0)