Skip to content

Commit caad7fd

Browse files
Merge pull request #108 from Shockang/master
第二周作业#108
2 parents 4534a73 + 6a4204a commit caad7fd

11 files changed

+199
-2
lines changed

Week_02/id_12/LeetCode_1_012.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
int[] result = new int[2];
5+
int temp;
6+
for (int i = 0; i < nums.length; i++) {
7+
temp = nums[i];
8+
if (map.containsKey(temp)) {
9+
if (map.get(temp) != i) {
10+
result[0] = i;
11+
result[1] = map.get(temp);
12+
}
13+
} else {
14+
map.put(target - temp, i);
15+
}
16+
}
17+
return result;
18+
}
19+
}

Week_02/id_12/LeetCode_242_012.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public boolean isAnagram(String s, String t) {
3+
int[] alphabet = new int[26];
4+
for(char c : s.toCharArray()) alphabet[c - 'a']++;
5+
for(char c : t.toCharArray()) alphabet[c - 'a']--;
6+
for (int a : alphabet) if(a != 0) return false;
7+
return true;
8+
}
9+
}

Week_02/id_12/LeetCode_3_012.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int lengthOfLongestSubstring(String s) {
3+
Deque<Character> characterDeque = new LinkedList<>();
4+
int result = 0;
5+
for (int i = 0; i < s.length(); i++) {
6+
char c = s.charAt(i);
7+
if (characterDeque.contains(c)) {
8+
int len = characterDeque.size();
9+
if (len > result) {
10+
result = len;
11+
}
12+
while (c != characterDeque.peek()) {
13+
characterDeque.poll();
14+
}
15+
characterDeque.poll();
16+
}
17+
characterDeque.add(c);
18+
}
19+
int size = characterDeque.size();
20+
return size > result ? size : result;
21+
}
22+
}

Week_02/id_12/NOTE.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

Week_02/id_12/学习总结.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 学习笔记
2+
这周由于比较忙我只做了3题,但是3题我都认真做了,总结了一下经验:
3+
1.第1题,刚开始确实想不出来这里面存在一个映射关系,可以利用到hashmap,学到了,以后碰到类似的题目,一定到先考虑有没有一对一映射的可能性。
4+
2.第3题,刚开始我就想到了这里可以利用一个双端队列来实现,因为从头到尾遍历的过程中,头和尾都会出队列,之后我又对我的代码优化了几次,感觉受益匪浅,起码在双端队列这一块学到了很多。
5+
3.第242题,这一题我刚开始的想法是可以排序后比较,直到我看到了leetcode英文版上面的讨论区学到了这个方法,利用26个长度的数组来判断,真的比较巧妙,从这里我学到了,以后一定要注意题目当中的关键字,比如只包含小写字母,这就是一个很明显的暗示,可以立马联想到这个题目。
6+
算法训练是一个长期的过程,这周我只完成了3道题目,感觉很惭愧,下周我会挤出更多的时间来完成算法训练。

Week_03/id_12/LeetCode_200_012.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
2+
//
3+
// Example 1:
4+
//
5+
//
6+
//Input:
7+
//11110
8+
//11010
9+
//11000
10+
//00000
11+
//
12+
//Output: 1
13+
//
14+
//
15+
// Example 2:
16+
//
17+
//
18+
//Input:
19+
//11000
20+
//11000
21+
//00100
22+
//00011
23+
//
24+
//Output: 3
25+
//
26+
27+
28+
class Solution {
29+
30+
int[] x = {1, 0, -1, 0};
31+
int[] y = {0, -1, 0, 1};
32+
33+
public int numIslands(char[][] grid) {
34+
int count = 0;
35+
for (int i = 0; i < grid.length; i++) {
36+
for (int j = 0; j < grid[i].length; j++) {
37+
if (grid[i][j] == '1') {
38+
count++;
39+
sink(grid, i, j);
40+
}
41+
}
42+
}
43+
return count;
44+
}
45+
46+
private void sink(char[][] grid, int i, int j) {
47+
if (i < 0 || j < 0 || i > grid.length - 1 || j > grid[i].length - 1 || grid[i][j] == '0') {
48+
return;
49+
}
50+
grid[i][j] = '0';
51+
for (int index = 0; index < 4; index++) {
52+
sink(grid, i + x[index], j + y[index]);
53+
}
54+
}
55+
}

Week_03/id_12/LeetCode_210_012.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
3+
public int[] findOrder(int numCourses, int[][] prerequisites) {
4+
List[] course = new List[numCourses];
5+
int[] map = new int[numCourses];
6+
List<Integer> resultList = new ArrayList<Integer>();
7+
for (int i = 0; i < numCourses; i++) {
8+
course[i] = new ArrayList<Integer>();
9+
}
10+
for (int i = 0; i < prerequisites.length; i++) {
11+
course[prerequisites[i][0]].add(prerequisites[i][1]);
12+
}
13+
for (int i = 0; i < numCourses; i++) {
14+
if (!dfs(course, i, resultList, map)) {
15+
return new int[0];
16+
}
17+
}
18+
int[] an = new int[resultList.size()];
19+
for (int i = 0; i < resultList.size(); i++) {
20+
an[i] = resultList.get(i);
21+
}
22+
return an;
23+
}
24+
25+
private boolean dfs(List[] course, int req, List<Integer> list, int[] map) {
26+
if (map[req] == 0) {
27+
map[req] = 1;
28+
for (int i = 0; i < course[req].size(); i++) {
29+
if (!dfs(course, (int) course[req].get(i), list, map)) {
30+
return false;
31+
}
32+
}
33+
map[req] = 2;
34+
} else if (map[req] == 1) {
35+
return false;
36+
} else if (map[req] == 2) {
37+
return true;
38+
}
39+
list.add(req);
40+
return true;
41+
}
42+
}

Week_03/id_12/LeetCode_295_012.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class MedianFinder {
2+
3+
PriorityQueue<Integer> min = new PriorityQueue();
4+
PriorityQueue<Integer> max = new PriorityQueue(1000, Collections.reverseOrder());
5+
6+
public void addNum(int num) {
7+
max.offer(num);
8+
min.offer(max.poll());
9+
if (max.size() < min.size()) {
10+
max.offer(min.poll());
11+
}
12+
}
13+
14+
public double findMedian() {
15+
if (max.size() == min.size()) {
16+
return (max.peek() + min.peek()) / 2.0;
17+
} else {
18+
return max.peek();
19+
}
20+
}
21+
};

Week_03/id_12/LeetCode_703_012.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class KthLargest {
2+
3+
final PriorityQueue<Integer> q;
4+
final int k;
5+
6+
public KthLargest(int k, int[] a) {
7+
this.k = k;
8+
q = new PriorityQueue<>(k);
9+
for (int n : a) {
10+
add(n);
11+
}
12+
}
13+
14+
public int add(int n) {
15+
if (q.size() < k) {
16+
q.offer(n);
17+
} else if (q.peek() < n) {
18+
q.poll();
19+
q.offer(n);
20+
}
21+
return q.peek();
22+
}
23+
}

Week_03/id_12/NOTE.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

Week_03/id_12/学习总结.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 学习笔记
2+
首先感觉作业题目越来越难了,第200题老师上课讲过了,对小岛沉没这种解法很有印象,也安装老师要求的用两个数组来表示方向,算是一次课堂温习;第210题,刚开始题目没有看清楚,不清楚第一个参数的作业,提醒自己以后审题一定要仔细,感觉这题的数据结构很像Java中的HashMap,可以借鉴着来理解;第295题感觉用优先队列来解决太巧妙了,学到了一招;第703题算是对优先队列的又一次灵活应用。

0 commit comments

Comments
 (0)