Skip to content

第二周作业#108 #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Week_02/id_12/LeetCode_1_012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
int[] result = new int[2];
int temp;
for (int i = 0; i < nums.length; i++) {
temp = nums[i];
if (map.containsKey(temp)) {
if (map.get(temp) != i) {
result[0] = i;
result[1] = map.get(temp);
}
} else {
map.put(target - temp, i);
}
}
return result;
}
}
9 changes: 9 additions & 0 deletions Week_02/id_12/LeetCode_242_012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public boolean isAnagram(String s, String t) {
int[] alphabet = new int[26];
for(char c : s.toCharArray()) alphabet[c - 'a']++;
for(char c : t.toCharArray()) alphabet[c - 'a']--;
for (int a : alphabet) if(a != 0) return false;
return true;
}
}
22 changes: 22 additions & 0 deletions Week_02/id_12/LeetCode_3_012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int lengthOfLongestSubstring(String s) {
Deque<Character> characterDeque = new LinkedList<>();
int result = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (characterDeque.contains(c)) {
int len = characterDeque.size();
if (len > result) {
result = len;
}
while (c != characterDeque.peek()) {
characterDeque.poll();
}
characterDeque.poll();
}
characterDeque.add(c);
}
int size = characterDeque.size();
return size > result ? size : result;
}
}
1 change: 0 additions & 1 deletion Week_02/id_12/NOTE.md

This file was deleted.

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


class Solution {

int[] x = {1, 0, -1, 0};
int[] y = {0, -1, 0, 1};

public int numIslands(char[][] grid) {
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
if (grid[i][j] == '1') {
count++;
sink(grid, i, j);
}
}
}
return count;
}

private void sink(char[][] grid, int i, int j) {
if (i < 0 || j < 0 || i > grid.length - 1 || j > grid[i].length - 1 || grid[i][j] == '0') {
return;
}
grid[i][j] = '0';
for (int index = 0; index < 4; index++) {
sink(grid, i + x[index], j + y[index]);
}
}
}
42 changes: 42 additions & 0 deletions Week_03/id_12/LeetCode_210_012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution {

public int[] findOrder(int numCourses, int[][] prerequisites) {
List[] course = new List[numCourses];
int[] map = new int[numCourses];
List<Integer> resultList = new ArrayList<Integer>();
for (int i = 0; i < numCourses; i++) {
course[i] = new ArrayList<Integer>();
}
for (int i = 0; i < prerequisites.length; i++) {
course[prerequisites[i][0]].add(prerequisites[i][1]);
}
for (int i = 0; i < numCourses; i++) {
if (!dfs(course, i, resultList, map)) {
return new int[0];
}
}
int[] an = new int[resultList.size()];
for (int i = 0; i < resultList.size(); i++) {
an[i] = resultList.get(i);
}
return an;
}

private boolean dfs(List[] course, int req, List<Integer> list, int[] map) {
if (map[req] == 0) {
map[req] = 1;
for (int i = 0; i < course[req].size(); i++) {
if (!dfs(course, (int) course[req].get(i), list, map)) {
return false;
}
}
map[req] = 2;
} else if (map[req] == 1) {
return false;
} else if (map[req] == 2) {
return true;
}
list.add(req);
return true;
}
}
21 changes: 21 additions & 0 deletions Week_03/id_12/LeetCode_295_012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class MedianFinder {

PriorityQueue<Integer> min = new PriorityQueue();
PriorityQueue<Integer> max = new PriorityQueue(1000, Collections.reverseOrder());

public void addNum(int num) {
max.offer(num);
min.offer(max.poll());
if (max.size() < min.size()) {
max.offer(min.poll());
}
}

public double findMedian() {
if (max.size() == min.size()) {
return (max.peek() + min.peek()) / 2.0;
} else {
return max.peek();
}
}
};
23 changes: 23 additions & 0 deletions Week_03/id_12/LeetCode_703_012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class KthLargest {

final PriorityQueue<Integer> q;
final int k;

public KthLargest(int k, int[] a) {
this.k = k;
q = new PriorityQueue<>(k);
for (int n : a) {
add(n);
}
}

public int add(int n) {
if (q.size() < k) {
q.offer(n);
} else if (q.peek() < n) {
q.poll();
q.offer(n);
}
return q.peek();
}
}
1 change: 0 additions & 1 deletion Week_03/id_12/NOTE.md

This file was deleted.

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