Skip to content

Commit a1837cc

Browse files
author
anduo
committed
amazon
1 parent 7d78f70 commit a1837cc

30 files changed

+610
-69
lines changed

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,11 @@ doc中有好几份文档,比较经典,可以参考。
180180
- 题解 [WordLadder.java](./src/learn/freq05/WordLadder.java)
181181

182182
## frequency4
183+
184+
185+
# 刷题总结
186+
187+
## chapter02 binary search & sorted array
188+
这一部分都可以使用bs模板来解决问题 [二分搜索模板](./src/template/bs_template.md)
189+
190+
- [question01:first position of target](./src/ninechapter/ch02_binary_search_and_sorted_array/Question01FirstPositionOfTarget.java) || [lintcode](http://www.lintcode.com/zh-cn/problem/first-position-of-target/)

src/amazon/AddTwoNumbers.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package amazon;
2+
3+
import entity.ListNode;
4+
// You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
5+
6+
// You may assume the two numbers do not contain any leading zero, except the number 0 itself.
7+
8+
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
9+
// Output: 7 -> 0 -> 8
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* public class ListNode {
14+
* int val;
15+
* ListNode next;
16+
* ListNode(int x) { val = x; }
17+
* }
18+
*/
19+
20+
/**
21+
* leetcode 2 -> https://leetcode.com/problems/add-two-numbers
22+
* 扩展:https://leetcode.com/problems/add-two-numbers-ii/
23+
*/
24+
25+
public class AddTwoNumbers {
26+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
27+
ListNode dummy = new ListNode(0);// 哨兵节点,用来做返回值的头结点使用。
28+
ListNode tail = dummy; // 用一个tail指针来操作dummy的下一个节点
29+
int sum = 0; // 每次遍历两个链表的和
30+
while (l1 != null || l2 != null) {
31+
sum /= 10; // 每次进来之后都取上次sum的十位
32+
if (l1 != null) {
33+
sum += l1.val;
34+
l1 = l1.next;
35+
}
36+
if (l2 != null) {
37+
sum += l2.val;
38+
l2 = l2.next;
39+
}
40+
tail.next = new ListNode(sum % 10);// 尾插新节点,新节点的值为sum的个位数,十位进一
41+
tail = tail.next;
42+
}
43+
if (sum / 10 == 1) {
44+
tail.next = new ListNode(1);
45+
}
46+
47+
return dummy.next;
48+
}
49+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package amazon;
2+
// Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
3+
4+
// For example:
5+
// Given binary tree [3,9,20,null,null,15,7],
6+
// 3
7+
// / \
8+
// 9 20
9+
// / \
10+
// 15 7
11+
// return its level order traversal as:
12+
// [
13+
// [3],
14+
// [9,20],
15+
// [15,7]
16+
// ]
17+
18+
/**
19+
* Definition for a binary tree node.
20+
* public class TreeNode {
21+
* int val;
22+
* TreeNode left;
23+
* TreeNode right;
24+
* TreeNode(int x) { val = x; }
25+
* }
26+
*/
27+
28+
import entity.TreeNode;
29+
30+
import java.util.ArrayList;
31+
import java.util.LinkedList;
32+
import java.util.List;
33+
import java.util.Queue;
34+
35+
/**
36+
* leetcode 102 https://leetcode.com/problems/binary-tree-level-order-traversal
37+
* 二叉树的层次遍历,其实也就是图的宽度优先遍历,记住使用数据结构Queue
38+
* 两层循环:
39+
* 外层:while 队列不为空
40+
* 内层:while 队列元素大小的所有元素全部出队,打印该层元素;把下层元素入队
41+
*
42+
* 扩展练习:https://leetcode.com/problems/binary-tree-level-order-traversal-ii
43+
* Created by anduo on 17-3-13.
44+
*/
45+
public class BinaryTreeLevelOrderTraversal {
46+
public List<List<Integer>> levelOrder(TreeNode root) {
47+
List<List<Integer>> result = new ArrayList<>();
48+
if (root == null) return result;
49+
// bfs 算法
50+
Queue<TreeNode> queue = new LinkedList<>();
51+
queue.add(root);
52+
53+
TreeNode node;
54+
while (!queue.isEmpty()) {
55+
List<Integer> levels = new ArrayList<>();
56+
int nums = queue.size();
57+
while (nums-- > 0) {
58+
// 把该层的所有节点放到levels中,并且把这层的下级节点依次放入队列中
59+
node = queue.poll();
60+
levels.add(node.val);
61+
if (node.left != null) {
62+
queue.add(node.left);
63+
}
64+
if (node.right != null) {
65+
queue.add(node.right);
66+
}
67+
}
68+
// 把该层节点的list加到result中
69+
result.add(levels);
70+
}
71+
return result;
72+
}
73+
}

src/amazon/MergeKSortedLists.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package amazon;
2+
3+
import entity.ListNode;
4+
5+
import java.util.Comparator;
6+
import java.util.PriorityQueue;
7+
/**
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode(int x) { val = x; }
13+
* }
14+
*/
15+
/**
16+
* Created by anduo on 17-3-13.
17+
*/
18+
public class MergeKSortedLists {
19+
public ListNode mergetKLists(ListNode[] lists) {
20+
if (lists == null || lists.length == 0) return null;
21+
22+
PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, new Comparator<ListNode>() {
23+
@Override
24+
public int compare(ListNode o1, ListNode o2) {
25+
if (o1.val < o2.val) {
26+
return -1;
27+
} else if (o1.val == o2.val) {
28+
return 0;
29+
} else {
30+
return 1;
31+
}
32+
}
33+
});
34+
35+
ListNode dummy = new ListNode(0);
36+
ListNode head = dummy;
37+
38+
for (ListNode node : lists) {
39+
if (node != null) {
40+
queue.add(node);
41+
}
42+
}
43+
44+
while (!queue.isEmpty()) {
45+
head.next = queue.poll();
46+
head = head.next;
47+
48+
if (head.next != null) {
49+
queue.add(head.next);
50+
}
51+
}
52+
53+
return dummy.next;
54+
}
55+
}

src/amazon/PalindromeLinkedList.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package amazon;
2+
3+
import entity.ListNode;
4+
/**
5+
* https://leetcode.com/problems/palindrome-linked-list/
6+
* 判断链表是不是回文
7+
* 做法,快慢指针就可以了。
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode(int x) { val = x; }
13+
* }
14+
*/
15+
16+
/**
17+
* Created by anduo on 17-3-13.
18+
*/
19+
public class PalindromeLinkedList {
20+
public boolean isPalindrome(ListNode head) {
21+
if (head == null || head.next == null) return true;
22+
23+
ListNode slow = head, fast = head.next;
24+
25+
// 找到中间的节点
26+
while (fast != null && fast.next != null) {
27+
slow = slow.next;
28+
fast = fast.next.next;
29+
}
30+
if (fast != null) {// 奇数的情况
31+
slow = slow.next;
32+
}
33+
34+
// 翻转后边的节点
35+
ListNode reverse = reverse(slow);
36+
while (head != null && reverse != null && head.val == reverse.val) {
37+
head = head.next;
38+
reverse = reverse.next;
39+
}
40+
return reverse == null;
41+
}
42+
43+
private ListNode reverse(ListNode head) {
44+
ListNode now = null;
45+
while (head != null) {
46+
ListNode next = head.next;
47+
head.next = now;
48+
now = head;
49+
head = next;
50+
}
51+
return now;
52+
}
53+
}

src/amazon/ReverseLinkedList.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package amazon;
2+
3+
import entity.ListNode;
4+
5+
// Reverse a singly linked list.
6+
7+
/**
8+
* 直接尾插法
9+
* Created by anduo on 17-3-13.
10+
*/
11+
public class ReverseLinkedList {
12+
public ListNode reverseList(ListNode head) {
13+
if (head == null) return null;
14+
ListNode now = null;
15+
while (head != null) {
16+
ListNode next = head.next;// 保存下一个节点
17+
head.next = now; // 断开原链表的头节点,并把next指向上一个节点
18+
now = head;
19+
head = next;
20+
}
21+
return now;
22+
}
23+
}

src/amazon/RotateImage.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package amazon;
2+
// You are given an n x n 2D matrix representing an image.
3+
4+
// Rotate the image by 90 degrees (clockwise).
5+
6+
// Follow up:
7+
// Could you do this in-place?
8+
9+
/**
10+
* 旋转图像
11+
* <p>
12+
* 1 2 3 1 4 7 7 4 1
13+
* 4 5 6 ----正对角线翻转---> 2 5 8 ----竖对角线翻转---> 8 5 2
14+
* 7 8 9 3 6 9 9 6 3
15+
* Created by anduo on 17-3-13.
16+
*/
17+
public class RotateImage {
18+
public void rotate(int[][] matrix) {
19+
if (matrix == null || matrix.length == 0) {
20+
return;
21+
}
22+
int rows = matrix.length;
23+
int cols = matrix[0].length;
24+
25+
// 正对角线翻转
26+
for (int i = 0; i < rows; i++) {
27+
for (int j = 0; j < i; j++) {
28+
// swap
29+
// a ^= b; b ^= a; a ^= b;
30+
matrix[i][j] ^= matrix[j][i];
31+
matrix[j][i] ^= matrix[i][j];
32+
matrix[i][j] ^= matrix[j][i];
33+
}
34+
}
35+
36+
// 竖对角线翻转
37+
for (int i = 0; i < rows; i++) {
38+
for (int j = 0; j < cols / 2; j++) {
39+
matrix[i][j] ^= matrix[i][cols - j - 1];
40+
matrix[i][cols - j - 1] ^= matrix[i][j];
41+
matrix[i][j] ^= matrix[i][cols - j - 1];
42+
}
43+
}
44+
}
45+
}

src/amazon/ThreeSum.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package amazon;
2+
3+
4+
// Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
5+
6+
// Note: The solution set must not contain duplicate triplets.
7+
8+
// For example, given array S = [-1, 0, 1, 2, -1, -4],
9+
10+
// A solution set is:
11+
// [
12+
// [-1, 0, 1],
13+
// [-1, -1, 2]
14+
// ]
15+
16+
import java.util.ArrayList;
17+
import java.util.Arrays;
18+
import java.util.List;
19+
20+
/**
21+
* Created by anduo on 17-3-13.
22+
*/
23+
public class ThreeSum {
24+
25+
// 把3sum的问题,降级到2sum问题
26+
public List<List<Integer>> threeSum(int[] nums) {
27+
List<List<Integer>> result = new ArrayList<>();
28+
Arrays.sort(nums);
29+
30+
for (int i = 0; i < nums.length; i++) {
31+
if (i > 0 && nums[i] == nums[i - 1]) {// 这道题用不到这个条件,因为题目说了不考虑重复元素
32+
continue;
33+
}
34+
int left = i + 1;
35+
int right = nums.length - 1;
36+
int target = -nums[i];
37+
twoSum(nums, left, right, target, result);
38+
}
39+
return result;
40+
}
41+
42+
public void twoSum(int[] nums,
43+
int left,
44+
int right,
45+
int target,
46+
List<List<Integer>> results) {
47+
// 充分利用有序这样一个特点
48+
while (left < right) {
49+
if (nums[left] + nums[right] == target) {
50+
ArrayList<Integer> triple = new ArrayList<>();
51+
triple.add(-target);
52+
triple.add(nums[left]);
53+
triple.add(nums[right]);
54+
results.add(triple);
55+
56+
left++;
57+
right--;
58+
// skip duplicate pairs with the same left
59+
while (left < right && nums[left] == nums[left - 1]) {
60+
left++;
61+
}
62+
// skip duplicate pairs with the same right
63+
while (left < right && nums[right] == nums[right + 1]) {
64+
right--;
65+
}
66+
} else if (nums[left] + nums[right] < target) {
67+
left++;
68+
} else {
69+
right--;
70+
}
71+
}
72+
}
73+
74+
}

0 commit comments

Comments
 (0)