diff options
Diffstat (limited to '')
-rwxr-xr-x | 1239/main.py | 40 | ||||
-rw-r--r-- | 124/main.go | 1 | ||||
-rw-r--r-- | 14/main.go | 40 | ||||
-rw-r--r-- | 20/main.py | 47 | ||||
-rwxr-xr-x | 2610/2125/main.py | 26 | ||||
-rw-r--r-- | 872/main.go | 1 | ||||
-rw-r--r-- | 876/main.cpp | 2 | ||||
-rw-r--r-- | 886/main.go | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | 931/main.go | 4 | ||||
-rw-r--r-- | 938/872/go.mod | 3 | ||||
-rw-r--r-- | 938/872/main.go | 74 |
11 files changed, 131 insertions, 109 deletions
diff --git a/1239/main.py b/1239/main.py new file mode 100755 index 0000000..0ef5ef1 --- /dev/null +++ b/1239/main.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + + +import typing + + +class Solution: + def maxLength(self, arr: typing.List[str]) -> int: + result = [0] + self.dfs(arr, "", 0, result) + return result[0] + + def dfs(self, arr, path, idx, result): + if self.isUniqueChars(path): + result[0] = max(result[0], len(path)) + + if idx == len(arr) or not self.isUniqueChars(path): + return + + for i in range(idx, len(arr)): + self.dfs(arr, path + arr[i], i + 1, result) + + def isUniqueChars(self, s): + char_set = set() + for c in s: + if c in char_set: + return False + char_set.add(c) + return True + + +def main(): + solution = Solution() + print(solution.maxLength(["un", "iq", "ue"])) + print(solution.maxLength(["cha", "r", "act", "ers"])) + print(solution.maxLength(["abcdefghijklmnopqrstuvwxyz"])) + + +if __name__ == "__main__": + main() diff --git a/124/main.go b/124/main.go index fa953f1..73a515a 100644 --- a/124/main.go +++ b/124/main.go @@ -26,6 +26,7 @@ func walk(node *TreeNode, sum *int) int { left := max(0, walk(node.Left, sum)) right := max(0, walk(node.Right, sum)) + fmt.Println(left, right, sum) *sum = max(*sum, left+right+node.Val) return max(left, right) + node.Val diff --git a/14/main.go b/14/main.go new file mode 100644 index 0000000..f22304a --- /dev/null +++ b/14/main.go @@ -0,0 +1,40 @@ +package main + +import "fmt" + +func longestCommonPrefix(strs []string) string { + longestPrefix := strs[0] + for _, element := range strs { + for index, char := range element { + if len(longestPrefix) == 0 { + break + } + if index > len(longestPrefix)-1 { + // longestPrefix = longestPrefix[0 : index-1] + // fmt.Println("XXX:", longestPrefix) + break + } + if byte(char) != longestPrefix[index] { + longestPrefix = longestPrefix[0:index] + } + } + + if len(longestPrefix) > len(element) { + longestPrefix = longestPrefix[0:len(element)] + } + // fmt.Println(longestPrefix) + } + + return longestPrefix +} + +func main() { + strs := []string{"flower", "flow", "flight"} + fmt.Println(longestCommonPrefix(strs)) + strs2 := []string{"dog", "racecar", "car"} + fmt.Println(longestCommonPrefix(strs2)) + strs3 := []string{"ab", "a"} + fmt.Println(longestCommonPrefix(strs3)) + strs4 := []string{"aaa", "aa", "aaa"} + fmt.Println(longestCommonPrefix(strs4)) +} diff --git a/20/main.py b/20/main.py new file mode 100644 index 0000000..b3c5136 --- /dev/null +++ b/20/main.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + + +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + for char in s: + if char == "(": + stack.append("(") + elif char == "[": + stack.append("[") + elif char == "{": + stack.append("{") + elif char == ")": + if len(stack) == 0: + return False + if stack[-1] == "(": + stack.pop() + else: + return False + elif char == "]": + if len(stack) == 0: + return False + if stack[-1] == "[": + stack.pop() + else: + return False + elif char == "}": + if len(stack) == 0: + return False + if stack[-1] == "{": + stack.pop() + else: + return False + + if len(stack) != 0: + return False + + return True + + +def main() -> None: + solution = Solution() + + +if __name__ == "__main__": + main() diff --git a/2610/2125/main.py b/2610/2125/main.py deleted file mode 100755 index da45c29..0000000 --- a/2610/2125/main.py +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env python - -import typing - - -class Solution: - def numberOfBeams(self, bank: typing.List[str]) -> int: - row_count = 0 - result = 0 - for row in bank: - count = row.count("1") - if count != 0: - result += row_count * count - row_count = row.count("1") - - return result - - -def main(): - solution = Solution() - print(solution.numberOfBeams(["011001", "000000", "010100", "00100"])) - print(solution.numberOfBeams(["000", "111", "000"])) - - -if __name__ == "__main__": - main() diff --git a/872/main.go b/872/main.go index 24ef0ab..d71a335 100644 --- a/872/main.go +++ b/872/main.go @@ -70,5 +70,4 @@ func main() { node4.Right = &node5 fmt.Println(leafSimilar(&node1, &node4)) - } diff --git a/876/main.cpp b/876/main.cpp index 20d49ad..8bee31f 100644 --- a/876/main.cpp +++ b/876/main.cpp @@ -22,7 +22,7 @@ public: heads.push_back(head); int size = heads.size(); - std::cout << "size:" << size << "\n"; + /* std::cout << "size:" << size << "\n"; */ return heads[int(size / 2)]; } }; diff --git a/886/main.go b/886/main.go index d06b3b8..8979d71 100644 --- a/886/main.go +++ b/886/main.go @@ -60,7 +60,7 @@ func possibleBipartition(n int, dislikes [][]int) bool { return color[u] == c } - //fmt.Printf("node: %d will mark color: %d\n", u, c) + fmt.Printf("node: %d will mark color: %d\n", u, c) color[u] = c for _, v := range edges[u] { if !dfs(v, -c) { diff --git a/931/main.go b/931/main.go index b05fb8e..f6df355 100644..100755 --- a/931/main.go +++ b/931/main.go @@ -51,15 +51,13 @@ func dumpMatrix(matrix [][]int) { func minFallingPathSum(matrix [][]int) int { for i := 1; i < len(matrix[0]); i++ { for j := 0; j < len(matrix[0]); j++ { - // matrix[i][j] += min(matrix[i-1][j], min(matrix[i-1][max(0, j-1)], matrix[i-1][min(len(matrix[0])-1, j+1)])) matrix[i][j] += min3(matrix[i-1][j], matrix[i-1][max(0, j-1)], matrix[i-1][min(len(matrix[0])-1, j+1)]) } } - // dumpMatrix(matrix) + dumpMatrix(matrix) return getRowMin(matrix[len(matrix[0])-1]) - } func main() { diff --git a/938/872/go.mod b/938/872/go.mod deleted file mode 100644 index 10fbec9..0000000 --- a/938/872/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module 872 - -go 1.18 diff --git a/938/872/main.go b/938/872/main.go deleted file mode 100644 index 24ef0ab..0000000 --- a/938/872/main.go +++ /dev/null @@ -1,74 +0,0 @@ -package main - -import "fmt" - -type TreeNode struct { - Val int - Left *TreeNode - Right *TreeNode -} - -func preorderVisit(node *TreeNode, leafSequence *[]int) { - if node == nil { - return - } - if node.Left == nil && node.Right == nil { - *leafSequence = append(*leafSequence, node.Val) - return - } - - preorderVisit(node.Left, leafSequence) - preorderVisit(node.Right, leafSequence) -} - -func leafSimilar(root1, root2 *TreeNode) bool { - var leafSequence1 []int - var leafSequence2 []int - - preorderVisit(root1, &leafSequence1) - preorderVisit(root2, &leafSequence2) - - // for i := 0; i < len(leafSequence1); i++ { - // fmt.Println((leafSequence1)[i]) - // } - // for i := 0; i < len(leafSequence2); i++ { - // fmt.Println((leafSequence2)[i]) - // } - - if len(leafSequence1) != len(leafSequence2) { - return false - } - - for i := 0; i < len(leafSequence1); i++ { - if (leafSequence1)[i] != (leafSequence2)[i] { - return false - } - } - - return true -} - -func main() { - var node1 TreeNode - var node2 TreeNode - var node3 TreeNode - node1.Val = 1 - node2.Val = 2 - node3.Val = 3 - - node1.Left = &node2 - node1.Right = &node3 - - var node4 TreeNode - var node5 TreeNode - var node6 TreeNode - node4.Val = 1 - node5.Val = 2 - node6.Val = 3 - - node4.Left = &node6 - node4.Right = &node5 - - fmt.Println(leafSimilar(&node1, &node4)) - -} |