├── 202008 ├── 14 │ ├── BenLampson │ │ └── Solution.cs │ ├── CrazyLiuxp │ │ └── Program.cs │ ├── Ivysaur │ │ └── Program.cs │ ├── Jackyye │ │ └── Program.cs │ ├── Lemon │ │ └── Solution.java │ ├── Markxu │ │ └── StackCode.cs │ ├── Murph │ │ ├── Solution.cs │ │ └── Solution.go │ ├── README.md │ ├── Richeir │ │ └── Solution.go │ ├── Sky-shang │ │ └── Solution.cs │ ├── Turbo │ │ └── Solution.cs │ ├── Zhuangkh │ │ └── Solution.cs │ ├── wxj │ │ └── 20200814.java │ ├── 明洋 │ │ └── Solution.cs │ └── 麦田的香 │ │ └── Solution.cs ├── 16 │ ├── BenLampson │ │ └── Solution.cs │ ├── CrazyLiuxp │ │ └── Program.cs │ ├── Ivysaur │ │ └── Program.cs │ ├── Murph │ │ └── Solution.cs │ ├── README.md │ ├── Richeir │ │ └── leetcode733.go │ ├── Sky-shang │ │ └── Solution.cs │ ├── Zhuangkh │ │ └── Solution.cs │ ├── wxj │ │ └── Solutionwxj.java │ ├── 明洋 │ │ └── Solution.cs │ └── 麦田的香 │ │ └── Solution.cs ├── 17 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── Murph │ │ └── Solution.cs │ ├── README.md │ ├── Richeir │ │ └── 20200817_110.go │ ├── Sky-shang │ │ └── Solution.cs │ ├── Zhuangkh │ │ └── Solution.cs │ ├── wxj │ │ └── SolutionWxj.java │ └── 明洋 │ │ └── Solution.cs ├── 18 │ ├── CrazyLiuxp │ │ ├── Program.cs │ │ └── Solution.cs │ ├── Murph │ │ ├── Solution.cs │ │ └── Solution.go │ ├── README.md │ ├── Sky-shang │ │ └── Solution.go │ ├── wxj │ │ └── SolutionWxj.java │ └── 明洋 │ │ └── Solution.cs ├── 19 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── README.md │ ├── Richeir │ │ └── 20200819_647.go │ └── Sky-shang │ │ └── Solution.go ├── 20 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── README.md │ ├── Sky-shang │ │ └── Solution.go │ └── Zhuangkh │ │ └── Solution.cs ├── 21 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── Murph │ │ ├── Solution.cs │ │ └── Solution.go │ ├── README.md │ ├── Richeir │ │ └── 20200821_111.go │ ├── Sky-shang │ │ └── Solution.go │ ├── Zhuangkh │ │ └── Solution.cs │ └── 明洋 │ │ └── Solution.cs ├── 22 │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 23 │ ├── README.md │ ├── Richeir │ │ └── 20200823_201.go │ └── Sky-shang │ │ └── Solution.go ├── 24 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── Murph │ │ └── Solution.cs │ ├── README.md │ ├── Richeir │ │ └── 20200824_459.go │ ├── Sky-shang │ │ └── Solution.go │ ├── Zhuangkh │ │ └── Solution.cs │ └── 明洋 │ │ └── Solution.cs ├── 25 │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 26 │ ├── Murph │ │ └── Solution.go │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 27 │ ├── Murph │ │ └── Solution.go │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 28 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── Murph │ │ └── Solution.go │ ├── README.md │ ├── Richeir │ │ └── 20200828_657.go │ ├── Sky-shang │ │ └── Solution.go │ ├── Zhuangkh │ │ └── Solution.cs │ └── 明洋 │ │ └── Solution.cs ├── 29 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 30 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── Murph │ │ └── Solution.go │ ├── README.md │ ├── Richeir │ │ └── 20200830_557.go │ ├── Sky-shang │ │ └── Solution.go │ └── Zhuangkh │ │ └── Solution.cs └── 31 │ ├── CrazyLiuxp │ └── Solution.cs │ ├── Murph │ └── Solution.go │ ├── README.md │ ├── Sky-shang │ └── Solution.go │ └── Zhuangkh │ └── Solution.cs ├── 202009 ├── 10 │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 11 │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 12 │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 13 │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 14 │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 16 │ ├── Murph │ │ └── Solution.go │ └── READMD.md ├── 19 │ ├── Murph │ │ └── Solution.go │ └── README.md ├── 01 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── Murph │ │ └── Solution.go │ ├── README.md │ ├── Sky-shang │ │ └── Solution.go │ └── Zhuangkh │ │ └── Solution.cs ├── 02 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── README.md │ ├── Sky-shang │ │ └── Solution.go │ └── Zhuangkh │ │ └── Solution.cs ├── 03 │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 04 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 05 │ ├── CrazyLiuxp │ │ └── Solution.cs │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 06 │ ├── README.md │ ├── Sky-shang │ │ └── Solution.go │ └── Zhuangkh │ │ └── Solution.cs ├── 07 │ ├── Murph │ │ └── Solution.go │ ├── README.md │ └── Sky-shang │ │ └── Solution.go ├── 08 │ ├── Murph │ │ └── Solution.go │ ├── README.md │ └── Sky-shang │ │ └── Solution.go └── 09 │ ├── README.md │ └── Sky-shang │ └── Solution.go ├── .github └── workflows │ └── automerge.yml └── README.md /.github/workflows/automerge.yml: -------------------------------------------------------------------------------- 1 | name: automerge 2 | on: 3 | schedule: 4 | - cron: "*/15 * * * *" 5 | pull_request_review: 6 | types: 7 | - submitted 8 | check_suite: 9 | types: 10 | - completed 11 | status: {} 12 | jobs: 13 | automerge: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - name: automerge 17 | uses: pascalgn/automerge-action@v0.9.0 18 | if: github.repository_owner == 'lemon-book' 19 | with: 20 | args: "--trace" 21 | env: 22 | GITHUB_TOKEN: "${{ secrets.MERGE_TOKEN }}" 23 | MERGE_METHOD: "squash" 24 | MERGE_LABELS: "" 25 | MERGE_COMMIT_MESSAGE: "Auto Merge" 26 | -------------------------------------------------------------------------------- /202008/14/BenLampson/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsValid(string s) { 3 | Stack charList = new Stack(); 4 | foreach (var item in s) 5 | { 6 | if (charList.Count==0) 7 | { 8 | charList.Push(item); 9 | } 10 | else 11 | { 12 | switch (charList.Peek()) 13 | { 14 | case '[': 15 | if (item==']') 16 | { 17 | charList.Pop(); 18 | } 19 | else 20 | { 21 | charList.Push(item); 22 | } 23 | break; 24 | case '{': 25 | if (item == '}') 26 | { 27 | charList.Pop(); 28 | } 29 | else 30 | { 31 | charList.Push(item); 32 | } 33 | break; 34 | case '(': 35 | if (item == ')') 36 | { 37 | charList.Pop(); 38 | } 39 | else 40 | { 41 | charList.Push(item); 42 | } 43 | break; 44 | } 45 | } 46 | 47 | } 48 | return charList.Count == 0; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /202008/14/CrazyLiuxp/Program.cs: -------------------------------------------------------------------------------- 1 | class Program{ 2 | public static bool IsValid(string s) 3 | { 4 | if (string.IsNullOrEmpty(s)) return true; 5 | 6 | if (s.Length % 2 != 0) return false; 7 | 8 | Stack stack = new Stack(); 9 | foreach(char c in s) 10 | { 11 | if (stack.Count == 0) 12 | stack.Push(c); 13 | else if (c - stack.Peek() == 1 || c - stack.Peek() == 2) 14 | stack.Pop(); 15 | else 16 | stack.Push(c); 17 | } 18 | return !stack.Any(); 19 | } 20 | } -------------------------------------------------------------------------------- /202008/14/Ivysaur/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Problem20 { 5 | class Program { 6 | // 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 7 | // 有效字符串需满足: 8 | // 左括号必须用相同类型的右括号闭合。 9 | // 左括号必须以正确的顺序闭合。 10 | // 注意空字符串可被认为是有效字符串。 11 | 12 | // 示例 1: 13 | // 输入: "()" 14 | // 输出: true 15 | 16 | // 示例 2: 17 | // 输入: "()[]{}" 18 | // 输出: true 19 | 20 | // 示例 3: 21 | // 输入: "(]" 22 | // 输出: false 23 | 24 | // 示例 4: 25 | // 输入: "([)]" 26 | // 输出: false 27 | 28 | // 示例 5: 29 | // 输入: "{[]}" 30 | // 输出: true 31 | 32 | static void Main (string[] args) { 33 | Console.WriteLine (IsValid ("()")); 34 | } 35 | 36 | public static bool IsValid (string s) { 37 | 38 | if (s.Length % 2 == 1) { 39 | return false; 40 | } 41 | 42 | Stack stack = new Stack (); 43 | 44 | foreach (var item in s) { 45 | if (stack.Count == 0) { 46 | stack.Push (item); 47 | continue; 48 | } 49 | 50 | var topItem = stack.Peek (); 51 | 52 | switch (topItem) { 53 | case '(': 54 | if (item == ')') { 55 | stack.Pop (); 56 | } else { 57 | stack.Push (item); 58 | } 59 | break; 60 | case '[': 61 | if (item == ']') { 62 | stack.Pop (); 63 | } else { 64 | stack.Push (item); 65 | } 66 | break; 67 | case '{': 68 | if (item == '}') { 69 | stack.Pop (); 70 | } else { 71 | stack.Push (item); 72 | } 73 | break; 74 | case ')': 75 | return false; 76 | case ']': 77 | return false; 78 | case '}': 79 | return false; 80 | } 81 | 82 | } 83 | 84 | return stack.Count == 0; 85 | } 86 | 87 | } 88 | } -------------------------------------------------------------------------------- /202008/14/Jackyye/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Json 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | 10 | Console.WriteLine(Solution.IsValid("()")); 11 | Console.WriteLine(Solution.IsValid("((")); 12 | Console.WriteLine(Solution.IsValid("(([]){})")); 13 | Console.ReadLine(); 14 | } 15 | 16 | 17 | 18 | public static class Solution 19 | { 20 | public static bool IsValid(string s) 21 | { 22 | if (s.Length % 2 == 1) 23 | { 24 | return false; 25 | } 26 | var stacks = new System.Collections.Generic.Stack(); 27 | var hash = new System.Collections.Hashtable(); 28 | hash.Add(')', '('); 29 | hash.Add(']', '['); 30 | hash.Add('}', '{'); 31 | var chars = s.ToCharArray(); 32 | foreach (var c in chars) 33 | { 34 | if (hash.ContainsKey(c)) 35 | { 36 | if (stacks.Count==0 || (char)hash[c] != stacks.Peek()) 37 | { 38 | return false; 39 | } 40 | stacks.Pop(); 41 | } 42 | else 43 | { 44 | stacks.Push(c); 45 | } 46 | } 47 | if(stacks.Count != 0) 48 | { 49 | return false; 50 | } 51 | return true; 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /202008/14/Lemon/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isValid(String s) { 3 | if(s==null || s.length() == 0){ 4 | return true; 5 | } 6 | if(s.length() % 2 ==1){ 7 | return false; 8 | } 9 | Map map = new HashMap<>(); 10 | map.put('}', '{'); 11 | map.put(')', '('); 12 | map.put(']', '['); 13 | Stack stack = new Stack(); 14 | for(char c:s.toCharArray()){ 15 | if(map.containsKey(c)){ 16 | if(stack.isEmpty() || stack.pop()!= map.get(c)){ 17 | return false; 18 | } 19 | } else{ 20 | stack.push(c); 21 | } 22 | } 23 | 24 | return stack.isEmpty(); 25 | } 26 | } -------------------------------------------------------------------------------- /202008/14/Markxu/StackCode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace MyLeetCode 6 | { 7 | public class StackCode 8 | { 9 | public bool IsValid_2(string s) 10 | { 11 | if (s.Length % 2 == 1) 12 | { 13 | return false; 14 | } 15 | Dictionary map = new Dictionary 16 | { 17 | { ']', '[' }, 18 | { ')', '(' }, 19 | { '}', '{' } 20 | }; 21 | var stack = new Stack(); 22 | 23 | foreach (var item in s) 24 | { 25 | if (map.ContainsKey(item)) 26 | { 27 | if (stack.Count == 0) 28 | { 29 | return false; 30 | } 31 | else 32 | { 33 | var element = stack.Pop(); 34 | if (element != map[item]) 35 | { 36 | return false; 37 | } 38 | } 39 | } 40 | else 41 | { 42 | stack.Push(item); 43 | } 44 | } 45 | return stack.Count == 0; 46 | } 47 | 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /202008/14/Murph/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsValid (string s) { 3 | if (s.Length == 0) return true; 4 | if (s.Length % 2 == 1) return false; 5 | var dic = new Dictionary (); 6 | dic.Add (']', '['); 7 | dic.Add ('}', '{'); 8 | dic.Add (')', '('); 9 | var stack = new Stack (); 10 | for (var i = 0; i < s.Length; i++) { 11 | var temp = s[i]; 12 | 13 | if (dic.TryGetValue (temp, out var value)) { 14 | if (stack.Count == 0) return false; 15 | if (!Convert.ToChar (stack.Pop ()).Equals (value)) return false; 16 | if (stack.Count == 0) return true; 17 | } else { 18 | stack.Push (temp); 19 | } 20 | } 21 | return false; 22 | } 23 | } -------------------------------------------------------------------------------- /202008/14/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | func isValid(s string) bool { 2 | lennnn := len(s) 3 | if lennnn % 2 == 1 { 4 | return false 5 | } 6 | dic := map[byte]byte{ 7 | ')': '(', 8 | ']': '[', 9 | '}': '{', 10 | } 11 | stack := []byte{} 12 | for i := 0; i < lennnn; i++ { 13 | if dic[s[i]] > 0 { 14 | if len(stack) == 0 || stack[len(stack)-1] != dic[s[i]] { 15 | return false 16 | } 17 | stack = stack[:len(stack)-1] 18 | } else { 19 | stack = append(stack, s[i]) 20 | } 21 | } 22 | return len(stack) == 0 23 | } -------------------------------------------------------------------------------- /202008/14/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/valid-parentheses/ 3 | 4 | #### 题目描述 5 | 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 6 | 7 | 有效字符串需满足: 8 | 9 | 左括号必须用相同类型的右括号闭合。 10 | 左括号必须以正确的顺序闭合。 11 | 注意空字符串可被认为是有效字符串。 12 | 13 | 示例 1: 14 | ``` 15 | 输入: "()" 16 | 输出: true 17 | ``` 18 | 示例 2: 19 | ``` 20 | 输入: "()[]{}" 21 | 输出: true 22 | ``` 23 | 示例 3: 24 | ``` 25 | 输入: "(]" 26 | 输出: false 27 | ``` 28 | 示例 4: 29 | ``` 30 | 输入: "([)]" 31 | 输出: false 32 | ``` 33 | 示例 5: 34 | ``` 35 | 输入: "{[]}" 36 | 输出: true 37 | ``` 38 | 39 | ## 打卡 40 | - LEMON 41 | - Richeir 42 | - 麦田的香 43 | - CRAZYLIUXP 44 | - Zhuangkh 45 | - BenLampson 46 | - Murph 47 | - wxj 48 | - 明洋 49 | - Turbo 50 | - Sky-shang 51 | - Markxu 52 | - Ivysaur 53 | -------------------------------------------------------------------------------- /202008/14/Richeir/Solution.go: -------------------------------------------------------------------------------- 1 | func isValid(s string) bool { 2 | intStr := []rune(s) 3 | targetStack := []rune{} 4 | if len(intStr)%2 == 1 { 5 | return false 6 | } 7 | 8 | for i := 0; i < len(intStr); i++ { 9 | lengTarget := len(targetStack) - 1 10 | if lengTarget < 0 { 11 | lengTarget = 0 12 | } 13 | switch intStr[i] { 14 | case 40: 15 | targetStack = append(targetStack, intStr[i]) 16 | case 41: 17 | if len(targetStack) != 0 && targetStack[lengTarget] != 40 { 18 | return false 19 | } else { 20 | targetStack = append(targetStack[:lengTarget]) 21 | } 22 | 23 | case 91: 24 | targetStack = append(targetStack, intStr[i]) 25 | case 93: 26 | if len(targetStack) != 0 && targetStack[lengTarget] != 91 { 27 | return false 28 | } else { 29 | targetStack = append(targetStack[:lengTarget]) 30 | } 31 | 32 | case 123: 33 | targetStack = append(targetStack, intStr[i]) 34 | case 125: 35 | if len(targetStack) != 0 && targetStack[lengTarget] != 123 { 36 | return false 37 | } else { 38 | targetStack = append(targetStack[:lengTarget]) 39 | } 40 | } 41 | } 42 | 43 | if len(targetStack) != 0 { 44 | return false 45 | } else { 46 | return true 47 | } 48 | } -------------------------------------------------------------------------------- /202008/14/Sky-shang/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsValid(string s) 3 | { 4 | if (s == "") return true; 5 | if (s.Length%2 != 0) return false; 6 | Stack stack = new Stack(); 7 | for(int i = 0; i < s.Length; i++) 8 | { 9 | if(s[i]=='('|| s[i] == '['|| s[i] == '{') 10 | { 11 | //遇到左括号进栈 12 | stack.Push(s[i]); 13 | } 14 | //栈空时遇到右括号 直接false 15 | if ((s[i] == ')' || s[i] == ']' || s[i] == '}') && stack.Count == 0) 16 | return false; 17 | //遇到右括号且与栈顶匹配 18 | if (s[i] == ']') 19 | { 20 | if ((char)stack.Peek() != '[') 21 | return false; 22 | else 23 | stack.Pop(); 24 | 25 | } 26 | if (s[i] == ')') 27 | { 28 | if ((char)stack.Peek() != '(') 29 | return false; 30 | else 31 | stack.Pop(); 32 | 33 | } 34 | if (s[i] == '}') 35 | { 36 | if ((char)stack.Peek() != '{') 37 | return false; 38 | else 39 | stack.Pop(); 40 | 41 | } 42 | } 43 | //结束时栈空 44 | if (stack.Count == 0) 45 | return true; 46 | else 47 | return false; 48 | } 49 | } -------------------------------------------------------------------------------- /202008/14/Turbo/Solution.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace LeetCodeDailyTraining._20200814.Turbo 7 | { 8 | public class Solution 9 | { 10 | public bool IsValid(string s) 11 | { 12 | if (s.Length % 2 != 0) 13 | { 14 | return false; 15 | } 16 | Dictionary map = new Dictionary() 17 | { 18 | {')','(' }, 19 | {']','[' }, 20 | {'}','{' } 21 | }; 22 | Stack stack = new Stack(); 23 | for (int i = 0; i < s.Length; i++) 24 | { 25 | var item = s[i]; 26 | if (map.Values.Contains(item)) 27 | { 28 | stack.Push(item); 29 | } 30 | else 31 | { 32 | if(stack.Count == 0 || map[item] != stack.Pop()) 33 | { 34 | return false; 35 | } 36 | } 37 | } 38 | return stack.Count == 0; 39 | } 40 | 41 | /// 42 | /// Marked from CrazyLiuxp 43 | /// 44 | /// 45 | /// 46 | public bool IsValid2(string s) 47 | { 48 | if (string.IsNullOrEmpty(s)) 49 | { 50 | return true; 51 | } 52 | 53 | if (s.Length % 2 != 0) 54 | { 55 | return false; 56 | } 57 | 58 | Stack stack = new Stack(); 59 | foreach (char c in s) 60 | { 61 | if (stack.Count == 0) 62 | { 63 | stack.Push(c); 64 | } 65 | else if (c - stack.Peek() == 1 || c - stack.Peek() == 2) 66 | { 67 | stack.Pop(); 68 | } 69 | else 70 | { 71 | stack.Push(c); 72 | } 73 | } 74 | return !stack.Any(); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /202008/14/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public bool IsValid(string s) 4 | { 5 | if (s.Length % 2 == 0) 6 | { 7 | Stack stack = new Stack(); 8 | Dictionary brackets = new Dictionary() 9 | { 10 | {'(',')'}, 11 | {'[',']'}, 12 | {'{','}'} 13 | }; 14 | 15 | foreach (var c in s) 16 | { 17 | if (brackets.ContainsKey(c)) 18 | { 19 | stack.Push(c); 20 | continue; 21 | } 22 | 23 | if (stack.TryPop(out var temp) && brackets[temp] == c) 24 | { 25 | continue; 26 | } 27 | 28 | return false; 29 | } 30 | 31 | return stack.Count == 0; 32 | } 33 | return false; 34 | } 35 | } -------------------------------------------------------------------------------- /202008/14/wxj/20200814.java: -------------------------------------------------------------------------------- 1 | 2 | class 20200814 { 3 | public boolean isValid(String s) { 4 | if(s.length() % 2 != 0){ 5 | return false; 6 | } 7 | Stack st = new Stack(); 8 | for(char c: s.toCharArray()){ 9 | if(c == '(') st.push(')'); 10 | else if(c == '[') st.push(']'); 11 | else if(c == '{') st.push('}'); 12 | else if(st.isEmpty() || c!=st.pop()) return false; 13 | } 14 | return st.isEmpty(); 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /202008/14/明洋/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsValid(string s) { 3 | if (s.Length % 2 == 1) 4 | return false; 5 | 6 | var st = new System.Collections.Generic.Stack(); 7 | for (int i = 0; i < s.Length; i++) 8 | { 9 | // 如果是左括号则 向栈添加 10 | if (s[i] == '(' || s[i] == '{' || s[i] == '[') 11 | { 12 | st.Push(s[i]); 13 | } 14 | // 如果栈内有值,并且 (栈顶值 与现在值相对应 无则返回false 有则去掉栈顶对应) 15 | else if (st.Count != 0 && ((st.Peek() == '(' && s[i] == ')') || (st.Peek() == '[' && s[i] == ']') || (st.Peek() == '{' && s[i] == '}'))) 16 | { 17 | st.Pop(); 18 | continue; 19 | } 20 | else 21 | { 22 | return false; 23 | } 24 | } 25 | return st.Count == 0; 26 | } 27 | } -------------------------------------------------------------------------------- /202008/14/麦田的香/Solution.cs: -------------------------------------------------------------------------------- 1 | public bool IsValid_2(string s) 2 | { 3 | Dictionary dic = new Dictionary 4 | { 5 | { ']', '[' }, 6 | { ')', '(' }, 7 | { '}', '{' } 8 | }; 9 | var stack = new Stack(); 10 | foreach (var item in s) 11 | { 12 | if (dic.ContainsKey(item)) 13 | { 14 | if (stack.Count == 0) 15 | { 16 | return false; 17 | } 18 | else 19 | { 20 | var element = stack.Pop(); 21 | if (element != dic[item]) 22 | { 23 | return false; 24 | } 25 | } 26 | } 27 | else 28 | { 29 | stack.Push(item); 30 | } 31 | } 32 | //ex:"[[[" 33 | if (stack.Count == 0) 34 | { 35 | return true; 36 | } 37 | else 38 | { 39 | return false; 40 | } 41 | } -------------------------------------------------------------------------------- /202008/16/BenLampson/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | int oldData,newData, dimensional1, dimensional2 = 0; 4 | 5 | public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) 6 | { 7 | if (image[sr][sc] != newColor) 8 | { 9 | oldData = image[sr][sc]; 10 | newData = newColor; 11 | dimensional1 = image.Length; 12 | dimensional2 = image[0].Length; 13 | Fill(image, sr, sc ); 14 | } 15 | 16 | return image; 17 | } 18 | 19 | public void Fill(int[][] image, int sr, int sc) 20 | { 21 | if (sr < 0 || sc < 0 || sr >= dimensional1 || sc >= dimensional2 || image[sr][sc] != oldData) 22 | { 23 | return; 24 | } 25 | image[sr][sc] = newData; 26 | Fill(image, sr+1, sc); 27 | Fill(image, sr-1, sc); 28 | Fill(image, sr, sc+1); 29 | Fill(image, sr, sc-1); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /202008/16/CrazyLiuxp/Program.cs: -------------------------------------------------------------------------------- 1 | class Program{ 2 | 3 | int[] vx = new int[] { -1, 0, 1, 0 }; 4 | int[] vy = new int[] { 0, 1, 0, -1 }; 5 | 6 | public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) 7 | { 8 | var originColor = image[sr][sc]; 9 | Fill(image, sr, sc, originColor, newColor); 10 | return image; 11 | } 12 | 13 | private void Fill(int[][] image,int sr,int sc,int originColor,int newColor) 14 | { 15 | if (sr < 0 || sr >= image.Length || sc < 0 || sc >= image[0].Length) return; 16 | if (image[sr][sc] != originColor || image[sr][sc]==newColor) return; 17 | 18 | image[sr][sc] = newColor; 19 | 20 | for(int i = 0; i < 4; i++) 21 | { 22 | var x = sr + vx[i]; 23 | var y = sc + vy[i]; 24 | Fill(image, x, y, originColor, newColor); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /202008/16/Ivysaur/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Problem733 { 4 | 5 | // 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。 6 | // 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newColor,让你重新上色这幅图像。 7 | // 为了完成上色工作,从初始坐标开始,记录初始坐标的上下左右四个方向上像素值与初始坐标相同的相连像素点, 8 | //接着再记录这四个方向上符合条件的像素点与他们对应四个方向上像素值与初始坐标相同的相连像素点,……,重复该过程。 9 | //将所有有记录的像素点的颜色值改为新的颜色值。最后返回经过上色渲染后的图像。 10 | 11 | // 示例 1: 12 | // 输入: 13 | // image = [[1,1,1],[1,1,0],[1,0,1]] 14 | // sr = 1, sc = 1, newColor = 2 15 | // 输出: [[2,2,2],[2,2,0],[2,0,1]] 16 | // 解析: 17 | // 在图像的正中间,(坐标(sr,sc)=(1,1)), 18 | // 在路径上所有符合条件的像素点的颜色都被更改成2。 19 | // 注意,右下角的像素没有更改为2, 20 | // 因为它不是在上下左右四个方向上与初始点相连的像素点。 21 | 22 | // 注意: 23 | // image 和 image[0] 的长度在范围 [1, 50] 内。 24 | // 给出的初始点将满足 0 <= sr < image.length 和 0 <= sc < image[0].length。 25 | // image[i][j] 和 newColor 表示的颜色值在范围 [0, 65535]内。 26 | class Program { 27 | static void Main (string[] args) { 28 | Console.WriteLine ("Hello World!"); 29 | 30 | // int[][] image = new int[3][] { new int[] { 1, 1, 1 }, new int[] { 1, 1, 0 }, new int[] { 1, 0, 1 } }; 31 | // int sr = 1; 32 | // int sc = 1; 33 | // int newColor = 2; 34 | 35 | int[][] image = new int[2][] { new int[] { 0, 0, 0 }, new int[] { 0, 1, 1 } }; 36 | int sr = 1; 37 | int sc = 1; 38 | int newColor = 1; 39 | 40 | var grid = FloodFill (image, sr, sc, newColor); 41 | 42 | Console.WriteLine ("************"); 43 | foreach (var item in grid) { 44 | foreach (var i in item) { 45 | Console.Write ($"{i},"); 46 | } 47 | Console.WriteLine (); 48 | } 49 | Console.WriteLine ("************"); 50 | 51 | } 52 | 53 | public static int[][] FloodFill (int[][] image, int sr, int sc, int newColor) { 54 | if (sr < 0 || sr < 0 || image.Length < 1 || sr > image.Length - 1) { 55 | return image; 56 | } 57 | 58 | if (sr > image[0].Length - 1) { 59 | return image; 60 | } 61 | 62 | int initialNum = image[sr][sc]; 63 | 64 | if (initialNum == newColor) { 65 | return image; 66 | } 67 | 68 | image = Fill (image, sr, sc, newColor, initialNum); 69 | image[sr][sc] = newColor; 70 | return image; 71 | } 72 | 73 | static int[][] Fill (int[][] image, int sr, int sc, int newColor, int initialNum) { 74 | if (sr - 1 > -1) { 75 | if (image[sr - 1][sc] == initialNum) { 76 | image[sr - 1][sc] = newColor; 77 | image = Fill (image, sr - 1, sc, newColor, initialNum); 78 | } 79 | } 80 | 81 | if (sr + 1 < image.Length) { 82 | if (image[sr + 1][sc] == initialNum) { 83 | image[sr + 1][sc] = newColor; 84 | image = Fill (image, sr + 1, sc, newColor, initialNum); 85 | } 86 | } 87 | 88 | if (sc - 1 > -1) { 89 | if (image[sr][sc - 1] == initialNum) { 90 | image[sr][sc - 1] = newColor; 91 | image = Fill (image, sr, sc - 1, newColor, initialNum); 92 | } 93 | } 94 | 95 | if (sc + 1 < image[0].Length) { 96 | if (image[sr][sc + 1] == initialNum) { 97 | image[sr][sc + 1] = newColor; 98 | image = Fill (image, sr, sc + 1, newColor, initialNum); 99 | } 100 | } 101 | return image; 102 | } 103 | 104 | } 105 | } -------------------------------------------------------------------------------- /202008/16/Murph/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) { 3 | var hub = image[sr][sc]; // 中心点的基准值 4 | var maxX = image.Length; 5 | if (hub == newColor) return image; 6 | 7 | void next(int x, int y) 8 | { 9 | Console.WriteLine($"{x} {y}"); 10 | if (x < 0 || x >= maxX) return; 11 | if (y < 0 || y >= (image[x]?.Length ?? 0)) return; 12 | if (image[x][y] != hub) return; 13 | image[x][y] = newColor; 14 | 15 | next(x + 1, y); 16 | next(x - 1, y); 17 | next(x, y + 1); 18 | next(x, y - 1); 19 | } 20 | 21 | next(sr, sc); 22 | return image; 23 | } 24 | } -------------------------------------------------------------------------------- /202008/16/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/flood-fill/ 3 | 4 | #### 题目描述 5 | 有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。 6 | 7 | 给你一个坐标 `(sr, sc)` 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 `newColor`,让你重新上色这幅图像。 8 | 9 | 为了完成上色工作,从初始坐标开始,记录初始坐标的上下左右四个方向上像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应四个方向上像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为新的颜色值。 10 | 11 | 最后返回经过上色渲染后的图像。 12 | 13 | **示例 1:** 14 | 15 | ``` 16 | 输入: 17 | image = [[1,1,1],[1,1,0],[1,0,1]] 18 | sr = 1, sc = 1, newColor = 2 19 | 输出: [[2,2,2],[2,2,0],[2,0,1]] 20 | 解析: 21 | 在图像的正中间,(坐标(sr,sc)=(1,1)), 22 | 在路径上所有符合条件的像素点的颜色都被更改成2。 23 | 注意,右下角的像素没有更改为2, 24 | 因为它不是在上下左右四个方向上与初始点相连的像素点。 25 | ``` 26 | **注意:** 27 | 28 | - `image` 和 `image[0]` 的长度在范围 `[1, 50]` 内。 29 | - 给出的初始点将满足 `0 <= sr < image.length` 和 `0 <= sc < image[0].length`。 30 | - `image[i][j]` 和 `newColor` 表示的颜色值在范围 `[0, 65535]`内。 31 | 32 | 33 | 34 | ## 打卡 35 | - CRAZYLIUXP 36 | - Richeir 37 | - Murph 38 | - 明洋 39 | - BenLampson 40 | - Richeir 41 | - Murph 42 | - Ivysaur 43 | - wxj 44 | - Zhuangkh 45 | - Sky-shang 46 | - 麦田的香 47 | -------------------------------------------------------------------------------- /202008/16/Richeir/leetcode733.go: -------------------------------------------------------------------------------- 1 | func floodFill(image [][]int, sr int, sc int, newColor int) [][]int { 2 | result := recursion(&image, sr, sc, newColor) 3 | return result 4 | } 5 | 6 | //递归算法 7 | func recursion(imageRef *[][]int, x int, y int, newValue int) [][]int { 8 | //找到对应的4个数,如果数字和原版数字相同,则继续执行递归 9 | var image = *imageRef 10 | var orgValue = image[x][y] 11 | 12 | if image[x][y] == newValue { 13 | return image 14 | } 15 | 16 | image[x][y] = newValue 17 | 18 | top, left, bottom, right := findRonud(imageRef, x, y, orgValue) 19 | if top != -1 { 20 | recursion(imageRef, x-1, y, newValue) 21 | } 22 | 23 | if left != -1 { 24 | recursion(imageRef, x, y-1, newValue) 25 | } 26 | 27 | if bottom != -1 { 28 | recursion(imageRef, x+1, y, newValue) 29 | } 30 | 31 | if right != -1 { 32 | recursion(imageRef, x, y+1, newValue) 33 | } 34 | 35 | return image 36 | } 37 | 38 | func findRonud(imageRef *[][]int, x int, y int, orgValue int) (int, int, int, int) { 39 | var image = *imageRef 40 | lenX := len(image) - 1 41 | lenY := len(image[0]) - 1 42 | 43 | if x < 0 || y < 0 { 44 | return -1, -1, -1, -1 45 | } 46 | 47 | if x > lenX || y > lenY { 48 | return -1, -1, -1, -1 49 | } 50 | top := -1 51 | left := -1 52 | bottom := -1 53 | right := -1 54 | 55 | if x-1 < 0 || y < 0 { 56 | top = -1 57 | } else { 58 | top = image[x-1][y] 59 | if top != orgValue { 60 | top = -1 61 | } 62 | } 63 | 64 | if x < 0 || y-1 < 0 { 65 | left = -1 66 | } else { 67 | left = image[x][y-1] 68 | if left != orgValue { 69 | left = -1 70 | } 71 | } 72 | 73 | if x+1 > lenX { 74 | bottom = -1 75 | } else { 76 | bottom = image[x+1][y] 77 | if bottom != orgValue { 78 | bottom = -1 79 | } 80 | } 81 | 82 | if y+1 > lenY { 83 | right = -1 84 | } else { 85 | right = image[x][y+1] 86 | if right != orgValue { 87 | right = -1 88 | } 89 | } 90 | 91 | return top, left, bottom, right 92 | } -------------------------------------------------------------------------------- /202008/16/Sky-shang/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) { 3 | var hub = image[sr][sc]; // 中心点的基准值 4 | var maxX = image.Length; 5 | if (hub == newColor) return image; 6 | 7 | void next(int x, int y) 8 | { 9 | Console.WriteLine($"{x} {y}"); 10 | if (x < 0 || x >= maxX) return; 11 | if (y < 0 || y >= (image[x]?.Length ?? 0)) return; 12 | if (image[x][y] != hub) return; 13 | image[x][y] = newColor; 14 | 15 | next(x + 1, y); 16 | next(x - 1, y); 17 | next(x, y + 1); 18 | next(x, y - 1); 19 | } 20 | 21 | next(sr, sc); 22 | return image; 23 | } 24 | } -------------------------------------------------------------------------------- /202008/16/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) 4 | { 5 | if (image[sr][sc] == newColor) 6 | { 7 | return image; 8 | } 9 | 10 | int oldColor = image[sr][sc]; 11 | image[sr][sc] = newColor; 12 | 13 | if (sr != 0 && image[sr - 1][sc] == oldColor) 14 | { 15 | FloodFill(image, sr - 1, sc, newColor); 16 | } 17 | 18 | if (sr != image.Length - 1 && image[sr + 1][sc] == oldColor) 19 | { 20 | FloodFill(image, sr + 1, sc, newColor); 21 | } 22 | 23 | if (sc != 0 && image[sr][sc - 1] == oldColor) 24 | { 25 | FloodFill(image, sr, sc - 1, newColor); 26 | } 27 | 28 | if (sc != image[sr].Length - 1 && image[sr][sc + 1] == oldColor) 29 | { 30 | FloodFill(image, sr, sc + 1, newColor); 31 | } 32 | 33 | return image; 34 | } 35 | } -------------------------------------------------------------------------------- /202008/16/wxj/Solutionwxj.java: -------------------------------------------------------------------------------- 1 | class Solutionwxj { 2 | int[] dx = {1,0,0,-1}; 3 | int[] dy = {0,1,-1,0}; 4 | public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { 5 | int currColor = image[sr][sc]; 6 | if(currColor != newColor){ 7 | dfs(image,sr,sc,currColor,newColor); 8 | } 9 | return image; 10 | } 11 | 12 | void dfs(int[][] image, int sr, int sc,int currColor, int newColor){ 13 | if(image[sr][sc] == currColor){ 14 | image[sr][sc] = newColor; 15 | for(int i = 0; i < 4; i++){ 16 | if( sr+dx[i] >= 0 && sr+dx[i] < image.length && sc+dy[i] >=0 && sc+dy[i] < image[0].length){ 17 | if(image[sr+dx[i]][sc + dy[i]] == currColor){ 18 | dfs(image,sr+dx[i],sc + dy[i],currColor,newColor); 19 | } 20 | } 21 | } 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /202008/16/明洋/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public int[][] floodFill(int[][] image, int sr, int sc, int newColor) 4 | { 5 | int oldColor = image[sr][sc]; 6 | if (oldColor != newColor) 7 | ChangeColor(image, sr, sc, newColor, oldColor); 8 | return image; 9 | } 10 | 11 | private void ChangeColor(int[][] image, int x, int y, int newColor, int oldColor) 12 | { 13 | // 判断是否超出边界 14 | if (x < 0 || x >= image.Length || y < 0 || y >= image[0].Length) return; 15 | 16 | if (image[x][y] == oldColor) 17 | { 18 | image[x][y] = newColor; 19 | 20 | // 递归寻找四周是否有无,有则替换并继续递归,递归终止条件为超出边界和不是oldColor 21 | ChangeColor(image, x - 1, y, newColor, oldColor); 22 | ChangeColor(image, x + 1, y, newColor, oldColor); 23 | ChangeColor(image, x, y - 1, newColor, oldColor); 24 | ChangeColor(image, x, y + 1, newColor, oldColor); 25 | } 26 | 27 | return; 28 | } 29 | } -------------------------------------------------------------------------------- /202008/16/麦田的香/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[][] FloodFill(int[][] image, int sr, int sc, int newColor) 3 | { 4 | int initColor = image[sr][sc]; 5 | if (initColor == newColor) 6 | { 7 | return image; 8 | } 9 | dfs(image, sr, sc, initColor, newColor); 10 | return image; 11 | } 12 | 13 | public void dfs(int[][] image, int sr, int sc, int initColor, int newColor) 14 | { 15 | if (sr < 0 || sr > image.Count() - 1 || sc < 0 16 | || sc > image[0].Count() - 1 || image[sr][sc] != initColor) 17 | { 18 | return; 19 | } 20 | image[sr][sc] = newColor; 21 | 22 | dfs(image, sr - 1, sc, initColor, newColor);//上 23 | dfs(image, sr + 1, sc, initColor, newColor);//下 24 | dfs(image, sr, sc - 1, initColor, newColor);//左 25 | dfs(image, sr, sc + 1, initColor, newColor);//右 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /202008/17/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsBalanced(TreeNode root) 3 | { 4 | return height(root) != -1; 5 | } 6 | 7 | private int height(TreeNode root) 8 | { 9 | if (root == null) return 0; 10 | 11 | int leftHeight, rightHeight; 12 | if ((leftHeight = height(root.left)) == -1 || (rightHeight = height(root.right)) == -1 || Math.Abs(leftHeight - rightHeight) > 1) 13 | { 14 | return -1; 15 | } 16 | 17 | return Math.Max(leftHeight, rightHeight) + 1; 18 | } 19 | } -------------------------------------------------------------------------------- /202008/17/Murph/Solution.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left; 6 | * public TreeNode right; 7 | * public TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public bool IsBalanced(TreeNode root) 12 | { 13 | if (root == null) return true; 14 | return Math.Abs(height(root.left) - height(root.right)) <= 1 && IsBalanced(root.left) && IsBalanced(root.right); 15 | int height(TreeNode root) 16 | { 17 | if (root == null) return 0; 18 | 19 | return Math.Max(height(root.left), height(root.right)) + 1; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /202008/17/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/balanced-binary-tree/ 3 | 4 | #### 题目描述 5 | 给定一个二叉树,判断它是否是高度平衡的二叉树。 6 | 7 | 本题中,一棵高度平衡二叉树定义为: 8 | > 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1 9 | 10 | **示例 1:** 11 | 12 | 给定二叉树 **[3,9,20,null,null,15,7]** 13 | 14 | ``` 15 | 3 16 | / \ 17 | 9 20 18 | / \ 19 | 15 7 20 | 21 | ``` 22 | 23 | 返回 true 。 24 | **示例 2:** 25 | 给定二叉树 **[1,2,2,3,3,null,null,4,4]** 26 | 27 | ``` 28 | 1 29 | / \ 30 | 2 2 31 | / \ 32 | 3 3 33 | / \ 34 | 4 4 35 | 36 | ``` 37 | 返回 false 。 38 | ## 打卡 39 | - CrazyLiuxp 40 | - 明洋 41 | - Sky-shang 42 | - Murph 43 | - wxj 44 | - Richeir 45 | -------------------------------------------------------------------------------- /202008/17/Richeir/20200817_110.go: -------------------------------------------------------------------------------- 1 | func isBalanced(root *TreeNode) bool { 2 | if root == nil { 3 | return true 4 | } 5 | 6 | balance, _ := CheckNode(root) 7 | 8 | if balance { 9 | return true 10 | } 11 | 12 | return false 13 | } 14 | 15 | func CheckNode(node *TreeNode) (bool, int) { 16 | deepL := 0 17 | deepR := 0 18 | allBalance := true 19 | 20 | if node.Left != nil { 21 | allBalance, deepL = CheckNode(node.Left) 22 | if !allBalance { 23 | return false, 0 24 | } 25 | } 26 | 27 | if node.Right != nil { 28 | allBalance, deepR = CheckNode(node.Right) 29 | if !allBalance { 30 | return false, 0 31 | } 32 | } 33 | 34 | //叶子节点处理 35 | if node.Left == nil && node.Right == nil { 36 | return true, 1 37 | } 38 | 39 | //判断当前节点的是否平衡 40 | if node.Left != nil && node.Right != nil { 41 | // deepL++ 42 | // deepR++ 43 | 44 | if deepL-deepR <= 1 && deepL-deepR >= -1 { 45 | allBalance = true 46 | } else { 47 | return false, 0 48 | } 49 | 50 | if deepL > deepR { 51 | return true, deepL + 1 52 | } 53 | return true, deepR + 1 54 | } else if node.Left != nil && node.Right == nil { 55 | // deepL++ 56 | 57 | if deepL-deepR <= 1 && deepL-deepR >= -1 { 58 | allBalance = true 59 | } else { 60 | return false, 0 61 | } 62 | 63 | return allBalance, deepL + 1 64 | } else { 65 | // deepR++ 66 | 67 | if deepL-deepR <= 1 && deepL-deepR >= -1 { 68 | allBalance = true 69 | } else { 70 | return false, 0 71 | } 72 | 73 | return allBalance, deepR + 1 74 | } 75 | } -------------------------------------------------------------------------------- /202008/17/Sky-shang/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsBalanced(TreeNode root) 3 | { 4 | return height(root) != -1; 5 | } 6 | 7 | private int height(TreeNode root) 8 | { 9 | if (root == null) return 0; 10 | 11 | int leftHeight, rightHeight; 12 | if ((leftHeight = height(root.left)) == -1 || (rightHeight = height(root.right)) == -1 || Math.Abs(leftHeight - rightHeight) > 1) 13 | { 14 | return -1; 15 | } 16 | 17 | return Math.Max(leftHeight, rightHeight) + 1; 18 | } 19 | } -------------------------------------------------------------------------------- /202008/17/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left; 6 | * public TreeNode right; 7 | * public TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution 11 | { 12 | public bool IsBalanced(TreeNode root) 13 | { 14 | if (root == null) 15 | { 16 | return true; 17 | } 18 | 19 | if (IsBalanced(root.left) && IsBalanced(root.right)) 20 | { 21 | int left = root.left == null ? 0 : CalcHeight(root.left); 22 | int right = root.right == null ? 0 : CalcHeight(root.right); 23 | 24 | return Math.Abs(left - right) <= 1; 25 | } 26 | 27 | return false; 28 | } 29 | 30 | public int CalcHeight(TreeNode node) 31 | { 32 | int left = 0, right = 0; 33 | 34 | if (node?.left != null) 35 | { 36 | left = CalcHeight(node.left); 37 | } 38 | 39 | if (node?.right != null) 40 | { 41 | right = CalcHeight(node.right); 42 | } 43 | 44 | return left > right ? left + 1 : right + 1; 45 | } 46 | } -------------------------------------------------------------------------------- /202008/17/wxj/SolutionWxj.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * 4 | */ 5 | public class TreeNode { 6 | int val; 7 | TreeNode left; 8 | TreeNode right; 9 | TreeNode(int x) { val = x; } 10 | } 11 | class Solution { 12 | public boolean isBalanced(TreeNode root) { 13 | if(root == null) { 14 | return false; 15 | } 16 | return dataDeph(root) >= 0 ? true:false; 17 | 18 | } 19 | 20 | private int dataDeph(TreeNode root){ 21 | if(root == null) return 0; 22 | int leftDeph = dataDeph(root.left); 23 | int rightDeph = dataDeph(root.right); 24 | if(leftDeph < 0 || rightDeph < 0 || Math.abs(leftDeph - rightDeph) >1){ 25 | return -1; 26 | } 27 | return Math.max(leftDeph,rightDeph) +1; 28 | } 29 | } -------------------------------------------------------------------------------- /202008/17/明洋/Solution.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left; 6 | * public TreeNode right; 7 | * public TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution 11 | { 12 | public bool IsBalanced(TreeNode root) 13 | { 14 | return Height(root) >= 0; 15 | } 16 | 17 | // 返回当前根节点高度,如果有超出其高度平衡(1) 则返回-1 18 | public int Height(TreeNode root) 19 | { 20 | if (root == null) return 0; 21 | 22 | int legtHeight = Height(root.left); 23 | int rightHeight = Height(root.right); 24 | 25 | if (legtHeight == -1 || rightHeight == -1 || Math.Abs(legtHeight - rightHeight) > 1) 26 | { 27 | return -1; 28 | } 29 | else 30 | { 31 | return Math.Max(legtHeight, rightHeight) + 1; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /202008/18/CrazyLiuxp/Program.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * public int val; 5 | * public ListNode next; 6 | * public ListNode(int val=0, ListNode next=null) { 7 | * this.val = val; 8 | * this.next = next; 9 | * } 10 | * } 11 | */ 12 | /** 13 | * Definition for a binary tree node. 14 | * public class TreeNode { 15 | * public int val; 16 | * public TreeNode left; 17 | * public TreeNode right; 18 | * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { 19 | * this.val = val; 20 | * this.left = left; 21 | * this.right = right; 22 | * } 23 | * } 24 | */ 25 | public class Solution { 26 | private ListNode gloabalhead; 27 | public TreeNode SortedListToBST(ListNode head) { 28 | gloabalhead = head; 29 | int length = GetLength(head); 30 | return BuildTree(0, length - 1); 31 | } 32 | 33 | private int GetLength(ListNode head) 34 | { 35 | int result = 0; 36 | while (head != null) 37 | { 38 | result++; 39 | head = head.next; 40 | } 41 | return result; 42 | } 43 | 44 | //中序遍历 45 | private TreeNode BuildTree(int left,int right) 46 | { 47 | if (left > right) return null; 48 | 49 | int mid = (left + right + 1) / 2; 50 | 51 | TreeNode root = new TreeNode(); 52 | root.left = BuildTree(left, mid - 1); 53 | root.val = gloabalhead.val; 54 | gloabalhead = gloabalhead.next; 55 | root.right = BuildTree(mid + 1, right); 56 | 57 | return root; 58 | } 59 | } -------------------------------------------------------------------------------- /202008/18/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * public int val; 5 | * public ListNode next; 6 | * public ListNode(int val=0, ListNode next=null) { 7 | * this.val = val; 8 | * this.next = next; 9 | * } 10 | * } 11 | */ 12 | /** 13 | * Definition for a binary tree node. 14 | * public class TreeNode { 15 | * public int val; 16 | * public TreeNode left; 17 | * public TreeNode right; 18 | * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { 19 | * this.val = val; 20 | * this.left = left; 21 | * this.right = right; 22 | * } 23 | * } 24 | */ 25 | public class Solution { 26 | public TreeNode SortedListToBST(ListNode head) 27 | { 28 | if (head == null) return null; 29 | 30 | return BuildTree(head, null); 31 | } 32 | 33 | //方法1:快慢指针找中心节点 34 | private TreeNode BuildTree(ListNode start, ListNode end) 35 | { 36 | if (start == end) return null; 37 | 38 | ListNode fast = start, slow = start; 39 | 40 | while(fast!=end && fast.next != end) 41 | { 42 | fast = fast.next?.next; 43 | slow = slow.next; 44 | } 45 | 46 | TreeNode root = new TreeNode(slow.val); 47 | root.left = BuildTree(start, slow); 48 | root.right = BuildTree(slow.next, end); 49 | 50 | return root; 51 | } 52 | } -------------------------------------------------------------------------------- /202008/18/Murph/Solution.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * public int val; 5 | * public ListNode next; 6 | * public ListNode(int val=0, ListNode next=null) { 7 | * this.val = val; 8 | * this.next = next; 9 | * } 10 | * } 11 | */ 12 | /** 13 | * Definition for a binary tree node. 14 | * public class TreeNode { 15 | * public int val; 16 | * public TreeNode left; 17 | * public TreeNode right; 18 | * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { 19 | * this.val = val; 20 | * this.left = left; 21 | * this.right = right; 22 | * } 23 | * } 24 | */ 25 | public class Solution 26 | { 27 | public TreeNode SortedListToBST(ListNode head) 28 | { 29 | return build(head, null); 30 | 31 | TreeNode build(ListNode left, ListNode right) 32 | { 33 | if (left == right) return null; 34 | 35 | var mid = midFunc(left, right); 36 | var temp = new TreeNode(mid.val); 37 | temp.left = build(left, mid); 38 | temp.right = build(mid.next, right); 39 | return temp; 40 | } 41 | 42 | ListNode midFunc(ListNode left, ListNode right) 43 | { 44 | var fast = left; 45 | var last = left; 46 | while (fast != right && fast.next != right) 47 | { 48 | fast = fast.next; 49 | fast = fast.next; 50 | last = last.next; 51 | } 52 | 53 | return last; 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /202008/18/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | /** 9 | * Definition for a binary tree node. 10 | * type TreeNode struct { 11 | * Val int 12 | * Left *TreeNode 13 | * Right *TreeNode 14 | * } 15 | */ 16 | func sortedListToBST(head *ListNode) *TreeNode { 17 | return build(head, nil) 18 | } 19 | 20 | func build(left *ListNode, right *ListNode) *TreeNode { 21 | if left == right { 22 | return nil 23 | } 24 | 25 | mid := midFunc(left, right) 26 | root := &TreeNode{Val: mid.Val} 27 | root.Left = build(left, mid) 28 | root.Right = build(mid.Next, right) 29 | return root 30 | 31 | } 32 | 33 | func midFunc(left *ListNode, right *ListNode) *ListNode { 34 | fast := left 35 | last := left 36 | 37 | for fast != right && fast.Next != right { 38 | fast = fast.Next.Next 39 | last = last.Next 40 | } 41 | return last 42 | 43 | } -------------------------------------------------------------------------------- /202008/18/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ 3 | 4 | #### 题目描述 5 | 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。 6 | 7 | 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。 8 | 9 | 示例: 10 | 11 | ``` 12 | 给定的有序链表: [-10, -3, 0, 5, 9], 13 | 14 | 一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树: 15 | 16 | 0 17 | / \ 18 | -3 9 19 | / / 20 | -10 5 21 | 22 | ``` 23 | 24 | 25 | 26 | ## 打卡 27 | 28 | - CrazyLiuxp 29 | - Sky-shang 30 | - 明洋 31 | - wxj 32 | -------------------------------------------------------------------------------- /202008/18/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func sortedListToBST(head *ListNode) *TreeNode { 2 | return buildTree(head, nil) 3 | } 4 | 5 | func getMedian(left, right *ListNode) *ListNode { 6 | fast, slow := left, left 7 | for fast != right && fast.Next != right { 8 | fast = fast.Next.Next 9 | slow = slow.Next 10 | } 11 | return slow 12 | } 13 | 14 | func buildTree(left, right *ListNode) *TreeNode{ 15 | if left == right { 16 | return nil 17 | } 18 | mid := getMedian(left, right) 19 | root := &TreeNode{mid.Val, nil, nil} 20 | root.Left = buildTree(left, mid) 21 | root.Right = buildTree(mid.Next, right) 22 | return root 23 | } -------------------------------------------------------------------------------- /202008/18/wxj/SolutionWxj.java: -------------------------------------------------------------------------------- 1 | 2 | // //Definition for singly-linked list. 3 | public class ListNode { 4 | int val; 5 | ListNode next; 6 | ListNode() {} 7 | ListNode(int val) { this.val = val; } 8 | ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | } 10 | // 11 | // //Definition for a binary tree node. 12 | public class TreeNode { 13 | int val; 14 | TreeNode left; 15 | TreeNode right; 16 | TreeNode() {} 17 | TreeNode(int val) { this.val = val; } 18 | TreeNode(int val, TreeNode left, TreeNode right) { 19 | this.val = val; 20 | this.left = left; 21 | this.right = right; 22 | } 23 | } 24 | 25 | class Solution { 26 | public TreeNode sortedListToBST(ListNode head) { 27 | return buildTree(head,null); 28 | } 29 | 30 | public TreeNode buildTree (ListNode left,ListNode right){ 31 | //if(left == right || left == null ){ 32 | if (left == right) { 33 | return null; 34 | } 35 | ListNode mid = getMid(left,right); 36 | TreeNode treeNode = new TreeNode(mid.val); 37 | treeNode.left = buildTree(left,mid); 38 | treeNode.right = buildTree(mid.next,right); 39 | return treeNode; 40 | } 41 | public ListNode getMid (ListNode left,ListNode right){ 42 | ListNode fast = left; 43 | ListNode slow = left; 44 | while(fast != right && fast.next != right){ 45 | fast = fast.next; 46 | //if(fast != null){ 47 | fast = fast.next; 48 | // } 49 | slow = slow.next; 50 | //if(fast == null){ 51 | // break; 52 | //} 53 | } 54 | return slow; 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /202008/18/明洋/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public TreeNode SortedListToBST(ListNode head) 4 | { 5 | return BuildTree(head, null); 6 | } 7 | public TreeNode BuildTree(ListNode left, ListNode right) 8 | { 9 | if (left == right) 10 | { 11 | return null; 12 | } 13 | ListNode mid = GetMedian(left, right); 14 | TreeNode root = new TreeNode(mid.val); 15 | root.left = BuildTree(left, mid); 16 | root.right = BuildTree(mid.next, right); 17 | return root; 18 | } 19 | 20 | public ListNode GetMedian(ListNode left, ListNode right) 21 | { 22 | ListNode fast = left; 23 | ListNode slow = left; 24 | while (fast != right && fast.next != right) 25 | { 26 | fast = fast.next; 27 | fast = fast.next; 28 | slow = slow.next; 29 | } 30 | return slow; 31 | } 32 | } -------------------------------------------------------------------------------- /202008/19/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int CountSubstrings(string s) 3 | { 4 | int num = 0; 5 | for(int i = 0; i < s.Length; i++) 6 | { 7 | Count(s, i, i, ref num); 8 | Count(s, i, i + 1, ref num); 9 | } 10 | 11 | return num; 12 | } 13 | 14 | private void Count(string s, int left, int right, ref int num) 15 | { 16 | while (left >= 0 && right < s.Length && s[left] == s[right]) 17 | { 18 | num++; 19 | left--; 20 | right++; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /202008/19/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/palindromic-substrings/ 3 | 4 | #### 题目描述 5 | 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。 6 | 7 | 具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。 8 | 9 | 示例1: 10 | 11 | ``` 12 | 输入:"abc" 13 | 输出:3 14 | 解释:三个回文子串: "a", "b", "c" 15 | ``` 16 | 17 | 示例2: 18 | 19 | ``` 20 | 输入:"aaa" 21 | 输出:6 22 | 解释:6个回文子串: "a", "a", "a", "aa", "aa", "aaa" 23 | ``` 24 | 25 | 26 | ## 打卡 27 | 28 | - CrazyLiuxp 29 | - Richeir 30 | - Sky-shang 31 | -------------------------------------------------------------------------------- /202008/19/Richeir/20200819_647.go: -------------------------------------------------------------------------------- 1 | func countSubstrings(s string) int { 2 | result := 0 3 | strInt := []rune(s) 4 | lenS := len(s) 5 | for i := 1; i <= lenS+1; i++ { 6 | for j := 0; j < lenS; j++ { 7 | if (i + j) <= lenS { 8 | temp := strInt[j : j+i] 9 | if isMirror(temp) { 10 | result++ 11 | } 12 | } else { 13 | break 14 | } 15 | } 16 | } 17 | 18 | return result 19 | } 20 | 21 | func isMirror(str []rune) bool { 22 | lengthStr := len(str) 23 | 24 | if lengthStr == 1 { 25 | return true 26 | } 27 | 28 | for i := 0; i < lengthStr/2; i++ { 29 | if str[i] != str[lengthStr-i-1] { 30 | return false 31 | } 32 | } 33 | 34 | return true 35 | } -------------------------------------------------------------------------------- /202008/19/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func countSubstrings(s string) int { 2 | n := len(s) 3 | ans := 0 4 | for i := 0; i < 2 * n - 1; i++ { 5 | l, r := i / 2, i / 2 + i % 2 6 | for l >= 0 && r < n && s[l] == s[r] { 7 | l-- 8 | r++ 9 | ans++ 10 | } 11 | } 12 | return ans 13 | } -------------------------------------------------------------------------------- /202008/20/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 1.当前点击的是【未挖出来的雷】,我们将其改为X即可,表示点到了雷,然后结束。 3 | // 2.当前点击的是【为挖出的空方块】,我们需要统计他周围相邻(八个)的方块里的地雷的数量cnt(即M的数量)。 4 | // 如果cnt>0,就将当前方块改为cnt的数字(附近的雷数) 5 | // 如果cnt为0,则将其改为B,并递归计算周围八个未挖出的方块,如果没有更多方块可以被揭露,则返回面板。 6 | public char[][] UpdateBoard (char[][] board, int[] click) { 7 | int x = click[0], y = click[1]; 8 | 9 | //如果当前坐标位置处为M,则点中了“雷”,则Game over 10 | if (board[x][y] == 'M') { 11 | board[x][y] = 'X'; 12 | } else { 13 | dfs (board, x, y); 14 | } 15 | 16 | return board; 17 | } 18 | 19 | //左、左上、上、右上、右、右下、下、左下 20 | int[] vx = new int[] {-1, -1, 0, 1, 1, 1, 0, -1 }; 21 | int[] vy = new int[] { 0, -1, -1, -1, 0, 1, 1, 1 }; 22 | private void dfs (char[][] board, int x, int y) { 23 | //计算当前点[x,y]周围M的数量 24 | int cnt = 0; 25 | for (int i = 0; i < 8; i++) { 26 | int currentX = x + vx[i]; 27 | int currentY = y + vy[i]; 28 | 29 | //跳过不符合递归条件的块 30 | //1.如果坐标超出范围 31 | if (currentX < 0 || currentX >= board.Length || currentY < 0 || currentY >= board[0].Length) { 32 | continue; 33 | } 34 | 35 | if (board[currentX][currentY] == 'M') { 36 | cnt++; 37 | } 38 | } 39 | 40 | //周围M个数不为0时,将[x,y]处的值设置成cnt数字 41 | if (cnt > 0) { 42 | board[x][y] = (char) (cnt + '0'); 43 | } else { 44 | //将[x,y]初的值设置成B 45 | board[x][y] = 'B'; 46 | 47 | //递归周围8个点 48 | for (int i = 0; i < 8; i++) { 49 | int currentX = x + vx[i]; 50 | int currentY = y + vy[i]; 51 | 52 | //跳过不符合递归条件的块 53 | //1.如果坐标超出范围 54 | //2.或者当前坐标处值为B,表示当前坐标已经处理过 55 | if (currentX < 0 || currentX >= board.Length || currentY < 0 || currentY >= board[0].Length || board[currentX][currentY] != 'E') { 56 | continue; 57 | } 58 | 59 | dfs (board, x + vx[i], y + vy[i]); 60 | } 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /202008/20/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/minesweeper/ 3 | 4 | #### 题目描述 5 | 6 | 让我们一起来玩扫雷游戏! 7 | 8 | 给定一个代表游戏板的二维字符矩阵。 '**M**' 代表一个**未挖出的**地雷,'**E**' 代表一个**未挖出的**空方块,'**B**' 代表没有相邻(上,下,左,右,和所有4个对角线)地雷的**已挖出的**空白方块,数字('1' 到 '8')表示有多少地雷与这块**已挖出的**方块相邻,'**X**' 则表示一个**已挖出的**地雷。 9 | 10 | 现在给出在所有**未挖出的**方块中('M'或者'E')的下一个点击位置(行和列索引),根据以下规则,返回相应位置被点击后对应的面板: 11 | 12 | 1. 如果一个地雷('M')被挖出,游戏就结束了- 把它改为 '**X**'。 13 | 2. 如果一个**没有相邻地雷**的空方块('E')被挖出,修改它为('B'),并且所有和其相邻的**未挖出**方块都应该被递归地揭露。 14 | 3. 如果一个**至少与一个地雷相邻**的空方块('E')被挖出,修改它为数字('1'到'8'),表示相邻地雷的数量。 15 | 4. 如果在此次点击中,若无更多方块可被揭露,则返回面板。 16 | 17 | 18 | 19 | **示例 1:** 20 | 21 | ``` 22 | 输入: 23 | 24 | [['E', 'E', 'E', 'E', 'E'], 25 | ['E', 'E', 'M', 'E', 'E'], 26 | ['E', 'E', 'E', 'E', 'E'], 27 | ['E', 'E', 'E', 'E', 'E']] 28 | 29 | Click : [3,0] 30 | 31 | 输出: 32 | 33 | [['B', '1', 'E', '1', 'B'], 34 | ['B', '1', 'M', '1', 'B'], 35 | ['B', '1', '1', '1', 'B'], 36 | ['B', 'B', 'B', 'B', 'B']] 37 | ``` 38 | 39 | 40 | 41 | **示例 2:** 42 | 43 | ``` 44 | 输入: 45 | 46 | [['B', '1', 'E', '1', 'B'], 47 | ['B', '1', 'M', '1', 'B'], 48 | ['B', '1', '1', '1', 'B'], 49 | ['B', 'B', 'B', 'B', 'B']] 50 | 51 | Click : [1,2] 52 | 53 | 输出: 54 | 55 | [['B', '1', 'E', '1', 'B'], 56 | ['B', '1', 'X', '1', 'B'], 57 | ['B', '1', '1', '1', 'B'], 58 | ['B', 'B', 'B', 'B', 'B']] 59 | ``` 60 | 61 | 62 | 63 | **注意:** 64 | 65 | 1. 输入矩阵的宽和高的范围为 [1,50]。 66 | 2. 点击的位置只能是未被挖出的方块 ('M' 或者 'E'),这也意味着面板至少包含一个可点击的方块。 67 | 3. 输入面板不会是游戏结束的状态(即有地雷已被挖出)。 68 | 4. 简单起见,未提及的规则在这个问题中可被忽略。例如,当游戏结束时你不需要挖出所有地雷,考虑所有你可能赢得游戏或标记方块的情况。 69 | 70 | 71 | 72 | 73 | ## 打卡 74 | 75 | - CrazyLiuxp 76 | - Zhuangkh 77 | - Sky-shang 78 | -------------------------------------------------------------------------------- /202008/20/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | var dirX = []int{0, 1, 0, -1, 1, 1, -1, -1} 2 | var dirY = []int{1, 0, -1, 0, 1, -1, 1, -1} 3 | 4 | func updateBoard(board [][]byte, click []int) [][]byte { 5 | x, y := click[0], click[1] 6 | if board[x][y] == 'M' { 7 | board[x][y] = 'X' 8 | } else { 9 | dfs(board, x, y) 10 | } 11 | return board 12 | } 13 | 14 | func dfs(board [][]byte, x, y int) { 15 | cnt := 0 16 | for i := 0; i < 8; i++ { 17 | tx, ty := x + dirX[i], y + dirY[i] 18 | if tx < 0 || tx >= len(board) || ty < 0 || ty >= len(board[0]) { 19 | continue 20 | } 21 | // 不用判断 M,因为如果有 M 的话游戏已经结束了 22 | if board[tx][ty] == 'M' { 23 | cnt++ 24 | } 25 | } 26 | if cnt > 0 { 27 | board[x][y] = byte(cnt + '0') 28 | } else { 29 | board[x][y] = 'B' 30 | for i := 0; i < 8; i++ { 31 | tx, ty := x + dirX[i], y + dirY[i] 32 | // 这里不需要在存在 B 的时候继续扩展,因为 B 之前被点击的时候已经被扩展过了 33 | if tx < 0 || tx >= len(board) || ty < 0 || ty >= len(board[0]) || board[tx][ty] != 'E' { 34 | continue 35 | } 36 | dfs(board, tx, ty) 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /202008/20/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public char[][] UpdateBoard(char[][] board, int[] click) 4 | { 5 | //检查数组合规性,或者点击的是否为Blank 6 | if (click.Length != 2 || click[0] > board.Length || click[1] > board[click[0]].Length || board[click[0]][click[1]] == 'B') 7 | { 8 | return board; 9 | } 10 | 11 | int r = click[0], c = click[1]; 12 | 13 | //检查是否为Mine 14 | if (board[r][c] == 'M') 15 | { 16 | board[r][c] = 'X'; 17 | return board; 18 | } 19 | 20 | //检查周围八方块是否有Mine,有则最终修改当前方块数量,无则递归周围八方块 21 | int[] u = { -1, -1, -1, 0, 0, 1, 1, 1 }; 22 | int[] v = { -1, 0, 1, -1, 1, -1, 0, 1 }; 23 | int mines = 0; 24 | for (int i = 0; i < 8; i++) 25 | { 26 | if (r + u[i] < 0 || c + v[i] < 0 || r + u[i] >= board.Length || c + v[i] >= board[r].Length) 27 | { 28 | continue; 29 | } 30 | 31 | if (board[r + u[i]][c + v[i]] == 'M') 32 | { 33 | mines++; 34 | } 35 | } 36 | 37 | if (mines > 0) 38 | { 39 | board[r][c] = (char)('0' + mines); 40 | return board; 41 | } 42 | 43 | board[r][c] = 'B'; 44 | 45 | for (int i = 0; i < 8; i++) 46 | { 47 | if (r + u[i] < 0 || c + v[i] < 0 || r + u[i] >= board.Length || c + v[i] >= board[r].Length) 48 | { 49 | continue; 50 | } 51 | 52 | board = UpdateBoard(board, new int[] { r + u[i], c + v[i] }); 53 | } 54 | 55 | return board; 56 | } -------------------------------------------------------------------------------- /202008/21/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public int MinDepth(TreeNode root) 4 | { 5 | if (root == null) return 0; 6 | 7 | if (root.left == null && root.right != null) { 8 | return MinDepth (root.right) + 1; 9 | } 10 | 11 | if (root.left != null && root.right == null) { 12 | return MinDepth (root.left) + 1; 13 | } 14 | 15 | return Math.Min (MinDepth (root.left), MinDepth (root.right)) + 1; 16 | } 17 | } -------------------------------------------------------------------------------- /202008/21/Murph/Solution.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left; 6 | * public TreeNode right; 7 | * public TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public int MinDepth (TreeNode root) { 12 | if (root == null) return 0; 13 | var minTemplate = min (root); 14 | return minTemplate; 15 | int min (TreeNode node) { 16 | if (node.left == null && node.right == null) return 1; 17 | 18 | int minTT = int.MaxValue; 19 | if (node.left != null) minTT = Math.Min (min (node.left), minTT); 20 | 21 | if (node.right != null) minTT = Math.Min (min (node.right), minTT); 22 | 23 | return minTT + 1; 24 | } 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /202008/21/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | func minDepth(root *TreeNode) int { 2 | if root == nil { 3 | return 0 4 | } 5 | return getMin(root) 6 | } 7 | 8 | func min(x int, y int) int { 9 | if x < y { 10 | return x 11 | } 12 | return y 13 | } 14 | 15 | func getMin(node *TreeNode) int { 16 | 17 | if node.Left == nil && node.Right == nil { 18 | return 1 19 | } 20 | 21 | minTT := 99999 22 | 23 | if node.Left != nil { 24 | minTT = min(minTT, getMin(node.Left)) 25 | } 26 | 27 | if node.Right != nil { 28 | minTT = min(minTT, getMin(node.Right)) 29 | } 30 | 31 | return minTT + 1 32 | 33 | } -------------------------------------------------------------------------------- /202008/21/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ 3 | 4 | #### 题目描述 5 | 6 | 给定一个二叉树,找出其最小深度。 7 | 8 | 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 9 | 10 | **说明**: 叶子节点是指没有子节点的节点。 11 | 12 | **示例:** 13 | 14 | 给定二叉树 ```[3,9,20,null,null,15,7]```, 15 | 16 | ``` 17 | 3 18 | / \ 19 | 9 20 20 | / \ 21 | 15 7 22 | ``` 23 | 24 | 返回它的最小深度 2. 25 | 26 | ## 打卡 27 | 28 | - Zhuangkh 29 | - Murph 30 | - CrazyLiuxp 31 | - Sky-shang 32 | - Richeir -------------------------------------------------------------------------------- /202008/21/Richeir/20200821_111.go: -------------------------------------------------------------------------------- 1 | func minDepth(root *TreeNode) int { 2 | 3 | if root == nil { 4 | return 0 5 | } 6 | resultLeft := 0 7 | resultRight := 0 8 | if root.Left != nil { 9 | resultLeft = CheckNode111(root.Left) 10 | if root.Right == nil { 11 | return resultLeft + 1 12 | } 13 | } 14 | 15 | if root.Right != nil { 16 | resultRight = CheckNode111(root.Right) 17 | if root.Left == nil { 18 | return resultRight + 1 19 | } 20 | } 21 | 22 | if resultLeft < resultRight { 23 | return resultLeft + 1 24 | } else { 25 | return resultRight + 1 26 | } 27 | } 28 | 29 | func CheckNode111(node *TreeNode) int { 30 | deepL := 0 31 | deepR := 0 32 | 33 | if node.Left != nil { 34 | deepL += CheckNode111(node.Left) 35 | } 36 | 37 | if node.Right != nil { 38 | deepR += CheckNode111(node.Right) 39 | } 40 | 41 | //叶子节点处理 42 | if node.Left == nil && node.Right == nil { 43 | return 1 44 | } 45 | 46 | if node.Left != nil && node.Right != nil { 47 | if deepL < deepR { 48 | return deepL + 1 49 | } 50 | return deepR + 1 51 | } else if node.Left != nil && node.Right == nil { 52 | return deepL + 1 53 | } else { 54 | return deepR + 1 55 | } 56 | } -------------------------------------------------------------------------------- /202008/21/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func minDepth(root *TreeNode) int { 10 | if root == nil { 11 | return 0 12 | } 13 | if root.Left == nil && root.Right == nil { 14 | return 1 15 | } 16 | minD := math.MaxInt32 17 | if root.Left != nil { 18 | minD = min(minDepth(root.Left), minD) 19 | } 20 | if root.Right != nil { 21 | minD = min(minDepth(root.Right), minD) 22 | } 23 | return minD + 1 24 | } 25 | 26 | func min(x, y int) int { 27 | if x < y { 28 | return x 29 | } 30 | return y 31 | } -------------------------------------------------------------------------------- /202008/21/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public int MinDepth(TreeNode root) 4 | { 5 | if (root == null) 6 | { 7 | return 0; 8 | } 9 | 10 | int left = MinDepth(root.left); 11 | int right = MinDepth(root.right); 12 | 13 | return left == 0 ? right + 1 : right == 0 ? left + 1 : Math.Min(left, right) + 1; 14 | } 15 | } -------------------------------------------------------------------------------- /202008/21/明洋/Solution.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left; 6 | * public TreeNode right; 7 | * public TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution 11 | { 12 | public int MinDepth(TreeNode root) 13 | { 14 | if (root == null) return 0; 15 | 16 | if (root.left == null && root.right == null) return 1; 17 | 18 | var depth = int.MaxValue; 19 | if (root.left!= null) 20 | { 21 | depth = Math.Min(MinDepth(root.left), depth); 22 | } 23 | if (root.right != null) 24 | { 25 | depth = Math.Min(MinDepth(root.right), depth); 26 | } 27 | return depth + 1; 28 | } 29 | } -------------------------------------------------------------------------------- /202008/22/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/24-game/ 3 | 4 | #### 题目描述 5 | 24 点游戏 6 | 7 | 你有 4 张写有 1 到 9 数字的牌。你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24。 8 | 9 | 示例 1: 10 | 11 | 输入: [4, 1, 8, 7] 12 | 输出: True 13 | 解释: (8-4) * (7-1) = 24 14 | 示例 2: 15 | 16 | 输入: [1, 2, 1, 2] 17 | 输出: False 18 | 注意: 19 | 20 | 除法运算符 / 表示实数除法,而不是整数除法。例如 4 / (1 - 2/3) = 12 。 21 | 每个运算符对两个数进行运算。特别是我们不能用 - 作为一元运算符。例如,[1, 1, 1, 1] 作为输入时,表达式 -1 - 1 - 1 - 1 是不允许的。 22 | 你不能将数字连接在一起。例如,输入为 [1, 2, 1, 2] 时,不能写成 12 + 12 。 23 | 24 | ## 打卡 25 | 26 | - Sky-shang 27 | -------------------------------------------------------------------------------- /202008/22/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | const ( 2 | TARGET = 24 3 | EPSILON = 1e-6 4 | ADD, MULTIPLY, SUBTRACT, DIVIDE = 0, 1, 2, 3 5 | ) 6 | 7 | func judgePoint24(nums []int) bool { 8 | list := []float64{} 9 | for _, num := range nums { 10 | list = append(list, float64(num)) 11 | } 12 | return solve(list) 13 | } 14 | 15 | func solve(list []float64) bool { 16 | if len(list) == 0 { 17 | return false 18 | } 19 | if len(list) == 1 { 20 | return abs(list[0] - TARGET) < EPSILON 21 | } 22 | size := len(list) 23 | for i := 0; i < size; i++ { 24 | for j := 0; j < size; j++ { 25 | if i != j { 26 | list2 := []float64{} 27 | for k := 0; k < size; k++ { 28 | if k != i && k != j { 29 | list2 = append(list2, list[k]) 30 | } 31 | } 32 | for k := 0; k < 4; k++ { 33 | if k < 2 && i < j { 34 | continue 35 | } 36 | switch k { 37 | case ADD: 38 | list2 = append(list2, list[i] + list[j]) 39 | case MULTIPLY: 40 | list2 = append(list2, list[i] * list[j]) 41 | case SUBTRACT: 42 | list2 = append(list2, list[i] - list[j]) 43 | case DIVIDE: 44 | if abs(list[j]) < EPSILON { 45 | continue 46 | } else { 47 | list2 = append(list2, list[i] / list[j]) 48 | } 49 | } 50 | if solve(list2) { 51 | return true 52 | } 53 | list2 = list2[:len(list2) - 1] 54 | } 55 | } 56 | } 57 | } 58 | return false 59 | } 60 | 61 | func abs(x float64) float64 { 62 | if x < 0 { 63 | return -x 64 | } 65 | return x 66 | } -------------------------------------------------------------------------------- /202008/23/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/ 3 | 4 | #### 题目描述 5 | 6 | 数字范围按位与 7 | 8 | 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。 9 | 10 | 示例 1:  11 | 12 | 输入: [5,7] 13 | 输出: 4 14 | 示例 2: 15 | 16 | 输入: [0,1] 17 | 输出: 0 18 | 19 | ## 打卡 20 | - Richeir 21 | - Sky-shang 22 | -------------------------------------------------------------------------------- /202008/23/Richeir/20200823_201.go: -------------------------------------------------------------------------------- 1 | func rangeBitwiseAnd(m int, n int) int { 2 | var compareResult = m & n 3 | 4 | for i := n - 1; i >= m+1; i-- { 5 | compareResult = compareResult & i 6 | if compareResult == 0 { 7 | return compareResult 8 | } 9 | } 10 | 11 | return compareResult 12 | } -------------------------------------------------------------------------------- /202008/23/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func rangeBitwiseAnd(m int, n int) int { 2 | shift := 0 3 | for m < n { 4 | m, n = m >> 1, n >> 1 5 | shift++ 6 | } 7 | return m << shift 8 | } -------------------------------------------------------------------------------- /202008/24/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool RepeatedSubstringPattern(string s) 3 | { 4 | return (s + s).Substring(1, (s + s).Length - 2).Contains(s); 5 | } 6 | } -------------------------------------------------------------------------------- /202008/24/Murph/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool RepeatedSubstringPattern(string s) 3 | { 4 | if (s == null || s.Length <=0) return false; 5 | return (s + s).IndexOf(s, 1) != s.Length; 6 | } 7 | } -------------------------------------------------------------------------------- /202008/24/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/repeated-substring-pattern/ 3 | 4 | #### 题目描述 5 | 6 | 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。 7 | 8 | 9 | **示例 1:** 10 | 11 | ``` 12 | 输入: "abab" 13 | 14 | 输出: True 15 | 16 | 解释: 可由子字符串 "ab" 重复两次构成。 17 | ``` 18 | 19 | **示例 2:** 20 | 21 | ``` 22 | 输入: "aba" 23 | 24 | 输出: False 25 | ``` 26 | 27 | **示例 3:** 28 | 29 | ``` 30 | 输入: "abcabcabcabc" 31 | 32 | 输出: True 33 | 34 | 解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。) 35 | ``` 36 | 37 | 38 | ## 打卡 39 | 40 | - Zhuangkh 41 | - CrazyLiuxp 42 | - 明洋 43 | - Sky-sahng 44 | - Murph 45 | - Richeir 46 | -------------------------------------------------------------------------------- /202008/24/Richeir/20200824_459.go: -------------------------------------------------------------------------------- 1 | func repeatedSubstringPattern(s string) bool { 2 | result := false 3 | b1 := []rune(s) 4 | lenS := len(b1) 5 | isSame := true 6 | wordMaxLength := lenS / 2 7 | for i := 1; i <= wordMaxLength; i++ { 8 | if lenS%i != 0 { 9 | continue 10 | } 11 | 12 | isSame = true 13 | for k := 0; k < i; k++ { 14 | left := b1[k] 15 | if isSame { 16 | bei := lenS / i 17 | for j := 1; j < bei; j++ { 18 | //左边是子串的某一位,右边是需要匹配的下N个子串的某一位 19 | nextPos := k + j*i 20 | right := b1[nextPos] 21 | if left != right { 22 | isSame = false 23 | break 24 | } 25 | } 26 | } else { 27 | break 28 | } 29 | } 30 | if isSame { 31 | return true 32 | } 33 | } 34 | 35 | return result 36 | } -------------------------------------------------------------------------------- /202008/24/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func repeatedSubstringPattern(s string) bool { 2 | n := len(s) 3 | for i := 1; i * 2 <= n; i++ { 4 | if n % i == 0 { 5 | match := true 6 | for j := i; j < n; j++ { 7 | if s[j] != s[j - i] { 8 | match = false 9 | break 10 | } 11 | } 12 | if match { 13 | return true 14 | } 15 | } 16 | } 17 | return false 18 | } 19 | -------------------------------------------------------------------------------- /202008/24/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public bool RepeatedSubstringPattern(string s) 4 | { 5 | int length = s.Length; 6 | for (int i = 1; i < length; i++) 7 | { 8 | bool match = true; 9 | if (length % i == 0) 10 | { 11 | for (int j = i; j < length; j++) 12 | { 13 | if (s[j] != s[j - i]) 14 | { 15 | match = false; 16 | break; 17 | } 18 | 19 | } 20 | 21 | if (match) 22 | { 23 | return true; 24 | } 25 | } 26 | } 27 | 28 | return false; 29 | } 30 | 31 | public bool RepeatedSubstringPattern2(string s) 32 | { 33 | return (s + s).IndexOf(s, 1) != s.Length; 34 | } 35 | } -------------------------------------------------------------------------------- /202008/24/明洋/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public bool RepeatedSubstringPattern(string s) 4 | { 5 | // 如果输入abab 则abab + abab 截取到从1 取到最后一位前 (bababa) 看其是否包含abab有则true 6 | return (s + s).Substring(1, (s + s).Length - 2).Contains(s); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /202008/25/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/increasing-subsequences/ 3 | 4 | #### 题目描述 5 | 491. 递增子序列 6 | 7 | 给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2。 8 | 9 | 示例: 10 | 11 | 输入: [4, 6, 7, 7] 12 | 输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]] 13 | 说明: 14 | 15 | 给定数组的长度不会超过15。 16 | 数组中的整数范围是 [-100,100]。 17 | 给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况。 18 | 19 | ``` 20 | 21 | 22 | ## 打卡 23 | 24 | - Sky-sahng 25 | 26 | -------------------------------------------------------------------------------- /202008/25/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | var ( 2 | n int 3 | temp []int 4 | ) 5 | func findSubsequences(nums []int) [][]int { 6 | n = len(nums) 7 | ans := [][]int{} 8 | set := map[int]bool{} 9 | for i := 0; i < 1 << n; i++ { 10 | findSubsequences1(i, nums) 11 | hashValue := getHash(263, int(1e9 + 7)) 12 | if check() && !set[hashValue] { 13 | t := make([]int, len(temp)) 14 | copy(t, temp) 15 | ans = append(ans, t) 16 | set[hashValue] = true 17 | } 18 | } 19 | return ans 20 | } 21 | 22 | func findSubsequences1(mask int, nums []int) { 23 | temp = []int{} 24 | for i := 0; i < n; i++ { 25 | if (mask & 1) != 0 { 26 | temp = append(temp, nums[i]) 27 | } 28 | mask >>= 1 29 | } 30 | } 31 | 32 | func getHash(base, mod int) int { 33 | hashValue := 0 34 | for _, x := range temp { 35 | hashValue = hashValue * base % mod + (x + 101) 36 | hashValue %= mod 37 | } 38 | return hashValue 39 | } 40 | 41 | func check() bool { 42 | for i := 1; i < len(temp); i++ { 43 | if temp[i] < temp[i - 1] { 44 | return false 45 | } 46 | } 47 | return len(temp) >= 2 48 | } 49 | -------------------------------------------------------------------------------- /202008/26/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | var dic = map[string]string{ 2 | "2": "abc", 3 | "3": "def", 4 | "4": "ghi", 5 | "5": "jkl", 6 | "6": "mno", 7 | "7": "pqrs", 8 | "8": "tuv", 9 | "9": "wxyz", 10 | } 11 | 12 | var container []string 13 | 14 | func letterCombinations(digits string) []string { 15 | container = []string{} 16 | if len(digits) <= 0 { 17 | return []string{} 18 | } 19 | next(digits, 0, "") 20 | return container 21 | } 22 | 23 | func next(digits string, dig int, text string) { 24 | if dig == len(digits) { 25 | container = append(container, text) 26 | } else { 27 | temp := string(digits[dig]) // 当次指针指向的原数组对象 28 | last := dic[temp] 29 | 30 | for i := 0; i < len(last); i++ { 31 | next(digits, dig+1, text+string(last[i])) 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /202008/26/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/ 3 | 4 | #### 题目描述 5 | 17. 电话号码的字母组合 6 | 7 | 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 8 | 9 | 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。 10 | 11 | 12 | 13 | 示例: 14 | 15 | 输入:"23" 16 | 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 17 | 说明: 18 | 尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。 19 | 20 | ``` 21 | 22 | 23 | ## 打卡 24 | 25 | - Sky-shang 26 | - Murph 27 | 28 | -------------------------------------------------------------------------------- /202008/26/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | var phoneMap map[string]string = map[string]string{ 2 | "2": "abc", 3 | "3": "def", 4 | "4": "ghi", 5 | "5": "jkl", 6 | "6": "mno", 7 | "7": "pqrs", 8 | "8": "tuv", 9 | "9": "wxyz", 10 | } 11 | 12 | var combinations []string 13 | 14 | func letterCombinations(digits string) []string { 15 | if len(digits) == 0 { 16 | return []string{} 17 | } 18 | combinations = []string{} 19 | backtrack(digits, 0, "") 20 | return combinations 21 | } 22 | 23 | func backtrack(digits string, index int, combination string) { 24 | if index == len(digits) { 25 | combinations = append(combinations, combination) 26 | } else { 27 | digit := string(digits[index]) 28 | letters := phoneMap[digit] 29 | lettersCount := len(letters) 30 | for i := 0; i < lettersCount; i++ { 31 | backtrack(digits, index + 1, combination + string(letters[i])) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /202008/27/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | func findItinerary(tickets [][]string) []string { 2 | var ( 3 | tempMap = map[string][]string{} 4 | res []string 5 | ) 6 | 7 | //把传入来的所有first跟last按照收尾插入map 8 | for _, ticket := range tickets { 9 | first, last := ticket[0], ticket[1] 10 | tempMap[first] = append(tempMap[first], last) 11 | } 12 | //然后对map进行一次排序 自然语言 13 | for key := range tempMap { 14 | sort.Strings(tempMap[key]) 15 | } 16 | 17 | var dfs func(text string) 18 | dfs = func(text string) { 19 | for{ 20 | if v,ok := tempMap[text]; !ok || len(v) <=0{ 21 | break 22 | } 23 | 24 | tmp:=tempMap[text][0] //当前Start对应的End 25 | tempMap[text] = tempMap[text][1:] //对当前的Start进行一下切片,取第一个的后面 26 | dfs(tmp) 27 | } 28 | res = append(res,text) //对Map操作完以后,将本次的text推入字典 29 | } 30 | 31 | dfs("JFK") 32 | 33 | for i:=0;i < len(res) / 2;i++{ 34 | res[i],res[len(res)-1-i] = res[len(res) -1 -i],res[i] //进行一次头尾调转,将栈倒过来 35 | } 36 | 37 | return res 38 | } 39 | -------------------------------------------------------------------------------- /202008/27/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/reconstruct-itinerary/ 3 | 4 | #### 题目描述 5 | 6 | 给定一个机票的字符串二维数组 `[from, to]`,子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。 7 | 8 | **说明:** 9 | 1. 如果存在多种有效的行程,你可以按字符自然排序返回最小的行程组合。例如,行程 ["JFK", "LGA"] 与 ["JFK", "LGB"] 相比就更小,排序更靠前 10 | 2. 所有的机场都用三个大写字母表示(机场代码)。 11 | 3. 假定所有机票至少存在一种合理的行程。 12 | 13 | **示例 1:** 14 | 15 | ``` 16 | 输入: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]] 17 | 输出: ["JFK", "MUC", "LHR", "SFO", "SJC"] 18 | ``` 19 | 20 | **示例 2:** 21 | 22 | ``` 23 | 输入: [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]] 24 | 输出: ["JFK","ATL","JFK","SFO","ATL","SFO"] 25 | 解释: 另一种有效的行程是 ["JFK","SFO","ATL","JFK","ATL","SFO"]。但是它自然排序更大更靠后。 26 | ``` 27 | 28 | ## 打卡 29 | 30 | - Murph 31 | - Sky-shang -------------------------------------------------------------------------------- /202008/27/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func findItinerary(tickets [][]string) []string { 2 | var ( 3 | m = map[string][]string{} 4 | res []string 5 | ) 6 | 7 | for _, ticket := range tickets { 8 | src, dst := ticket[0], ticket[1] 9 | m[src] = append(m[src], dst) 10 | } 11 | for key := range m { 12 | sort.Strings(m[key]) 13 | } 14 | 15 | var dfs func(curr string) 16 | dfs = func(curr string) { 17 | for { 18 | if v, ok := m[curr]; !ok || len(v) == 0 { 19 | break 20 | } 21 | tmp := m[curr][0] 22 | m[curr] = m[curr][1:] 23 | dfs(tmp) 24 | } 25 | res = append(res, curr) 26 | } 27 | 28 | dfs("JFK") 29 | for i := 0; i < len(res)/2; i++ { 30 | res[i], res[len(res) - 1 - i] = res[len(res) - 1 - i], res[i] 31 | } 32 | return res 33 | } 34 | -------------------------------------------------------------------------------- /202008/28/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool JudgeCircle(string moves) { 3 | int x = 0; 4 | int y = 0; 5 | foreach(char move in moves) 6 | { 7 | switch (move) 8 | { 9 | case 'U': 10 | y--; 11 | break; 12 | case 'D': 13 | y++; 14 | break; 15 | case 'L': 16 | x--; 17 | break; 18 | case 'R': 19 | x++; 20 | break; 21 | } 22 | } 23 | 24 | return x == 0 && y == 0; 25 | } 26 | } -------------------------------------------------------------------------------- /202008/28/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | 2 | var dic = map[string][]int{ 3 | "U": {0, 1}, 4 | "D": {0, -1}, 5 | "R": {-1, 0}, 6 | "L": {1, 0}, 7 | } 8 | 9 | func judgeCircle(moves string) bool { 10 | 11 | if len(moves) <= 0 { 12 | return true 13 | } 14 | 15 | x := 0 16 | y := 0 17 | 18 | for _,n := range moves { 19 | chr := string(n) 20 | println(chr) 21 | if valueArr,ok := dic[chr];ok{ 22 | x+=valueArr[0] 23 | y+=valueArr[1] 24 | } 25 | } 26 | return x == 0 && y == 0 27 | } -------------------------------------------------------------------------------- /202008/28/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/robot-return-to-origin/ 3 | 4 | #### 题目描述 5 | 6 | 在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。 7 | 8 | 移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。 9 | 10 | 注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。 11 | 12 | **示例 1:** 13 | 14 | ``` 15 | 输入: "UD" 16 | 输出: true 17 | 解释:机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。 18 | ``` 19 | 20 | **示例 2:** 21 | 22 | ``` 23 | 输入: "LL" 24 | 输出: false 25 | 解释:机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回原点。 26 | ``` 27 | 28 | ## 打卡 29 | 30 | - Zhuangkh 31 | - Richeir 32 | - CrazyLiuxp 33 | - Murph 34 | - Sky-shang 35 | -------------------------------------------------------------------------------- /202008/28/Richeir/20200828_657.go: -------------------------------------------------------------------------------- 1 | func judgeCircle(moves string) bool { 2 | result := false 3 | 4 | s1 := []rune(moves) 5 | x, y := 0, 0 6 | for i := 0; i < len(s1); i++ { 7 | makeStep(&x, &y, s1[i]) 8 | } 9 | 10 | if x == 0 && y == 0 { 11 | result = true 12 | } 13 | 14 | return result 15 | } 16 | 17 | func makeStep(nowX *int, nowY *int, toward rune) { 18 | switch toward { 19 | case 85: 20 | //U 21 | *nowY++ 22 | case 68: 23 | //D 24 | *nowY-- 25 | case 76: 26 | //L 27 | *nowX-- 28 | case 82: 29 | *nowX++ 30 | } 31 | } -------------------------------------------------------------------------------- /202008/28/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func judgeCircle(moves string) bool { 2 | x, y := 0, 0 3 | length := len(moves) 4 | for i := 0; i < length; i++ { 5 | switch moves[i] { 6 | case 'U': 7 | y-- 8 | case 'D': 9 | y++ 10 | case 'L': 11 | x-- 12 | case 'R': 13 | x++ 14 | } 15 | } 16 | return x == 0 && y == 0 17 | } 18 | -------------------------------------------------------------------------------- /202008/28/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool JudgeCircle (string moves) { 3 | int u = 0, v = 0; 4 | foreach (char c in moves) { 5 | switch (c) { 6 | case 'R': 7 | u++; 8 | break; 9 | case 'L': 10 | u--; 11 | break; 12 | case 'U': 13 | v++; 14 | break; 15 | case 'D': 16 | v--; 17 | break; 18 | } 19 | } 20 | return u == 0 && v == 0; 21 | } 22 | } -------------------------------------------------------------------------------- /202008/28/明洋/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool JudgeCircle(string moves) { 3 | if(moves == null || moves == "") return false; 4 | 5 | int x = 0; 6 | int y = 0; 7 | for(int i = 0; i< moves.Length; i++) 8 | { 9 | if(moves[i] == 'R') x += 1; 10 | if(moves[i] == 'L') x -= 1; 11 | if(moves[i] == 'U') y += 1; 12 | if(moves[i] == 'D') y -= 1; 13 | } 14 | 15 | return x == 0 && y == 0; 16 | } 17 | } -------------------------------------------------------------------------------- /202008/29/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public string ShortestPalindrome(string s) { 3 | int left = 0; 4 | int right = s.Length - 1; 5 | int index = right; 6 | 7 | while (right > left) 8 | { 9 | if (s[left] == s[right]) 10 | { 11 | left++; 12 | right--; 13 | } 14 | else 15 | { 16 | left = 0; 17 | right = index - 1; 18 | index = right; 19 | } 20 | } 21 | 22 | var strArr = s.Substring(index + 1, s.Length - (index + 1)).Reverse().ToArray(); 23 | var str = string.Join("", strArr); 24 | 25 | return str + s; 26 | } 27 | } -------------------------------------------------------------------------------- /202008/29/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/shortest-palindrome/ 3 | 4 | #### 题目描述 5 | 6 | 给定一个字符串 **s**,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。 7 | 8 | 9 | **示例 1:** 10 | 11 | ``` 12 | 输入: "aacecaaa" 13 | 输出: "aaacecaaa" 14 | ``` 15 | 16 | **示例 2:** 17 | 18 | ``` 19 | 输入: "abcd" 20 | 输出: "dcbabcd" 21 | ``` 22 | 23 | ## 打卡 24 | 25 | - CrazyLiuxp 26 | - Sky-shang 27 | -------------------------------------------------------------------------------- /202008/29/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func shortestPalindrome(s string) string { 2 | n := len(s) 3 | base, mod := 131, 1000000007 4 | left, right, mul := 0, 0, 1 5 | best := -1 6 | for i := 0; i < n; i++ { 7 | left = (left * base + int(s[i] - '0')) % mod 8 | right = (right + mul * int(s[i] - '0')) % mod 9 | if left == right { 10 | best = i 11 | } 12 | mul = mul * base % mod 13 | } 14 | add := "" 15 | if best != n - 1 { 16 | add = s[best + 1:] 17 | } 18 | b := []byte(add) 19 | for i := 0; i < len(b) / 2; i++ { 20 | b[i], b[len(b) - 1 -i] = b[len(b) - 1 -i], b[i] 21 | } 22 | return string(b) + s 23 | } 24 | -------------------------------------------------------------------------------- /202008/30/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public string ReverseWords(string s) { 3 | var arr = s.Split(' '); 4 | 5 | List result = new List(); 6 | foreach(var str in arr) 7 | { 8 | result.Add(string.Join("", str.Reverse().ToArray())); 9 | } 10 | 11 | return string.Join(" ", result); 12 | } 13 | } -------------------------------------------------------------------------------- /202008/30/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | func reverseWords(s string) string { 2 | 3 | stringArr:= strings.Split(s," ") 4 | 5 | textArr := []string{} 6 | for _,textTuple := range stringArr{ 7 | 8 | charArr :=[]byte(textTuple) 9 | 10 | len := len(charArr) 11 | for i:=0; i< len/2;i++{ 12 | charArr[i],charArr[(len - i -1)] = charArr[(len-i-1)],charArr[i] 13 | } 14 | textArr=append(textArr, string(charArr)) 15 | } 16 | return strings.Join(textArr," ") 17 | } 18 | -------------------------------------------------------------------------------- /202008/30/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/ 3 | 4 | #### 题目描述 5 | 6 | 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 7 | 8 | **示例 1:** 9 | 10 | ``` 11 | 输入:"Let's take LeetCode contest" 12 | 输出:"s'teL ekat edoCteeL tsetnoc" 13 | ``` 14 | 15 | **提示:** 16 | 17 | - 在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。 18 | 19 | ## 打卡 20 | 21 | - Zhuangkh 22 | - Richeir 23 | - CrazyLiuxp 24 | - Murph 25 | - Sky-shang 26 | -------------------------------------------------------------------------------- /202008/30/Richeir/20200830_557.go: -------------------------------------------------------------------------------- 1 | func reverseWords(s string) string { 2 | result := "" 3 | 4 | s1 := []rune(s) 5 | s1Len := len(s1) 6 | var s2 []rune 7 | 8 | start := 0 9 | end := 0 10 | for i := 0; i < s1Len; i++ { 11 | end = i 12 | if s1[i] == 32 || i == s1Len-1 { 13 | // worldLength := end - start 14 | j := end - 1 15 | if i == s1Len-1 { 16 | j = end 17 | } 18 | 19 | for ; j >= start; j-- { 20 | s2 = append(s2, s1[j]) 21 | } 22 | if i != s1Len-1 { 23 | s2 = append(s2, 32) 24 | } 25 | 26 | start = i + 1 27 | } 28 | } 29 | 30 | result = string(s2) 31 | return result 32 | } -------------------------------------------------------------------------------- /202008/30/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public string ReverseWords(string s) 4 | { 5 | var arr = s.Split(" "); 6 | var result = string.Empty; 7 | for (var index = 0; index < arr.Length; index++) 8 | { 9 | foreach (var item in arr[index].Reverse()) 10 | { 11 | result += item; 12 | } 13 | if (index != arr.Length - 1) result += " "; 14 | } 15 | 16 | return result; 17 | } 18 | } -------------------------------------------------------------------------------- /202008/30/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public string ReverseWords (string s) 4 | { 5 | string result = string.Empty; 6 | string[] str = s.Split (' '); 7 | for (int i = 0, ic = str.Length; i < ic; i++) 8 | { 9 | var array = str[i].ToCharArray (); 10 | Array.Reverse (array); 11 | result += new string (array) + (i == ic - 1 ? "" : " "); 12 | } 13 | return result; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /202008/31/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | private bool[] vis; 3 | private int num; 4 | public bool CanVisitAllRooms(IList> rooms) 5 | { 6 | int len = rooms.Count; 7 | vis = new bool[len]; 8 | num = 0; 9 | 10 | //从第0个房间开始 11 | dfs(rooms, 0); 12 | 13 | return num == len; 14 | } 15 | 16 | private void dfs(IList> rooms, int index) 17 | { 18 | vis[index] = true; 19 | num++; 20 | 21 | foreach(var roomNum in rooms[index]){ 22 | if (!vis[roomNum]) 23 | { 24 | dfs(rooms, roomNum); 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /202008/31/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | var ( 2 | num int 3 | arr []byte 4 | ) 5 | 6 | func canVisitAllRooms(rooms [][]int) bool { 7 | 8 | ln := len(rooms) 9 | num = 0 10 | arr = make([]byte, ln) 11 | 12 | dfs(rooms, 0) 13 | return num == ln 14 | } 15 | 16 | func dfs(node [][]int, x int) { 17 | arr[x] = 1 18 | num++ 19 | 20 | for _, val := range node[x] { 21 | if arr[val] != 1 { 22 | dfs(node, val) 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /202008/31/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/keys-and-rooms/ 3 | 4 | #### 题目描述 5 | 6 | 有 N 个房间,开始时你位于 0 号房间。每个房间有不同的号码:```0,1,2,...,N-1```,并且房间里可能有一些钥匙能使你进入下一个房间。 7 | 8 | 在形式上,对于每个房间 i 都有一个钥匙列表 ```rooms[i]```,每个钥匙 ```rooms[i][j]``` 由 ```[0,1,...,N-1]``` 中的一个整数表示,其中 ```N = rooms.length```。 ```钥匙 rooms[i][j] = v``` 可以打开编号为 ```v``` 的房间。 9 | 10 | 最初,除 ```0``` 号房间外的其余所有房间都被锁住。 11 | 12 | 你可以自由地在房间之间来回走动。 13 | 14 | 如果能进入每个房间返回 ```true```,否则返回 ```false```。 15 | 16 | **示例 1:** 17 | 18 | ``` 19 | 输入: [[1],[2],[3],[]] 20 | 输出: true 21 | 解释: 22 | 我们从 0 号房间开始,拿到钥匙 1。 23 | 之后我们去 1 号房间,拿到钥匙 2。 24 | 然后我们去 2 号房间,拿到钥匙 3。 25 | 最后我们去了 3 号房间。 26 | 由于我们能够进入每个房间,我们返回 true。 27 | ``` 28 | 29 | **示例 2:** 30 | 31 | ``` 32 | 输入:[[1,3],[3,0,1],[2],[0]] 33 | 输出:false 34 | 解释:我们不能进入 2 号房间。 35 | ``` 36 | 37 | **提示:** 38 | 39 | 1. ```1 <= rooms.length <= 1000``` 40 | 2. ```0 <= rooms[i].length <= 1000``` 41 | 3. 所有房间中的钥匙数量总计不超过 ```3000```。 42 | 43 | ## 打卡 44 | 45 | - Zhuangkh 46 | - CrazyLiuxp 47 | - Sky-shang 48 | - Murph 49 | -------------------------------------------------------------------------------- /202008/31/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | var ( 2 | num int 3 | vis []bool 4 | ) 5 | 6 | func canVisitAllRooms(rooms [][]int) bool { 7 | n := len(rooms) 8 | num = 0 9 | vis = make([]bool, n) 10 | dfs(rooms, 0) 11 | return num == n 12 | } 13 | 14 | func dfs(rooms [][]int, x int) { 15 | vis[x] = true 16 | num++ 17 | for _, it := range rooms[x] { 18 | if !vis[it] { 19 | dfs(rooms, it) 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /202008/31/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public bool CanVisitAllRooms(IList> rooms) 4 | { 5 | int rc = rooms.Count; 6 | List keys = new List() { 0 }; 7 | CanVisitAllRooms(rooms, keys, 0); 8 | return keys.Count() == rooms.Count; 9 | } 10 | 11 | public static void CanVisitAllRooms(IList> rooms, List keys, int room) 12 | { 13 | int rc = rooms.Count; 14 | foreach (var i in rooms[room]) 15 | { 16 | if (!keys.Contains(i)) 17 | { 18 | keys.Add(i); 19 | if (keys.Count() == rc) 20 | { 21 | return; 22 | } 23 | 24 | CanVisitAllRooms(rooms, keys, i); 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /202009/01/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool PredictTheWinner(int[] nums) { 3 | if (nums == null || nums.Length < 1) 4 | { 5 | return false; 6 | } 7 | 8 | int n = nums.Length; 9 | int[,] first = new int[n, n]; 10 | int[,] last = new int[n, n]; 11 | for (int r = 0; r < n; r++) 12 | { 13 | first[r, r] = nums[r]; 14 | for (int l = r - 1; l >= 0; l--) 15 | { 16 | first[l, r] = Math.Max(nums[l] + last[l + 1, r], nums[r] + last[l, r - 1]); 17 | last[l, r] = Math.Min(first[l + 1, r], first[l, r - 1]); 18 | } 19 | } 20 | 21 | return first[0, n - 1] >= last[0, n - 1]; 22 | } 23 | } -------------------------------------------------------------------------------- /202009/01/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | func PredictTheWinner(nums []int) bool { 2 | return 0 <= total(nums, 0, len(nums)-1, 1) 3 | } 4 | 5 | func max(x int, y int) int { 6 | if x > y { 7 | return x 8 | } 9 | return y 10 | } 11 | func total(nums []int, x int, y int, turn int) int { 12 | 13 | if x == y { 14 | return nums[x] * turn 15 | } 16 | 17 | left := nums[x]*turn + total(nums, x+1, y, -turn) 18 | right := nums[y]*turn + total(nums, x, y-1, -turn) 19 | 20 | return max(left*turn, right*turn) * turn 21 | } 22 | -------------------------------------------------------------------------------- /202009/01/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/predict-the-winner/ 3 | 4 | #### 题目描述 5 | 6 | 给定一个表示分数的非负整数数组。 玩家 1 从数组任意一端拿取一个分数,随后玩家 2 继续从剩余数组任意一端拿取分数,然后玩家 1 拿,…… 。每次一个玩家只能拿取一个分数,分数被拿取之后不再可取。直到没有剩余分数可取时游戏结束。最终获得分数总和最多的玩家获胜。 7 | 8 | 给定一个表示分数的数组,预测玩家1是否会成为赢家。你可以假设每个玩家的玩法都会使他的分数最大化。 9 | 10 | **示例 1:** 11 | 12 | ``` 13 | 输入:[1, 5, 2] 14 | 输出:False 15 | 解释:一开始,玩家1可以从1和2中进行选择。 16 | 如果他选择 2(或者 1 ),那么玩家 2 可以从 1(或者 2 )和 5 中进行选择。如果玩家 2 选择了 5 ,那么玩家 1 则只剩下 1(或者 2 )可选。 17 | 所以,玩家 1 的最终分数为 1 + 2 = 3,而玩家 2 为 5 。 18 | 因此,玩家 1 永远不会成为赢家,返回 False 。 19 | ``` 20 | 21 | **示例 2:** 22 | 23 | ``` 24 | 输入:[1, 5, 233, 7] 25 | 输出:True 26 | 解释:玩家 1 一开始选择 1 。然后玩家 2 必须从 5 和 7 中进行选择。无论玩家 2 选择了哪个,玩家 1 都可以选择 233 。 27 | 最终,玩家 1(234 分)比玩家 2(12 分)获得更多的分数,所以返回 True,表示玩家 1 可以成为赢家。 28 | ``` 29 | 30 | **提示:** 31 | 32 | - 1 <= 给定的数组长度 <= 20. 33 | - 数组里所有分数都为非负数且不会大于 10000000 。 34 | - 如果最终两个玩家的分数相等,那么玩家 1 仍为赢家。 35 | 36 | ## 打卡 37 | - Zhuangkh 38 | - Sky-shang 39 | - Murph 40 | - CrazyLiuxp 41 | -------------------------------------------------------------------------------- /202009/01/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func PredictTheWinner(nums []int) bool { 2 | return total(nums, 0, len(nums) - 1, 1) >= 0 3 | } 4 | 5 | func total(nums []int, start, end int, turn int) int { 6 | if start == end { 7 | return nums[start] * turn 8 | } 9 | scoreStart := nums[start] * turn + total(nums, start + 1, end, -turn) 10 | scoreEnd := nums[end] * turn + total(nums, start, end - 1, -turn) 11 | return max(scoreStart * turn, scoreEnd * turn) * turn 12 | } 13 | 14 | func max(x, y int) int { 15 | if x > y { 16 | return x 17 | } 18 | return y 19 | } 20 | -------------------------------------------------------------------------------- /202009/01/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public bool PredictTheWinner(int[] nums) 4 | { 5 | return PredictTheWinner(nums, 0, 0, 0, nums.Length - 1, 0); 6 | } 7 | public bool PredictTheWinner(int[] nums, int gradeA, int gradeB, int startIndex, int lastIndex, int curIndex) 8 | { 9 | 10 | if (curIndex < nums.Length) 11 | { 12 | bool left, right; 13 | if (curIndex % 2 == 0) 14 | { 15 | left = PredictTheWinner(nums, gradeA + nums[startIndex], gradeB, startIndex + 1, lastIndex, curIndex + 1); 16 | right = PredictTheWinner(nums, gradeA + nums[lastIndex], gradeB, startIndex, lastIndex - 1, curIndex + 1); 17 | //先手只要任一赢 18 | return left || right; 19 | } 20 | else 21 | { 22 | left = PredictTheWinner(nums, gradeA, gradeB + nums[startIndex], startIndex + 1, lastIndex, curIndex + 1); 23 | right = PredictTheWinner(nums, gradeA, gradeB + nums[lastIndex], startIndex, lastIndex - 1, curIndex + 1); 24 | //后手选择对于先手来说要无论如何赢才可 25 | return left && right; 26 | } 27 | } 28 | 29 | return gradeA >= gradeB; 30 | 31 | } 32 | } 33 | 34 | public class Solution 35 | { 36 | public bool PredictTheWinner(int[] nums) 37 | { 38 | return PredictTheWinner(nums, 0, 0, nums.Length - 1, 1) >= 0; 39 | } 40 | public int PredictTheWinner(int[] nums, int total, int startIndex, int lastIndex, int a) 41 | { 42 | if (startIndex == lastIndex) 43 | { 44 | return nums[startIndex] * a; 45 | } 46 | int left = nums[startIndex] * a + PredictTheWinner(nums, total + nums[startIndex], startIndex + 1, lastIndex, -a); 47 | int right = nums[lastIndex] * a + PredictTheWinner(nums, total + nums[lastIndex], startIndex, lastIndex - 1, -a); 48 | 49 | return Math.Max(left * a, right * a) * a; 50 | } 51 | } -------------------------------------------------------------------------------- /202009/02/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public bool IsNumber(string s) { 3 | if (s == null || s.Length == 0) return false; // s为空对象或 s长度为0(空字符串)时, 不能表示数值 4 | bool isNum = false, isDot = false, ise_or_E = false; // 标记是否遇到数位、小数点、‘e’或'E' 5 | char[] str = s.Trim().ToCharArray(); // 删除字符串头尾的空格,转为字符数组,方便遍历判断每个字符 6 | for (int i = 0; i < str.Length; i++) 7 | { 8 | if (str[i] >= '0' && str[i] <= '9') isNum = true; // 判断当前字符是否为 0~9 的数位 9 | else if (str[i] == '.') 10 | { // 遇到小数点 11 | if (isDot || ise_or_E) return false; // 小数点之前可以没有整数,但是不能重复出现小数点、或出现‘e’、'E' 12 | isDot = true; // 标记已经遇到小数点 13 | } 14 | else if (str[i] == 'e' || str[i] == 'E') 15 | { // 遇到‘e’或'E' 16 | if (!isNum || ise_or_E) return false; // ‘e’或'E'前面必须有整数,且前面不能重复出现‘e’或'E' 17 | ise_or_E = true; // 标记已经遇到‘e’或'E' 18 | isNum = false; // 重置isNum,因为‘e’或'E'之后也必须接上整数,防止出现 123e或者123e+的非法情况 19 | } 20 | else if (str[i] == '-' || str[i] == '+') 21 | { 22 | if (i != 0 && str[i - 1] != 'e' && str[i - 1] != 'E') return false; // 正负号只可能出现在第一个位置,或者出现在‘e’或'E'的后面一个位置 23 | } 24 | else return false; // 其它情况均为不合法字符 25 | } 26 | return isNum; 27 | } 28 | } -------------------------------------------------------------------------------- /202009/02/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/ 3 | 4 | #### 题目描述 5 | 剑指 Offer 20. 表示数值的字符串 6 | 7 | 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100"、"5e2"、"-123"、"3.1416"、"-1E-16"、"0123"都表示数值,但"12e"、"1a3.14"、"1.2.3"、"+-5"及"12e+5.4"都不是。 8 | 9 | ## 打卡 10 | - Sky-shang 11 | - Zhuangkh 12 | - CrazyLiuxp 13 | -------------------------------------------------------------------------------- /202009/02/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | type State int 2 | type CharType int 3 | 4 | const ( 5 | STATE_INITIAL State = iota 6 | STATE_INT_SIGN 7 | STATE_INTEGER 8 | STATE_POINT 9 | STATE_POINT_WITHOUT_INT 10 | STATE_FRACTION 11 | STATE_EXP 12 | STATE_EXP_SIGN 13 | STATE_EXP_NUMBER 14 | STATE_END 15 | ) 16 | 17 | const ( 18 | CHAR_NUMBER CharType = iota 19 | CHAR_EXP 20 | CHAR_POINT 21 | CHAR_SIGN 22 | CHAR_SPACE 23 | CHAR_ILLEGAL 24 | ) 25 | 26 | func toCharType(ch byte) CharType { 27 | switch ch { 28 | case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': 29 | return CHAR_NUMBER 30 | case 'e', 'E': 31 | return CHAR_EXP 32 | case '.': 33 | return CHAR_POINT 34 | case '+', '-': 35 | return CHAR_SIGN 36 | case ' ': 37 | return CHAR_SPACE 38 | default: 39 | return CHAR_ILLEGAL 40 | } 41 | } 42 | 43 | func isNumber(s string) bool { 44 | transfer := map[State]map[CharType]State{ 45 | STATE_INITIAL: map[CharType]State{ 46 | CHAR_SPACE: STATE_INITIAL, 47 | CHAR_NUMBER: STATE_INTEGER, 48 | CHAR_POINT: STATE_POINT_WITHOUT_INT, 49 | CHAR_SIGN: STATE_INT_SIGN, 50 | }, 51 | STATE_INT_SIGN: map[CharType]State{ 52 | CHAR_NUMBER: STATE_INTEGER, 53 | CHAR_POINT: STATE_POINT_WITHOUT_INT, 54 | }, 55 | STATE_INTEGER: map[CharType]State{ 56 | CHAR_NUMBER: STATE_INTEGER, 57 | CHAR_EXP: STATE_EXP, 58 | CHAR_POINT: STATE_POINT, 59 | CHAR_SPACE: STATE_END, 60 | }, 61 | STATE_POINT: map[CharType]State{ 62 | CHAR_NUMBER: STATE_FRACTION, 63 | CHAR_EXP: STATE_EXP, 64 | CHAR_SPACE: STATE_END, 65 | }, 66 | STATE_POINT_WITHOUT_INT: map[CharType]State{ 67 | CHAR_NUMBER: STATE_FRACTION, 68 | }, 69 | STATE_FRACTION: map[CharType]State{ 70 | CHAR_NUMBER: STATE_FRACTION, 71 | CHAR_EXP: STATE_EXP, 72 | CHAR_SPACE: STATE_END, 73 | }, 74 | STATE_EXP: map[CharType]State{ 75 | CHAR_NUMBER: STATE_EXP_NUMBER, 76 | CHAR_SIGN: STATE_EXP_SIGN, 77 | }, 78 | STATE_EXP_SIGN: map[CharType]State{ 79 | CHAR_NUMBER: STATE_EXP_NUMBER, 80 | }, 81 | STATE_EXP_NUMBER: map[CharType]State{ 82 | CHAR_NUMBER: STATE_EXP_NUMBER, 83 | CHAR_SPACE: STATE_END, 84 | }, 85 | STATE_END: map[CharType]State{ 86 | CHAR_SPACE: STATE_END, 87 | }, 88 | } 89 | state := STATE_INITIAL 90 | for i := 0; i < len(s); i++ { 91 | typ := toCharType(s[i]) 92 | if _, ok := transfer[state][typ]; !ok { 93 | return false 94 | } else { 95 | state = transfer[state][typ] 96 | } 97 | } 98 | return state == STATE_INTEGER || state == STATE_POINT || state == STATE_FRACTION || state == STATE_EXP_NUMBER || state == STATE_END 99 | } 100 | -------------------------------------------------------------------------------- /202009/02/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public bool IsNumber (string s) 4 | { 5 | s = s.Trim (); 6 | if (string.IsNullOrEmpty (s)) 7 | { 8 | return false; 9 | } 10 | 11 | Status status = Status.Start; 12 | 13 | foreach (char c in s) 14 | { 15 | if (GetType (c) == CharType.Illegal) 16 | { 17 | return false; 18 | } 19 | switch (status) 20 | { 21 | case Status.Start: 22 | if (GetType (c) == CharType.Symbol) 23 | { 24 | status = Status.Symbol; 25 | } 26 | else if (GetType (c) == CharType.Point) 27 | { 28 | status = Status.StartPoint; 29 | } 30 | else if (GetType (c) == CharType.Number) 31 | { 32 | status = Status.Number; 33 | } 34 | else 35 | { 36 | return false; 37 | } 38 | break; 39 | case Status.Number: 40 | if (GetType (c) == CharType.Number) 41 | { 42 | break; 43 | } 44 | else if (GetType (c) == CharType.E) 45 | { 46 | status = Status.E; 47 | } 48 | else if (GetType (c) == CharType.Point) 49 | { 50 | status = Status.Point; 51 | } 52 | else 53 | { 54 | return false; 55 | } 56 | break; 57 | case Status.Symbol: 58 | if (GetType (c) == CharType.Number) 59 | { 60 | status = Status.Number; 61 | } 62 | else if (GetType (c) == CharType.Point) 63 | { 64 | status = Status.StartPoint; 65 | } 66 | else 67 | { 68 | return false; 69 | } 70 | break; 71 | case Status.Point: 72 | if (GetType (c) == CharType.Number) 73 | { 74 | break; 75 | } 76 | else if (GetType (c) == CharType.E) 77 | { 78 | status = Status.E; 79 | } 80 | else 81 | { 82 | return false; 83 | } 84 | break; 85 | case Status.E: 86 | if (GetType (c) == CharType.Number) 87 | { 88 | status = Status.ENumber; 89 | } 90 | else if (GetType (c) == CharType.Symbol) 91 | { 92 | status = Status.ESymbol; 93 | } 94 | else 95 | { 96 | return false; 97 | } 98 | break; 99 | case Status.ESymbol: 100 | if (GetType (c) == CharType.Number) 101 | { 102 | status = Status.ENumber; 103 | } 104 | else 105 | { 106 | return false; 107 | } 108 | break; 109 | case Status.ENumber: 110 | if (GetType (c) == CharType.Number) 111 | { 112 | break; 113 | } 114 | else 115 | { 116 | return false; 117 | } 118 | break; 119 | case Status.StartPoint: 120 | if (GetType (c) == CharType.Number) 121 | { 122 | status = Status.Point; 123 | 124 | } 125 | else 126 | { 127 | return false; 128 | } 129 | break; 130 | } 131 | 132 | } 133 | 134 | if (status == Status.Symbol || status == Status.ESymbol || status == Status.StartPoint || status == Status.E) 135 | { 136 | return false; 137 | } 138 | 139 | return true; 140 | } 141 | 142 | public enum CharType 143 | { 144 | Number, 145 | E, 146 | Symbol, 147 | Point, 148 | Illegal 149 | } 150 | 151 | public CharType GetType (char c) 152 | { 153 | if ('0' <= c && c <= '9') 154 | { 155 | return CharType.Number; 156 | } 157 | 158 | if (c == '.') 159 | { 160 | return CharType.Point; 161 | } 162 | 163 | if (c == 'E' || c == 'e') 164 | { 165 | return CharType.E; 166 | } 167 | 168 | if (c == '+' || c == '-') 169 | { 170 | return CharType.Symbol; 171 | } 172 | 173 | return CharType.Illegal; 174 | } 175 | public enum Status 176 | { 177 | Start, 178 | StartPoint, 179 | Symbol, 180 | Number, 181 | Point, 182 | E, 183 | ESymbol, 184 | ENumber, 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /202009/03/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/n-queens/ 3 | 4 | #### 题目描述 5 | 51. N 皇后 6 | 7 | n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。 8 | 9 | 10 | 11 | 上图为 8 皇后问题的一种解法。 12 | 13 | 给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。 14 | 15 | 每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。 16 | 17 |   18 | 19 | 示例: 20 | 21 | 输入:4 22 | 输出:[ 23 | [".Q..", // 解法 1 24 | "...Q", 25 | "Q...", 26 | "..Q."], 27 | 28 | ["..Q.", // 解法 2 29 | "Q...", 30 | "...Q", 31 | ".Q.."] 32 | ] 33 | 解释: 4 皇后问题存在两个不同的解法。 34 |   35 | 36 | 提示:皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。 37 | 38 | ## 打卡 39 | - Sky-shang 40 | -------------------------------------------------------------------------------- /202009/03/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | var solutions [][]string 2 | 3 | func solveNQueens(n int) [][]string { 4 | solutions = [][]string{} 5 | queens := make([]int, n) 6 | for i := 0; i < n; i++ { 7 | queens[i] = -1 8 | } 9 | columns := map[int]bool{} 10 | diagonals1, diagonals2 := map[int]bool{}, map[int]bool{} 11 | backtrack(queens, n, 0, columns, diagonals1, diagonals2) 12 | return solutions 13 | } 14 | 15 | func backtrack(queens []int, n, row int, columns, diagonals1, diagonals2 map[int]bool) { 16 | if row == n { 17 | board := generateBoard(queens, n) 18 | solutions = append(solutions, board) 19 | return 20 | } 21 | for i := 0; i < n; i++ { 22 | if columns[i] { 23 | continue 24 | } 25 | diagonal1 := row - i 26 | if diagonals1[diagonal1] { 27 | continue 28 | } 29 | diagonal2 := row + i 30 | if diagonals2[diagonal2] { 31 | continue 32 | } 33 | queens[row] = i 34 | columns[i] = true 35 | diagonals1[diagonal1], diagonals2[diagonal2] = true, true 36 | backtrack(queens, n, row + 1, columns, diagonals1, diagonals2) 37 | queens[row] = -1 38 | delete(columns, i) 39 | delete(diagonals1, diagonal1) 40 | delete(diagonals2, diagonal2) 41 | } 42 | } 43 | 44 | func generateBoard(queens []int, n int) []string { 45 | board := []string{} 46 | for i := 0; i < n; i++ { 47 | row := make([]byte, n) 48 | for j := 0; j < n; j++ { 49 | row[j] = '.' 50 | } 51 | row[queens[i]] = 'Q' 52 | board = append(board, string(row)) 53 | } 54 | return board 55 | } -------------------------------------------------------------------------------- /202009/04/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left; 6 | * public TreeNode right; 7 | * public TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public IList BinaryTreePaths(TreeNode root) 12 | { 13 | var result = new List(); 14 | if (root == null) return result; 15 | var currentPath = string.Empty; 16 | 17 | dfs(root, currentPath, result); 18 | 19 | return result; 20 | } 21 | 22 | private void dfs(TreeNode root, string currentPath, IList result) 23 | { 24 | if (root == null) return; 25 | 26 | if(root.left==null && root.right == null) 27 | { 28 | result.Add(currentPath + root.val); 29 | } 30 | 31 | dfs(root.left, currentPath + root.val + "->", result); 32 | dfs(root.right, currentPath + root.val + "->", result); 33 | } 34 | } -------------------------------------------------------------------------------- /202009/04/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/binary-tree-paths/ 3 | 4 | #### 题目描述 5 | 257. 二叉树的所有路径 6 | 7 | 8 | 9 | 给定一个二叉树,返回所有从根节点到叶子节点的路径。 10 | 11 | **说明**: 叶子节点是指没有子节点的节点。 12 | 13 |   14 | 15 | 示例: 16 | 17 | ``` 18 | 输入: 19 | 20 | 1 21 | / \ 22 | 2 3 23 | \ 24 | 5 25 | 26 | 输出: ["1->2->5", "1->3"] 27 | 28 | 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3 29 | ``` 30 | 31 | ## 打卡 32 | - CrazyLiuxp 33 | - Sky-shang 34 | -------------------------------------------------------------------------------- /202009/04/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | var paths []string 2 | 3 | func binaryTreePaths(root *TreeNode) []string { 4 | paths = []string{} 5 | constructPaths(root, "") 6 | return paths 7 | } 8 | 9 | func constructPaths(root *TreeNode, path string) { 10 | if root != nil { 11 | pathSB := path 12 | pathSB += strconv.Itoa(root.Val) 13 | if root.Left == nil && root.Right == nil { 14 | paths = append(paths, pathSB) 15 | } else { 16 | pathSB += "->" 17 | constructPaths(root.Left, pathSB) 18 | constructPaths(root.Right, pathSB) 19 | } 20 | } 21 | } 22 | 。 -------------------------------------------------------------------------------- /202009/05/CrazyLiuxp/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | private int jiechen(int n) 3 | { 4 | if (n <= 1) return n; 5 | return n * jiechen(n - 1); 6 | } 7 | public string GetPermutation(int n, int k) 8 | { 9 | List leftlist = new List(); 10 | for (int i = 1; i <= n; i++) 11 | leftlist.Add(i); 12 | //leftlist剩余需要找的个数 13 | string forreturn = ""; 14 | while (leftlist.Count > 1) 15 | { 16 | int jiechenval = jiechen(n - 1); 17 | double shang = k * 1.0 / jiechenval; 18 | if (shang % 1 != 0) 19 | shang = 1 + Math.Truncate(shang); 20 | int removeindex = (int)shang - 1; 21 | forreturn += leftlist[removeindex]; 22 | leftlist.RemoveAt(removeindex); 23 | k = k - removeindex * jiechenval; 24 | n--; 25 | } 26 | forreturn += leftlist[0].ToString(); 27 | return forreturn; 28 | } 29 | } -------------------------------------------------------------------------------- /202009/05/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/permutation-sequence/ 3 | 4 | #### 题目描述 5 | 60. 第k个排列 6 | 7 | 8 | 9 | 给出集合 `[1,2,3,…,n]`,其所有元素共有 n! 种排列。 10 | 11 | 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: 12 | 1. `"123"` 13 | 2. `"132"` 14 | 3. `"213"` 15 | 4. `"231"` 16 | 5. `"312"` 17 | 6. `"321"` 18 | 19 | 给定 *n* 和 *k*,返回第 *k* 个排列。 20 | 21 | **说明:** 22 | 23 | - 给定 *n* 的范围是 [1, 9]。 24 | - 给定 *k* 的范围是[1, *n*!]。 25 | 26 | 27 | 示例1: 28 | 29 | ``` 30 | 输入: n = 3, k = 3 31 | 输出: "213" 32 | ``` 33 | 34 | 实例2: 35 | 36 | ``` 37 | 输入: n = 4, k = 9 38 | 输出: "2314" 39 | ``` 40 | 41 | 42 | 43 | ## 打卡 44 | 45 | - CrazyLiuxp 46 | - Sky-shang 47 | -------------------------------------------------------------------------------- /202009/05/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func getPermutation(n int, k int) string { 2 | factorial := make([]int, n) 3 | factorial[0] = 1 4 | for i := 1; i < n; i++ { 5 | factorial[i] = factorial[i - 1] * i 6 | } 7 | k-- 8 | 9 | ans := "" 10 | valid := make([]int, n + 1) 11 | for i := 0; i < len(valid); i++ { 12 | valid[i] = 1 13 | } 14 | for i := 1; i <= n; i++ { 15 | order := k / factorial[n - i] + 1 16 | for j := 1; j <= n; j++ { 17 | order -= valid[j] 18 | if order == 0 { 19 | ans += strconv.Itoa(j) 20 | valid[j] = 0 21 | break 22 | } 23 | } 24 | k %= factorial[n - i] 25 | } 26 | return ans 27 | } 28 | -------------------------------------------------------------------------------- /202009/06/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/ 3 | 4 | #### 题目描述 5 | 6 | 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 7 | 8 | 例如: 9 | 给定二叉树 ```[3,9,20,null,null,15,7]```, 10 | 11 | ``` 12 | 3 13 | / \ 14 | 9 20 15 | / \ 16 | 15 7 17 | ``` 18 | 19 | 返回其自底向上的层次遍历为: 20 | 21 | ``` 22 | [ 23 | [15,7], 24 | [9,20], 25 | [3] 26 | ] 27 | ``` 28 | 29 | ## 打卡 30 | 31 | - Zhuangkh 32 | - Sky-shang -------------------------------------------------------------------------------- /202009/06/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func levelOrderBottom(root *TreeNode) [][]int { 2 | levelOrder := [][]int{} 3 | if root == nil { 4 | return levelOrder 5 | } 6 | queue := []*TreeNode{} 7 | queue = append(queue, root) 8 | for len(queue) > 0 { 9 | level := []int{} 10 | size := len(queue) 11 | for i := 0; i < size; i++ { 12 | node := queue[0] 13 | queue = queue[1:] 14 | level = append(level, node.Val) 15 | if node.Left != nil { 16 | queue = append(queue, node.Left) 17 | } 18 | if node.Right != nil { 19 | queue = append(queue, node.Right) 20 | } 21 | } 22 | levelOrder = append(levelOrder, level) 23 | } 24 | for i := 0; i < len(levelOrder) / 2; i++ { 25 | levelOrder[i], levelOrder[len(levelOrder) - 1 - i] = levelOrder[len(levelOrder) - 1 - i], levelOrder[i] 26 | } 27 | return levelOrder 28 | } -------------------------------------------------------------------------------- /202009/06/Zhuangkh/Solution.cs: -------------------------------------------------------------------------------- 1 | public class Solution 2 | { 3 | public IList> LevelOrderBottom (TreeNode root) 4 | { 5 | List> result = new List> (); 6 | LevelOrderBottom (result, root, 0); 7 | result.Reverse (); 8 | return result; 9 | } 10 | 11 | public void LevelOrderBottom (List> result, TreeNode node, int index) 12 | { 13 | if (node != null) 14 | { 15 | if (result.Count () > index) 16 | { 17 | result[index].Add (node.val); 18 | } 19 | else 20 | { 21 | result.Add (new List () { node.val }); 22 | } 23 | LevelOrderBottom (result, node.left, index + 1); 24 | LevelOrderBottom (result, node.right, index + 1); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /202009/07/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | func topKFrequent(nums []int, k int) []int { 2 | 3 | var tempMap = make(map[int]int) 4 | var n = make([]int, 0) 5 | for _, v := range nums { 6 | if i, ok := tempMap[v]; ok { 7 | tempMap[v] = i + 1 8 | } else { 9 | tempMap[v] = 1 10 | n = append(n, v) 11 | } 12 | } 13 | 14 | sort.Slice(n, func(i, j int) bool { 15 | return tempMap[n[i]] > tempMap[n[j]] 16 | }) 17 | 18 | return n[:k] 19 | } -------------------------------------------------------------------------------- /202009/07/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/top-k-frequent-elements/ 3 | 4 | #### 题目描述 5 | 6 | 给定一个非空的整数数组,返回其中出现频率前 k 高的元素。 7 | 8 | 9 | 示例1: 10 | 11 | ``` 12 | 输入: nums = [1,1,1,2,2,3], k = 2 13 | 输出: [1,2] 14 | ``` 15 | 16 | 示例2: 17 | ``` 18 | 输入: nums = [1], k = 1 19 | 输出: [1] 20 | ``` 21 | 22 | 提示: 23 | 24 | - 你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。 25 | - 你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。 26 | - 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的。 27 | - 你可以按任意顺序返回答案。 28 | 29 | 30 | 31 | ## 打卡 32 | 33 | - Murph 34 | - Sky-shang -------------------------------------------------------------------------------- /202009/07/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func topKFrequent(nums []int, k int) []int { 2 | occurrences := map[int]int{} 3 | for _, num := range nums { 4 | occurrences[num]++ 5 | } 6 | h := &IHeap{} 7 | heap.Init(h) 8 | for key, value := range occurrences { 9 | heap.Push(h, [2]int{key, value}) 10 | if h.Len() > k { 11 | heap.Pop(h) 12 | } 13 | } 14 | ret := make([]int, k) 15 | for i := 0; i < k; i++ { 16 | ret[k - i - 1] = heap.Pop(h).([2]int)[0] 17 | } 18 | return ret 19 | } 20 | 21 | type IHeap [][2]int 22 | 23 | func (h IHeap) Len() int { return len(h) } 24 | func (h IHeap) Less(i, j int) bool { return h[i][1] < h[j][1] } 25 | func (h IHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 26 | 27 | func (h *IHeap) Push(x interface{}) { 28 | *h = append(*h, x.([2]int)) 29 | } 30 | 31 | func (h *IHeap) Pop() interface{} { 32 | old := *h 33 | n := len(old) 34 | x := old[n-1] 35 | *h = old[0 : n-1] 36 | return x 37 | } 38 | -------------------------------------------------------------------------------- /202009/08/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | var ( 2 | arr [][]int 3 | s int 4 | e int 5 | ) 6 | 7 | func combine(n int, k int) [][]int { 8 | arr = [][]int{} 9 | s=n 10 | e=k 11 | dfs(1,[]int{}) 12 | return arr 13 | } 14 | 15 | func dfs(i int,tempArr []int){ 16 | if len(tempArr) == e{ 17 | tmp := make([]int, e) 18 | copy(tmp, tempArr) 19 | arr = append(arr, tmp) 20 | return 21 | } 22 | if i > s{ 23 | return 24 | } 25 | dfs(i+1,tempArr) 26 | dfs(i+1,append(tempArr,i)) 27 | } -------------------------------------------------------------------------------- /202009/08/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/combinations/ 3 | 4 | #### 题目描述 5 | 6 | 77. 组合 7 | 8 | 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 9 | 10 | 示例: 11 | 12 | 输入: n = 4, k = 2 13 | 输出: 14 | [ 15 | [2,4], 16 | [3,4], 17 | [2,3], 18 | [1,2], 19 | [1,3], 20 | [1,4], 21 | ] 22 | 23 | 24 | 25 | ## 打卡 26 | 27 | - Sky-shang 28 | - Murph 29 | -------------------------------------------------------------------------------- /202009/08/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func combine(n int, k int) (ans [][]int) { 2 | temp := []int{} 3 | var dfs func(int) 4 | dfs = func(cur int) { 5 | // 剪枝:temp 长度加上区间 [cur, n] 的长度小于 k,不可能构造出长度为 k 的 temp 6 | if len(temp) + (n - cur + 1) < k { 7 | return 8 | } 9 | // 记录合法的答案 10 | if len(temp) == k { 11 | comb := make([]int, k) 12 | copy(comb, temp) 13 | ans = append(ans, comb) 14 | return 15 | } 16 | // 考虑选择当前位置 17 | temp = append(temp, cur) 18 | dfs(cur + 1) 19 | temp = temp[:len(temp)-1] 20 | // 考虑不选择当前位置 21 | dfs(cur + 1) 22 | } 23 | dfs(1) 24 | return 25 | } -------------------------------------------------------------------------------- /202009/09/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/combination-sum/ 3 | 4 | #### 题目描述 5 | 6 | 39. 组合总和 7 | 8 | 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 9 | 10 | candidates 中的数字可以无限制重复被选取。 11 | 12 | 说明: 13 | 14 | 所有数字(包括 target)都是正整数。 15 | 解集不能包含重复的组合。  16 | 示例 1: 17 | 18 | 输入:candidates = [2,3,6,7], target = 7, 19 | 所求解集为: 20 | [ 21 | [7], 22 | [2,2,3] 23 | ] 24 | 示例 2: 25 | 26 | 输入:candidates = [2,3,5], target = 8, 27 | 所求解集为: 28 | [ 29 |   [2,2,2,2], 30 |   [2,3,3], 31 |   [3,5] 32 | ] 33 |   34 | 35 | 提示: 36 | 37 | 1 <= candidates.length <= 30 38 | 1 <= candidates[i] <= 200 39 | candidate 中的每个元素都是独一无二的。 40 | 1 <= target <= 500 41 | 42 | ## 打卡 43 | 44 | - Sky-shang 45 | -------------------------------------------------------------------------------- /202009/09/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func combinationSum(candidates []int, target int) (ans [][]int) { 2 | comb := []int{} 3 | var dfs func(target, idx int) 4 | dfs = func(target, idx int) { 5 | if idx == len(candidates) { 6 | return 7 | } 8 | if target == 0 { 9 | ans = append(ans, append([]int(nil), comb...)) 10 | return 11 | } 12 | // 直接跳过 13 | dfs(target, idx+1) 14 | // 选择当前数 15 | if target-candidates[idx] >= 0 { 16 | comb = append(comb, candidates[idx]) 17 | dfs(target-candidates[idx], idx) 18 | comb = comb[:len(comb)-1] 19 | } 20 | } 21 | dfs(target, 0) 22 | return 23 | } -------------------------------------------------------------------------------- /202009/10/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/combination-sum-ii/ 3 | 4 | #### 题目描述 5 | 6 | 40. 组合总和 II 7 | 8 | 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 9 | 10 | candidates 中的每个数字在每个组合中只能使用一次。 11 | 12 | 说明: 13 | 14 | 所有数字(包括目标数)都是正整数。 15 | 解集不能包含重复的组合。  16 | 示例 1: 17 | 18 | 输入: candidates = [10,1,2,7,6,1,5], target = 8, 19 | 所求解集为: 20 | [ 21 | [1, 7], 22 | [1, 2, 5], 23 | [2, 6], 24 | [1, 1, 6] 25 | ] 26 | 示例 2: 27 | 28 | 输入: candidates = [2,5,2,1,2], target = 5, 29 | 所求解集为: 30 | [ 31 |   [1,2,2], 32 |   [5] 33 | ] 34 | 35 | 36 | ## 打卡 37 | 38 | - Sky-shang 39 | -------------------------------------------------------------------------------- /202009/10/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func combinationSum2(candidates []int, target int) (ans [][]int) { 2 | sort.Ints(candidates) 3 | var freq [][2]int 4 | for _, num := range candidates { 5 | if freq == nil || num != freq[len(freq)-1][0] { 6 | freq = append(freq, [2]int{num, 1}) 7 | } else { 8 | freq[len(freq)-1][1]++ 9 | } 10 | } 11 | 12 | var sequence []int 13 | var dfs func(pos, rest int) 14 | dfs = func(pos, rest int) { 15 | if rest == 0 { 16 | ans = append(ans, append([]int(nil), sequence...)) 17 | return 18 | } 19 | if pos == len(freq) || rest < freq[pos][0] { 20 | return 21 | } 22 | 23 | dfs(pos+1, rest) 24 | 25 | most := min(rest/freq[pos][0], freq[pos][1]) 26 | for i := 1; i <= most; i++ { 27 | sequence = append(sequence, freq[pos][0]) 28 | dfs(pos+1, rest-i*freq[pos][0]) 29 | } 30 | sequence = sequence[:len(sequence)-most] 31 | } 32 | dfs(0, target) 33 | return 34 | } 35 | 36 | func min(a, b int) int { 37 | if a < b { 38 | return a 39 | } 40 | return b 41 | } 42 | 43 | -------------------------------------------------------------------------------- /202009/11/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/combination-sum-iii/ 3 | 4 | #### 题目描述 5 | 6 | 216. 组合总和 III 7 | 8 | 找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。 9 | 10 | 说明: 11 | 12 | 所有数字都是正整数。 13 | 解集不能包含重复的组合。  14 | 示例 1: 15 | 16 | 输入: k = 3, n = 7 17 | 输出: [[1,2,4]] 18 | 示例 2: 19 | 20 | 输入: k = 3, n = 9 21 | 输出: [[1,2,6], [1,3,5], [2,3,4]] 22 | 23 | 24 | ## 打卡 25 | 26 | - Sky-shang 27 | -------------------------------------------------------------------------------- /202009/11/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func combinationSum3(k int, n int) (ans [][]int) { 2 | var temp []int 3 | check := func(mask int) bool { 4 | temp = nil 5 | sum := 0 6 | for i := 0; i < 9; i++ { 7 | if 1< 0 { 8 | temp = append(temp, i+1) 9 | sum += i + 1 10 | } 11 | } 12 | return len(temp) == k && sum == n 13 | } 14 | 15 | for mask := 0; mask < 1<<9; mask++ { 16 | if check(mask) { 17 | ans = append(ans, append([]int(nil), temp...)) 18 | } 19 | } 20 | return 21 | } 22 | -------------------------------------------------------------------------------- /202009/12/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/submissions/ 3 | 4 | #### 题目描述 5 | 6 | 637. 二叉树的层平均值 7 | 8 | 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。 9 | 10 |   11 | 12 | 示例 1: 13 | 14 | 输入: 15 | 3 16 | / \ 17 | 9 20 18 | / \ 19 | 15 7 20 | 输出:[3, 14.5, 11] 21 | 解释: 22 | 第 0 层的平均值是 3 , 第1层是 14.5 , 第2层是 11 。因此返回 [3, 14.5, 11] 。 23 |   24 | 25 | 提示: 26 | 27 | 节点值的范围在32位有符号整数范围内。 28 | 29 | 30 | ## 打卡 31 | 32 | - Sky-shang 33 | -------------------------------------------------------------------------------- /202009/12/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | type data struct{ sum, count int } 2 | 3 | func averageOfLevels(root *TreeNode) []float64 { 4 | levelData := []data{} 5 | var dfs func(node *TreeNode, level int) 6 | dfs = func(node *TreeNode, level int) { 7 | if node == nil { 8 | return 9 | } 10 | if level < len(levelData) { 11 | levelData[level].sum += node.Val 12 | levelData[level].count++ 13 | } else { 14 | levelData = append(levelData, data{node.Val, 1}) 15 | } 16 | dfs(node.Left, level+1) 17 | dfs(node.Right, level+1) 18 | } 19 | dfs(root, 0) 20 | 21 | averages := make([]float64, len(levelData)) 22 | for i, d := range levelData { 23 | averages[i] = float64(d.sum) / float64(d.count) 24 | } 25 | return averages 26 | } 27 | -------------------------------------------------------------------------------- /202009/13/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/word-search/ 3 | 4 | #### 题目描述 5 | 6 | 79. 单词搜索 7 | 8 | 给定一个二维网格和一个单词,找出该单词是否存在于网格中。 9 | 10 | 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。 11 | 12 |   13 | 14 | 示例: 15 | 16 | board = 17 | [ 18 | ['A','B','C','E'], 19 | ['S','F','C','S'], 20 | ['A','D','E','E'] 21 | ] 22 | 23 | 给定 word = "ABCCED", 返回 true 24 | 给定 word = "SEE", 返回 true 25 | 给定 word = "ABCB", 返回 false 26 |   27 | 28 | 提示: 29 | 30 | board 和 word 中只包含大写和小写英文字母。 31 | 1 <= board.length <= 200 32 | 1 <= board[i].length <= 200 33 | 1 <= word.length <= 10^3 34 | 35 | ## 打卡 36 | 37 | - Sky-shang 38 | -------------------------------------------------------------------------------- /202009/13/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | type pair struct{ x, y int } 2 | 3 | var directions = []pair{{-1, 0}, {1, 0}, {0, -1}, {0, 1}} // 上下左右 4 | 5 | func exist(board [][]byte, word string) bool { 6 | h, w := len(board), len(board[0]) 7 | vis := make([][]bool, h) 8 | for i := range vis { 9 | vis[i] = make([]bool, w) 10 | } 11 | var check func(i, j, k int) bool 12 | check = func(i, j, k int) bool { 13 | if board[i][j] != word[k] { // 剪枝:当前字符不匹配 14 | return false 15 | } 16 | if k == len(word)-1 { // 单词存在于网格中 17 | return true 18 | } 19 | vis[i][j] = true 20 | defer func() { vis[i][j] = false }() // 回溯时还原已访问的单元格 21 | for _, dir := range directions { 22 | if newI, newJ := i+dir.x, j+dir.y; 0 <= newI && newI < h && 0 <= newJ && newJ < w && !vis[newI][newJ] { 23 | if check(newI, newJ, k+1) { 24 | return true 25 | } 26 | } 27 | } 28 | return false 29 | } 30 | for i, row := range board { 31 | for j := range row { 32 | if check(i, j, 0) { 33 | return true 34 | } 35 | } 36 | } 37 | return false 38 | } 39 | -------------------------------------------------------------------------------- /202009/14/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 3 | 4 | #### 题目描述 5 | 6 | 94. 二叉树的中序遍历 7 | 8 | 给定一个二叉树,返回它的中序 遍历。 9 | 10 | 示例: 11 | 12 | 输入: [1,null,2,3] 13 | 1 14 | \ 15 | 2 16 | / 17 | 3 18 | 19 | 输出: [1,3,2] 20 | 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 21 | 22 | ## 打卡 23 | 24 | - Sky-shang 25 | -------------------------------------------------------------------------------- /202009/14/Sky-shang/Solution.go: -------------------------------------------------------------------------------- 1 | func inorderTraversal(root *TreeNode) (res []int) { 2 | var inorder func(node *TreeNode) 3 | inorder = func(node *TreeNode) { 4 | if node == nil { 5 | return 6 | } 7 | inorder(node.Left) 8 | res = append(res, node.Val) 9 | inorder(node.Right) 10 | } 11 | inorder(root) 12 | return 13 | } 14 | -------------------------------------------------------------------------------- /202009/16/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * type TreeNode struct { 4 | * Val int 5 | * Left *TreeNode 6 | * Right *TreeNode 7 | * } 8 | */ 9 | func invertTree(root *TreeNode) *TreeNode { 10 | if root == nil { 11 | return nil 12 | } 13 | 14 | left :=invertTree(root.Left) 15 | right := invertTree(root.Right) 16 | 17 | root.Left = right 18 | root.Right = left 19 | return root 20 | 21 | } 22 | 23 | -------------------------------------------------------------------------------- /202009/16/READMD.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/invert-binary-tree/ 3 | 4 | #### 题目描述 5 | 6 | 226. 翻转二叉树 7 | 8 | 翻转一棵二叉树。 9 | 10 | 示例: 11 | 12 | 输入: 13 | 14 | ``` 15 | 4 16 | / \ 17 | 2 7 18 | / \ / \ 19 | 1 3 6 9 20 | 21 | ``` 22 | 23 | 输出: 24 | 25 | ``` 26 | 4 27 | / \ 28 | 7 2 29 | / \ / \ 30 | 9 6 3 1 31 | 32 | ``` 33 | ## 打卡 34 | - Murph 35 | -------------------------------------------------------------------------------- /202009/19/Murph/Solution.go: -------------------------------------------------------------------------------- 1 | func sumOfLeftLeaves(root *TreeNode) int { 2 | if root == nil { 3 | return 0 4 | } 5 | value := sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right) 6 | if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil { 7 | return value + root.Left.Val 8 | } 9 | return value 10 | } -------------------------------------------------------------------------------- /202009/19/README.md: -------------------------------------------------------------------------------- 1 | ## 每日一题 2 | https://leetcode-cn.com/problems/sum-of-left-leaves/ 3 | 4 | #### 题目描述 5 | 6 | ``` 7 | 8 | 计算给定二叉树的所有左叶子之和。 9 | 10 | 示例: 11 | 12 | 3 13 | / \ 14 | 9 20 15 | / \ 16 | 15 7 17 | 18 | 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 19 | 20 | ``` 21 | 22 | ## 打卡 23 | - Murph 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # leetcode 2 | 3 | # 关于自动提交 4 | 5 | 在您创建Pull Requests时,我们会自动合并您的pr.如果您的pr尚未完全开发完毕,不想立刻合并.请您在pr的标题中,添加`[!WIP]` --------------------------------------------------------------------------------