diff --git a/Week_03/.DS_Store b/Week_03/.DS_Store new file mode 100644 index 00000000..24b47487 Binary files /dev/null and b/Week_03/.DS_Store differ diff --git a/Week_04/.DS_Store b/Week_04/.DS_Store new file mode 100644 index 00000000..1742131b Binary files /dev/null and b/Week_04/.DS_Store differ diff --git "a/Week_04/G20200447010102/122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272-ii.java" "b/Week_04/G20200447010102/122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272-ii.java" new file mode 100644 index 00000000..f62aa480 --- /dev/null +++ "b/Week_04/G20200447010102/122.\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272-ii.java" @@ -0,0 +1,20 @@ +/* + * @lc app=leetcode.cn id=122 lang=java + * + * [122] 买卖股票的最佳时机 II + */ + +// @lc code=start +class Solution { + public int maxProfit(int[] prices) { + int result = 0; + for (int i = 0; i < prices.length - 1; i++) { + if (prices[i + 1] > prices[i]) { + result += prices[i + 1] - prices[i]; + } + } + return result; + } +} +// @lc code=end + diff --git "a/Week_04/G20200447010102/200.\345\262\233\345\261\277\346\225\260\351\207\217.java" "b/Week_04/G20200447010102/200.\345\262\233\345\261\277\346\225\260\351\207\217.java" new file mode 100644 index 00000000..1481dc32 --- /dev/null +++ "b/Week_04/G20200447010102/200.\345\262\233\345\261\277\346\225\260\351\207\217.java" @@ -0,0 +1,40 @@ +/* + * @lc app=leetcode.cn id=200 lang=java + * + * [200] 岛屿数量 + */ + +// @lc code=start +class Solution { + + private int m; + private int n; + + public int numIslands(char[][] grid) { + int result = 0; + n = grid.length; + if (n == 0) return 0; + m = grid[0].length; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == '1') { + islandMarking(grid, i, j); + ++result; + } + } + } + return result; + } + + private void islandMarking(char[][] gird, int i, int j) { + if (i < 0 || j < 0 || i >= n || j >= m || gird[i][j] != '1') return; + gird[i][j] = '0'; + islandMarking(gird, i + 1, j); + islandMarking(gird, i - 1, j); + islandMarking(gird, i, j + 1); + islandMarking(gird, i, j - 1); + } +} +// @lc code=end + diff --git "a/Week_04/G20200447010102/33.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204.java" "b/Week_04/G20200447010102/33.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204.java" new file mode 100644 index 00000000..ba7c4d8f --- /dev/null +++ "b/Week_04/G20200447010102/33.\346\220\234\347\264\242\346\227\213\350\275\254\346\216\222\345\272\217\346\225\260\347\273\204.java" @@ -0,0 +1,32 @@ +/* + * @lc app=leetcode.cn id=33 lang=java + * + * [33] 搜索旋转排序数组 + */ + +// @lc code=start +class Solution { + public int search(int[] nums, int target) { + int left = 0; + int right = nums.length - 1; + while (left < right) { + int mid = (left + right) / 2; + if (nums[mid] > nums[right]) left = mid + 1; + else right = mid; + } + + int rot = left; + left = 0; + right = nums.length - 1; + while (left <= right) { + int mid = (left + right) / 2; + int realMid = (mid + rot) % nums.length; + if (nums[realMid] == target) return realMid; + if (nums[realMid] < target) left = mid + 1; + else right = mid -1; + } + return -1; + } +} +// @lc code=end + diff --git "a/Week_04/G20200447010102/433.\346\234\200\345\260\217\345\237\272\345\233\240\345\217\230\345\214\226.java" "b/Week_04/G20200447010102/433.\346\234\200\345\260\217\345\237\272\345\233\240\345\217\230\345\214\226.java" new file mode 100644 index 00000000..2d46ab07 --- /dev/null +++ "b/Week_04/G20200447010102/433.\346\234\200\345\260\217\345\237\272\345\233\240\345\217\230\345\214\226.java" @@ -0,0 +1,14 @@ +/* + * @lc app=leetcode.cn id=433 lang=java + * + * [433] 最小基因变化 + */ + +// @lc code=start +class Solution { + public int minMutation(String start, String end, String[] bank) { + + } +} +// @lc code=end + diff --git "a/Week_04/G20200447010102/860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.java" "b/Week_04/G20200447010102/860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.java" new file mode 100644 index 00000000..88310510 --- /dev/null +++ "b/Week_04/G20200447010102/860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.java" @@ -0,0 +1,22 @@ +/* + * @lc app=leetcode.cn id=860 lang=java + * + * [860] 柠檬水找零 + */ + +// @lc code=start +class Solution { + public boolean lemonadeChange(int[] bills) { + int five = 0, ten = 0; + for (int i : bills) { + if (i == 5) five++; + else if (i == 10) { five--; ten++; } + else if (ten > 0) { ten--; five--; } + else five -= 3; + if (five < 0) return false; + } + return true; + } +} +// @lc code=end + diff --git "a/Week_06/G20200447010102/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.java" "b/Week_06/G20200447010102/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.java" new file mode 100644 index 00000000..05d38700 --- /dev/null +++ "b/Week_06/G20200447010102/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.java" @@ -0,0 +1,14 @@ +/* + * @lc app=leetcode.cn id=221 lang=java + * + * [221] 最大正方形 + */ + +// @lc code=start +class Solution { + public int maximalSquare(char[][] matrix) { + + } +} +// @lc code=end + diff --git "a/Week_06/G20200447010102/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250.java" "b/Week_06/G20200447010102/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250.java" new file mode 100644 index 00000000..005ca54a --- /dev/null +++ "b/Week_06/G20200447010102/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250.java" @@ -0,0 +1,34 @@ +/* + * @lc app=leetcode.cn id=621 lang=java + * + * [621] 任务调度器 + */ + +// @lc code=start +class Solution { + public int leastInterval(char[] tasks, int n) { + int[] counter = new int[26]; + int max = 0; + int maxCount = 0; + for(char task : tasks) { + counter[task - 'A']++; + if(max == counter[task - 'A']) { + maxCount++; + } + else if(max < counter[task - 'A']) { + max = counter[task - 'A']; + maxCount = 1; + } + } + + int partCount = max - 1; + int partLength = n - (maxCount - 1); + int emptySlots = partCount * partLength; + int availableTasks = tasks.length - max * maxCount; + int idles = Math.max(0, emptySlots - availableTasks); + + return tasks.length + idles; + } +} +// @lc code=end + diff --git "a/Week_06/G20200447010102/64.\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.java" "b/Week_06/G20200447010102/64.\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.java" new file mode 100644 index 00000000..c25901dd --- /dev/null +++ "b/Week_06/G20200447010102/64.\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.java" @@ -0,0 +1,34 @@ +/* + * @lc app=leetcode.cn id=64 lang=java + * + * [64] 最小路径和 + */ + +// @lc code=start +class Solution { + public int minPathSum(int[][] grid) { + if (grid == null || grid.length == 0) return 0; + + int m = grid[0].length; + int n = grid.length; + int[][] dp = new int[n][m]; + for (int i = n - 1; i >= 0; i--) { + for (int j = m -1; j >= 0; j--) { + if (i == n - 1 && j != m - 1) { + dp[i][j] = grid[i][j] + dp[i][j + 1]; + } + else if (i != n - 1 && j == m - 1) { + dp[i][j] = grid[i][j] + dp[i + 1][j]; + } + else if (i != n - 1 && j != m - 1) { + dp[i][j] = grid[i][j] + Math.min(dp[i + 1][j], dp[i][j + 1]); + } else { + dp[i][j] = grid[i][j]; + } + } + } + return dp[0][0]; + } +} +// @lc code=end + diff --git "a/Week_06/G20200447010102/91.\350\247\243\347\240\201\346\226\271\346\263\225.java" "b/Week_06/G20200447010102/91.\350\247\243\347\240\201\346\226\271\346\263\225.java" new file mode 100644 index 00000000..6dc73d73 --- /dev/null +++ "b/Week_06/G20200447010102/91.\350\247\243\347\240\201\346\226\271\346\263\225.java" @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode.cn id=91 lang=java + * + * [91] 解码方法 + */ + +// @lc code=start +class Solution { + public int numDecodings(String s) { + if (s == null || s.length() == 0) return 0; + int n = s.length(); + int[] dp = new int[n]; + dp[0] = s.charAt(0) != '0' ? 1 : 0; + for (int i = 1; i < n; i++) { + int first = Integer.valueOf(s.substring(i, i + 1)); + int second = Integer.valueOf(s.substring(i - 1, i + 1)); + if (first >= 1 && first <= 9) { + dp[i] += dp[i - 1]; + } + if (second >= 10 && second <= 26) { + dp[i] += i >= 2 ? dp[i - 2] : 1; + } + } + return dp[n - 1]; + } +} +// @lc code=end + diff --git "a/Week_07/G20200343040102/127.\345\215\225\350\257\215\346\216\245\351\276\231.java" "b/Week_07/G20200343040102/127.\345\215\225\350\257\215\346\216\245\351\276\231.java" new file mode 100644 index 00000000..686bba7d --- /dev/null +++ "b/Week_07/G20200343040102/127.\345\215\225\350\257\215\346\216\245\351\276\231.java" @@ -0,0 +1,50 @@ +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/* + * @lc app=leetcode.cn id=127 lang=java + * + * [127] 单词接龙 + */ + +// @lc code=start +class Solution { + public int ladderLength(String beginWord, String endWord, List wordAsList) { + if(!wordAsList.contains(endWord)) return 0; + + Set wordList = new HashSet(wordAsList); + Set start = new HashSet(); + Set end = new HashSet(); + int length = 1; + start.add(beginWord); end.add(endWord); + wordList.remove(beginWord); wordList.remove(endWord); + + while(!start.isEmpty()){ + Set next = new HashSet(); + for(String word: start){ + char[] wordArray = word.toCharArray(); + for(int i=0; i map = new HashMap(); + int capacity; + + public LRUCache(int _capacity) { + capacity = _capacity; + head.next = tail; + tail.prev = head; + } + + public int get(int key) { + if(map.containsKey(key)) { + Node node = map.get(key); + remove(node); + insert(node); + return node.value; + } else { + return -1; + } + } + + public void put(int key, int value) { + if(map.containsKey(key)) { + remove(map.get(key)); + } + if(map.size() == capacity) { + remove(tail.prev); + } + insert(new Node(key, value)); + } + + private void remove(Node node) { + map.remove(node.key); + node.prev.next = node.next; + node.next.prev = node.prev; + } + + private void insert(Node node){ + map.put(node.key, node); + Node headNext = head.next; + head.next = node; + node.prev = head; + headNext.prev = node; + node.next = headNext; + } + + class Node{ + Node prev, next; + int key, value; + Node(int _key, int _value) { + key = _key; + value = _value; + } + } + } + +/** + * Your LRUCache object will be instantiated and called as such: + * LRUCache obj = new LRUCache(capacity); + * int param_1 = obj.get(key); + * obj.put(key,value); + */ +// @lc code=end + diff --git "a/Week_08/G20200447010102/56.\345\220\210\345\271\266\345\214\272\351\227\264.java" "b/Week_08/G20200447010102/56.\345\220\210\345\271\266\345\214\272\351\227\264.java" new file mode 100644 index 00000000..b0d9e560 --- /dev/null +++ "b/Week_08/G20200447010102/56.\345\220\210\345\271\266\345\214\272\351\227\264.java" @@ -0,0 +1,27 @@ +/* + * @lc app=leetcode.cn id=56 lang=java + * + * [56] 合并区间 + */ + +// @lc code=start +class Solution { + public int[][] merge(int[][] intervals) { + Arrays.sort(intervals, (a, b) -> a[0] - b[0]); + List ret = new ArrayList<>(); + int[] prev = null; + for (int[] inter : intervals) { + //if prev is null or curr.start > prev.end, add the interval + if (prev==null || inter[0] > prev[1]) { + ret.add(inter); + prev = inter; + } else if (inter[1] > prev[1]) { + // curr.end > prev.end, modify the element already in list + prev[1] = inter[1]; + } + } + return ret.toArray(new int[ret.size()][2]); + } +} +// @lc code=end + diff --git "a/Week_09/G20200447010102/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.java" "b/Week_09/G20200447010102/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.java" new file mode 100644 index 00000000..c6899b96 --- /dev/null +++ "b/Week_09/G20200447010102/438.\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215.java" @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode.cn id=438 lang=java + * + * [438] 找到字符串中所有字母异位词 + */ + +// @lc code=start +class Solution { + public List findAnagrams(String s, String p) { + int[] freq = new int[256]; + for (int i = 0; i < p.length(); i++) freq[p.charAt(i)]++; + + List ret = new ArrayList<>(); + int diff = p.length(); + for (int i = 0, j = 0; i < s.length(); i++) { + if (freq[s.charAt(i)]-- > 0) diff--; + while (diff == 0) { + if (i - j + 1 == p.length()) { // Here is the key! + ret.add(j); + } + if (++freq[s.charAt(j++)] > 0) diff++; + } + } + return ret; + } +} +// @lc code=end + diff --git "a/Week_09/G20200447010102/7.\346\225\264\346\225\260\345\217\215\350\275\254.java" "b/Week_09/G20200447010102/7.\346\225\264\346\225\260\345\217\215\350\275\254.java" new file mode 100644 index 00000000..55bc30e9 --- /dev/null +++ "b/Week_09/G20200447010102/7.\346\225\264\346\225\260\345\217\215\350\275\254.java" @@ -0,0 +1,20 @@ +/* + * @lc app=leetcode.cn id=7 lang=java + * + * [7] 整数反转 + */ + +// @lc code=start +class Solution { + public int reverse(int x) { + long res = 0; + while (x != 0) { + res *= 10; + res += x % 10; + x /= 10; + } + return (int)res == res ? (int)res : 0; + } +} +// @lc code=end + diff --git "a/Week_09/G20200447010102/8.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260-atoi.java" "b/Week_09/G20200447010102/8.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260-atoi.java" new file mode 100644 index 00000000..037d857d --- /dev/null +++ "b/Week_09/G20200447010102/8.\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\225\264\346\225\260-atoi.java" @@ -0,0 +1,33 @@ +/* + * @lc app=leetcode.cn id=8 lang=java + * + * [8] 字符串转换整数 (atoi) + */ + +// @lc code=start +class Solution { + public int myAtoi(String str) { + str = str.trim(); + if (str.length() == 0) return 0; + if (!Character.isDigit(str.charAt(0)) + && str.charAt(0) != '-' && str.charAt(0) != '+') + return 0; + long ans = 0L; + boolean neg = str.charAt(0) == '-'; + int i = !Character.isDigit(str.charAt(0)) ? 1 : 0; + while (i < str.length() && Character.isDigit(str.charAt(i))) { + ans = ans * 10 + (str.charAt(i++) - '0'); + if (!neg && ans > Integer.MAX_VALUE) { + ans = Integer.MAX_VALUE; + break; + } + if (neg && ans > 1L + Integer.MAX_VALUE) { + ans = 1L + Integer.MAX_VALUE; + break; + } + } + return neg ? (int) -ans : (int) ans; + } +} +// @lc code=end + diff --git "a/Week_09/G20200447010102/91.\350\247\243\347\240\201\346\226\271\346\263\225.java" "b/Week_09/G20200447010102/91.\350\247\243\347\240\201\346\226\271\346\263\225.java" new file mode 100644 index 00000000..6dc73d73 --- /dev/null +++ "b/Week_09/G20200447010102/91.\350\247\243\347\240\201\346\226\271\346\263\225.java" @@ -0,0 +1,28 @@ +/* + * @lc app=leetcode.cn id=91 lang=java + * + * [91] 解码方法 + */ + +// @lc code=start +class Solution { + public int numDecodings(String s) { + if (s == null || s.length() == 0) return 0; + int n = s.length(); + int[] dp = new int[n]; + dp[0] = s.charAt(0) != '0' ? 1 : 0; + for (int i = 1; i < n; i++) { + int first = Integer.valueOf(s.substring(i, i + 1)); + int second = Integer.valueOf(s.substring(i - 1, i + 1)); + if (first >= 1 && first <= 9) { + dp[i] += dp[i - 1]; + } + if (second >= 10 && second <= 26) { + dp[i] += i >= 2 ? dp[i - 2] : 1; + } + } + return dp[n - 1]; + } +} +// @lc code=end +