diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..26d33521a --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/algorithm010.iml b/.idea/algorithm010.iml new file mode 100644 index 000000000..d0876a78d --- /dev/null +++ b/.idea/algorithm010.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 000000000..105ce2da2 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..8fa8e3bb9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..39a5bed6d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..94a25f7f4 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Summary.md b/Summary.md new file mode 100644 index 000000000..301c1c033 --- /dev/null +++ b/Summary.md @@ -0,0 +1,4 @@ +本次算法训练营收获颇多,了解了数组、链表、栈、队列、堆、树、图、hash表这类数据结构,以及它们的实现原理与代码; +还了解了递归、分治、回溯、贪心、动态规划、BFS、DFS、二分查找这类算法; +学习了字典树、并查集以及树的一些特殊结构。 +虽然到现在为止,题目才刷了几十道,但是因为覃超老师的五毒神掌刷题法,效果很不错,希望能在接下来的时间里继续努力刷题,争取校招找到一份满意的工作。 \ No newline at end of file diff --git a/Week04/122.py b/Week04/122.py new file mode 100644 index 000000000..a4b9237c1 --- /dev/null +++ b/Week04/122.py @@ -0,0 +1,8 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + profit = 0 + for i in range(0, len(prices) - 1): + temp = prices[i + 1] - prices[i] + if temp > 0: + profit = profit + temp + return profit diff --git a/Week04/860.py b/Week04/860.py new file mode 100644 index 000000000..304a7f9d6 --- /dev/null +++ b/Week04/860.py @@ -0,0 +1,20 @@ +# 860. 柠檬水找零 +class Solution: + def lemonadeChange(self, bills) -> bool: + five = ten = 0 + for money in bills: + if money == 5: + five += 1 + elif money == 10: + five -= 1 + ten += 1 + else: + if ten == 0: + five -= 3 + else: + ten -= 1 + five -= 1 + if five < 0 or ten < 0: + return False + return True + diff --git a/Week06/1143.py b/Week06/1143.py new file mode 100644 index 000000000..2dffcd183 --- /dev/null +++ b/Week06/1143.py @@ -0,0 +1,13 @@ +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + if not text1 or not text2: + return 0 + m, n = len(text1), len(text2) + dp = [[0] * (n + 1) for _ in range(m + 1)] + for i in range(1, m + 1): + for j in range(1, n + 1): + if text1[i - 1] == text2[j - 1]: + dp[i][j] = dp[i - 1][j - 1] + 1 + else: + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + return dp[m][n] diff --git a/Week06/62.py b/Week06/62.py new file mode 100644 index 000000000..4453929f5 --- /dev/null +++ b/Week06/62.py @@ -0,0 +1,11 @@ +class Solution: + def uniquePaths(self, m: int, n: int) -> int: + if not m or not n: + return 0 + + dp = [[1]*n] + [[1]+[0] * (n-1) for _ in range(m-1)] + + for i in range(1, m): + for j in range(1, n): + dp[i][j] = dp[i-1][j] + dp[i][j-1] + return dp[-1][-1] diff --git a/Week06/64.py b/Week06/64.py new file mode 100644 index 000000000..7f1a08234 --- /dev/null +++ b/Week06/64.py @@ -0,0 +1,14 @@ +class Solution: + def minPathSum(self, grid: List[List[int]]) -> int: + dp = grid + for i in range(len(grid)): + for j in range(len(grid[0])): + if i == j == 0: + continue + elif i == 0: + dp[i][j] = dp[i][j-1] + grid[i][j] + elif j == 0: + dp[i][j] = dp[i-1][j] + grid[i][j] + else: + dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j] + return dp[-1][-1] diff --git a/Week06/91.py b/Week06/91.py new file mode 100644 index 000000000..825037f9e --- /dev/null +++ b/Week06/91.py @@ -0,0 +1,21 @@ +def numDecodings(s: str) -> int: + pre = 1 + cur = 1 + if s[0] == "0": + return 0 + for i in range(1, len(s)): + if s[i] == "0": + if s[i - 1] == "1" or s[i - 1] == "2": + cur = pre + else: + return 0 + else: + if s[i - 1] == "1" or (s[i - 1] == "2" and "1" <= s[i] <= "6"): + temp = cur + cur += pre + pre = temp + else: + pre = cur + cur = cur + + return cur diff --git a/Week06/NOTE.md b/Week06/NOTE.md index 50de30414..9b4988afb 100644 --- a/Week06/NOTE.md +++ b/Week06/NOTE.md @@ -1 +1,5 @@ -学习笔记 \ No newline at end of file +学习笔记 +动态规划 +1.最优子结构 +2.储存中间状态 +3.DP方程 \ No newline at end of file diff --git a/Week07/200.py b/Week07/200.py new file mode 100644 index 000000000..cd03fea74 --- /dev/null +++ b/Week07/200.py @@ -0,0 +1,21 @@ +class Solution: + def dfs(self, grid, r, c): + grid[r][c] = "0" + nr = len(grid) + nl = len(grid[0]) + for x, y in [(r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)]: + if 0 <= x < nr and 0 <= y < nl and grid[x][y] == "1": + self.dfs(grid, x, y) + + def numIslands(self, grid: List[List[str]]) -> int: + nr = len(grid) + if nr == 0: + return 0 + nl = len(grid[0]) + num_island = 0 + for r in range(nr): + for l in range(nl): + if grid[r][l] == "1": + num_island += 1 + self.dfs(grid, r, l) + return num_island diff --git a/Week07/208.py b/Week07/208.py new file mode 100644 index 000000000..acf8aae2f --- /dev/null +++ b/Week07/208.py @@ -0,0 +1,39 @@ +class Trie: + + def __init__(self): + """ + Initialize your data structure here. + """ + self.root = {} + self.end_of_word = "#" + + def insert(self, word: str) -> None: + """ + Inserts a word into the trie. + """ + node = self.root + for char in word: + node = node.setdefault(char, {}) + node[self.end_of_word] = self.end_of_word + + def search(self, word: str) -> bool: + """ + Returns if the word is in the trie. + """ + node = self.root + for char in word: + if char not in node: + return False + node = node[char] + return self.end_of_word in node + + def startsWith(self, prefix: str) -> bool: + """ + Returns if there is any word in the trie that starts with the given prefix. + """ + node = self.root + for char in prefix: + if char not in node: + return False + node = node[char] + return True diff --git a/Week07/433.py b/Week07/433.py new file mode 100644 index 000000000..057ac32b6 --- /dev/null +++ b/Week07/433.py @@ -0,0 +1,25 @@ +class Solution: + def minMutation(self, start: str, end: str, bank: List[str]) -> int: + bank = set(bank) + if end not in bank: + return -1 + change_map = { + "A": "CGT", + "C": "AGT", + "G": "ACT", + "T": "ACG" + } + queue = [(start, 0)] + while queue: + node, step = queue.pop(0) + + if node == end: + return step + + for i, s in enumerate(node): + for c in change_map[s]: + new = node[:i] + c + node[i + 1:] + if new in bank: + queue.append((new, step + 1)) + bank.remove(new) + return -1 diff --git a/Week08/1122.py b/Week08/1122.py new file mode 100644 index 000000000..f0513284d --- /dev/null +++ b/Week08/1122.py @@ -0,0 +1,15 @@ +class Solution: + def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]: + dic = {} + not_arry = [] + for i in range(len(arr1)): + dic[arr1[i]] = dic.get(arr1[i], 0) + 1 + if arr1[i] not in arr2: + not_arry.append(arr1[i]) + not_arry.sort() + + new_arr1 = [] + arr1 = set(arr1) + for j in range(len(arr2)): + new_arr1 += [arr2[j]] * dic[arr2[j]] + return new_arr1 + not_arry diff --git a/Week08/146.py b/Week08/146.py new file mode 100644 index 000000000..e69de29bb diff --git a/Week08/191.py b/Week08/191.py new file mode 100644 index 000000000..667846dec --- /dev/null +++ b/Week08/191.py @@ -0,0 +1,7 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + res = 0 + while(n!=0): + n &= n-1 + res+=1 + return res diff --git a/Week08/242.py b/Week08/242.py new file mode 100644 index 000000000..9b83ddfb7 --- /dev/null +++ b/Week08/242.py @@ -0,0 +1,4 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): return False + return sorted(s) == sorted(t) diff --git a/Week08/338.py b/Week08/338.py new file mode 100644 index 000000000..92e4a9591 --- /dev/null +++ b/Week08/338.py @@ -0,0 +1,9 @@ +class Solution: + def countBits(self, num: int) -> List[int]: + dp = [0] * (num + 1) + for i in range(1, num+1): + if i%2 == 1: + dp[i] = dp[i-1] +1 + else: + dp[i] = dp[i//2] + return dp diff --git a/Week09/125.py b/Week09/125.py new file mode 100644 index 000000000..a3e172fdb --- /dev/null +++ b/Week09/125.py @@ -0,0 +1,4 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + res = "".join(ch.lower() for ch in s if ch.isalnum()) + return res == res[::-1] \ No newline at end of file diff --git a/Week09/14.py b/Week09/14.py new file mode 100644 index 000000000..81abe6a9e --- /dev/null +++ b/Week09/14.py @@ -0,0 +1,40 @@ +class Solution1: + def longestCommonPrefix(self, strs: List[str]) -> str: + if not strs: + return "" + s = len(strs) + if s == 1: + return strs[0] + n = len(min(strs)) + string = "" + flag = True + for j in range(n): + if flag: + for l in range(1, s): + if strs[l][j] == strs[l-1][j]: + if l == s-1: + string += strs[l][j] + else: + flag = False + break + return string + +class Solution2: + def longestCommonPrefix(self, strs: List[str]) -> str: + s = "" + for i in zip(*strs): + if len(set(i)) == 1: + s += i[0] + else: + break + return s + +class Solution3: + def longestCommonPrefix(self, strs: List[str]) -> str: + if not strs: return "" + str0 = min(strs) + str1 = max(strs) + for i in range(len(str0)): + if str0[i] != str1[i]: + return str0[:i] + return str0 diff --git a/Week09/344.py b/Week09/344.py new file mode 100644 index 000000000..ea49da454 --- /dev/null +++ b/Week09/344.py @@ -0,0 +1,17 @@ +class Solution1: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + n = len(s) + mid = n // 2 + for i in range(mid): + s[i], s[n-i-1] = s[n-i-1], s[i] + return s + +class Solution1: + def reverseString(self, s: List[str]) -> None: + """ + Do not return anything, modify s in-place instead. + """ + return s.reverse() \ No newline at end of file diff --git a/Week09/541.py b/Week09/541.py new file mode 100644 index 000000000..82f364e4b --- /dev/null +++ b/Week09/541.py @@ -0,0 +1,9 @@ +class Solution: + def reverseStr(self, s: str, k: int) -> str: + if not s: + return s + a = list(s) + n = len(s) + for i in range(0, n, 2*k): + a[i:i+k] = reversed(a[i:i+k]) + return "".join(a) diff --git a/Week09/58.py b/Week09/58.py new file mode 100644 index 000000000..69c1d395f --- /dev/null +++ b/Week09/58.py @@ -0,0 +1,21 @@ +class Solution1: + def lengthOfLastWord(self, s: str) -> int: + string = s.split() + return len(string[-1]) if string else 0 + +class Solution2: + def lengthOfLastWord(self, s: str) -> int: + if not s and len(s) == 0: + return 0 + count = 0 + for i in s[::-1]: + if i == " ": + if count == 0: + continue + else: + break + else: + count += 1 + + return count + diff --git a/Week09/680.py b/Week09/680.py new file mode 100644 index 000000000..2e0b507ae --- /dev/null +++ b/Week09/680.py @@ -0,0 +1,19 @@ +class Solution: + def validPalindrome(self, s: str) -> bool: + def check(low, high): + i, j = low, high + while i < j: + if s[i] != s[j]: + return False + i += 1 + j -= 1 + return True + + low, high = 0, len(s)-1 + while low < high: + if s[low] == s[high]: + low += 1 + high -= 1 + else: + return check(low+1, high) or check(low, high-1) + return True diff --git a/Week09/709.py b/Week09/709.py new file mode 100644 index 000000000..d82fde047 --- /dev/null +++ b/Week09/709.py @@ -0,0 +1,14 @@ +class Solution1: + def toLowerCase(self, str: str) -> str: + dic = {'A':'a', 'B':'b', 'C':'c', 'D':'d', 'E':'e', 'F':'f', + 'G':'g', 'H':'h', 'I':'i', 'J':'j', 'K':'k', 'L':'l', + 'M':'m', 'N':'n', 'O':'o','P':'p', 'Q':'q', 'R':'r', + 'S':'s', 'T':'t', 'U':'u', 'V':'v', 'W':'w', 'X':'x', + 'Y':'y', 'Z':'z'} + + return "".join([dic.get(s, s) for s in str]) + + +class Solution2: + def toLowerCase(self, str: str) -> str: + return str.lower() \ No newline at end of file diff --git a/Week09/746.py b/Week09/746.py new file mode 100644 index 000000000..847e2c747 --- /dev/null +++ b/Week09/746.py @@ -0,0 +1,5 @@ +class Solution: + def minCostClimbingStairs(self, cost: List[int]) -> int: + for i in range(2, len(cost)): + cost[i] = min(cost[i-1], cost[i-2]) + cost[i] + return min(cost[-1], cost[-2])