From 997fe87b204c5601f7afb7108d7f01c719bf513a Mon Sep 17 00:00:00 2001 From: Qiu Xiaoye Date: Sun, 5 Apr 2020 21:32:10 +0800 Subject: [PATCH 1/5] week4_0102(java) --- Week_03/.DS_Store | Bin 0 -> 6148 bytes Week_04/.DS_Store | Bin 0 -> 6148 bytes ...4\275\263\346\227\266\346\234\272-ii.java" | 20 +++++++++ ...\345\261\277\346\225\260\351\207\217.java" | 40 ++++++++++++++++++ ...\345\272\217\346\225\260\347\273\204.java" | 32 ++++++++++++++ ...\345\233\240\345\217\230\345\214\226.java" | 14 ++++++ ...\346\260\264\346\211\276\351\233\266.java" | 22 ++++++++++ 7 files changed, 128 insertions(+) create mode 100644 Week_03/.DS_Store create mode 100644 Week_04/.DS_Store create mode 100644 "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" create mode 100644 "Week_04/G20200447010102/200.\345\262\233\345\261\277\346\225\260\351\207\217.java" create mode 100644 "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" create mode 100644 "Week_04/G20200447010102/433.\346\234\200\345\260\217\345\237\272\345\233\240\345\217\230\345\214\226.java" create mode 100644 "Week_04/G20200447010102/860.\346\237\240\346\252\254\346\260\264\346\211\276\351\233\266.java" diff --git a/Week_03/.DS_Store b/Week_03/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..24b47487d05df4bbfa5d6748b6e3838a65d14487 GIT binary patch literal 6148 zcmeHKQA+|r5Z+a*)d+nE@^N2!Gw{?BAA-Y-=q_S<9kmNCZbqqfCZjxi=c5ff=Bz7T9jU6O?HAad15kri15 zo+=>8DlmX+mt`lc1S^L6xqcSds3mVa?0db?gUC0p-}8r2vG~pt$<*5Vh7cK%*~;8F z-EizgZZsO!-N6;s&fKW$8T&j8dvn|MF8t28ncF@JgUI!R&Ojym?GA)oU;05i9M{89 z&>pIO9Ni#9TBMt~{A9AXUn|IxRht&%TZo+XGRr0;28aP-V8t0Q+nU&1@p;he69dG+?=yh=g9Jsi z4aOSP)&T|nK4Nbhn36eLmnMg2 utpsg>qF`LCah?K(I*K6{kK#P267Wkj0BwV@M(}{pkAR|q3S!_-8TbGLzEE%g literal 0 HcmV?d00001 diff --git a/Week_04/.DS_Store b/Week_04/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1742131be1f0e1be104ea938fd0f54338da4a3bc GIT binary patch literal 6148 zcmeHKF=_)r43uIQh7@s`a-R?zL)yhSSdO|d$enXmWn3$wYI&EP~kau^%m(?@n!5eLGt#}l3vo@d~7M(%^X+7b-@hFA9>1k+bT;6NC7Dz1*Cu!xS#-QFKl+7s8I??0V!}&fcHa#6MNy17*_{| zXaRszgu^h8UIN${0QSNm5fPXt6_`}77Q>T{c&of#I3y2YOq^hl>k7KQE@HaSfe&96BgMuN- kF)+$87A(h)k(7CjbG+|`Lt@Yo4?0jk1Jp$(1^!!sUx20)Bme*a literal 0 HcmV?d00001 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 + From d568d257668dd30f1dd64afa06edbb3fc7c8e634 Mon Sep 17 00:00:00 2001 From: Qiu Xiaoye Date: Sun, 19 Apr 2020 21:51:27 +0800 Subject: [PATCH 2/5] =?UTF-8?q?0102=5FWeek06=EF=BC=88java=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\346\255\243\346\226\271\345\275\242.java" | 14 ++++++++ ...\350\260\203\345\272\246\345\231\250.java" | 34 +++++++++++++++++++ ...\350\267\257\345\276\204\345\222\214.java" | 34 +++++++++++++++++++ ...\347\240\201\346\226\271\346\263\225.java" | 28 +++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 "Week_06/G20200447010102/221.\346\234\200\345\244\247\346\255\243\346\226\271\345\275\242.java" create mode 100644 "Week_06/G20200447010102/621.\344\273\273\345\212\241\350\260\203\345\272\246\345\231\250.java" create mode 100644 "Week_06/G20200447010102/64.\346\234\200\345\260\217\350\267\257\345\276\204\345\222\214.java" create mode 100644 "Week_06/G20200447010102/91.\350\247\243\347\240\201\346\226\271\346\263\225.java" 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 + From 4c6fd25175d50414346d27a906046ba5fcbd7255 Mon Sep 17 00:00:00 2001 From: Qiu Xiaoye Date: Sun, 26 Apr 2020 21:23:26 +0800 Subject: [PATCH 3/5] =?UTF-8?q?0102=5FWeek07=EF=BC=88java=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\350\257\215\346\216\245\351\276\231.java" | 50 ++++++++++++++++ ...\346\234\213\345\217\213\345\234\210.java" | 29 +++++++++ ...\347\210\254\346\245\274\346\242\257.java" | 59 +++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 "Week_07/G20200343040102/127.\345\215\225\350\257\215\346\216\245\351\276\231.java" create mode 100644 "Week_07/G20200343040102/547.\346\234\213\345\217\213\345\234\210.java" create mode 100644 "Week_07/G20200343040102/70.\347\210\254\346\245\274\346\242\257.java" 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 Date: Sun, 3 May 2020 20:07:11 +0800 Subject: [PATCH 4/5] =?UTF-8?q?0102=5FWeek08=EF=BC=88java=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\345\255\230\346\234\272\345\210\266.java" | 73 +++++++++++++++++++ ...\345\271\266\345\214\272\351\227\264.java" | 27 +++++++ 2 files changed, 100 insertions(+) create mode 100644 "Week_08/G20200447010102/146.lru\347\274\223\345\255\230\346\234\272\345\210\266.java" create mode 100644 "Week_08/G20200447010102/56.\345\220\210\345\271\266\345\214\272\351\227\264.java" diff --git "a/Week_08/G20200447010102/146.lru\347\274\223\345\255\230\346\234\272\345\210\266.java" "b/Week_08/G20200447010102/146.lru\347\274\223\345\255\230\346\234\272\345\210\266.java" new file mode 100644 index 00000000..47a40c45 --- /dev/null +++ "b/Week_08/G20200447010102/146.lru\347\274\223\345\255\230\346\234\272\345\210\266.java" @@ -0,0 +1,73 @@ +/* + * @lc app=leetcode.cn id=146 lang=java + * + * [146] LRU缓存机制 + */ + +// @lc code=start +class LRUCache { + + Node head = new Node(0, 0), tail = new Node(0, 0); + Map 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 + From f73ca1040633560741df5e453ba6f461fb9a35a4 Mon Sep 17 00:00:00 2001 From: Qiu Xiaoye Date: Sun, 10 May 2020 20:44:44 +0800 Subject: [PATCH 5/5] homework --- ...\345\274\202\344\275\215\350\257\215.java" | 28 ++++++++++++++++ ...\346\225\260\345\217\215\350\275\254.java" | 20 +++++++++++ ...215\242\346\225\264\346\225\260-atoi.java" | 33 +++++++++++++++++++ ...\347\240\201\346\226\271\346\263\225.java" | 28 ++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 "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" create mode 100644 "Week_09/G20200447010102/7.\346\225\264\346\225\260\345\217\215\350\275\254.java" create mode 100644 "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" create mode 100644 "Week_09/G20200447010102/91.\350\247\243\347\240\201\346\226\271\346\263\225.java" 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 +