From e191be962e1e6a67cab9a423d19629effeb4997b Mon Sep 17 00:00:00 2001 From: MYP Date: Tue, 27 Oct 2020 18:32:46 +0800 Subject: [PATCH] =?UTF-8?q?Create=2014=20Leetcode=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E7=BB=83=E4=B9=A0=EF=BC=88=E4=BA=94=EF=BC=89.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../14 Leetcode同步练习(五).md | 1114 +++++++++++++++++ 1 file changed, 1114 insertions(+) create mode 100644 DataStructureAndAlgorithm/14 Leetcode同步练习(五).md diff --git a/DataStructureAndAlgorithm/14 Leetcode同步练习(五).md b/DataStructureAndAlgorithm/14 Leetcode同步练习(五).md new file mode 100644 index 0000000..3c02b54 --- /dev/null +++ b/DataStructureAndAlgorithm/14 Leetcode同步练习(五).md @@ -0,0 +1,1114 @@ +# Leetcodeͬϰ壩 +## Ŀ¼ +- Ŀ01ջʵֶ +- Ŀ02ľ +- Ŀ03ת +- Ŀ04ǰ׺ +- Ŀ05תַ +- Ŀ06ظַӴ +- Ŀ07Ӵ +- Ŀ08ַ +- Ŀ09ʽƥ +- Ŀ10ͨƥ + + +--- +## Ŀ01ջʵֶ + +> - ţ232 +> - Ѷȣ +> - https://leetcode-cn.com/problems/implement-queue-using-stacks/ + +ʹջʵֶев + +- `push(x)` -- һԪطеβ +- `pop()` -- ӶײƳԪء +- `peek()` -- ضײԪء +- `empty()` -- ضǷΪա + + +ʾ: + +```c +MyQueue queue = new MyQueue(); + +queue.push(1); +queue.push(2); +queue.peek(); // 1 +queue.pop(); // 1 +queue.empty(); // false +``` + +˵: + +- ֻʹñ׼ջ -- Ҳֻpush to toppeek/pop from topsize is empty ǺϷġ +- ʹõҲ֧ջʹ`list``deque`˫˶УģһջֻҪDZ׼ջɡ +- вЧģ磬һյĶв`pop``peek` + +**ο룺** + +- ִнͨ +- ִʱ112 ms, C# ύл 95.10% û +- ڴģ24.3 MB, C# ύл 5.33% û + +```c +public class MyQueue +{ + private Stack _stack = new Stack(); + + /** Initialize your data structure here. */ + public MyQueue() + { + ; + } + + /** Push element x to the back of queue. */ + public void Push(int x) + { + if (_stack.Count == 0) + { + _stack.Push(x); + return; + } + + Stack temp = new Stack(); + while (_stack.Count != 0) + { + temp.Push(_stack.Pop()); + } + _stack.Push(x); + while (temp.Count != 0) + { + _stack.Push(temp.Pop()); + } + } + + /** Removes the element from in front of queue and returns that element. */ + public int Pop() + { + return _stack.Count > 0 ? _stack.Pop() : -1; + } + + /** Get the front element. */ + public int Peek() + { + return _stack.Count > 0 ? _stack.Peek() : -1; + } + + /** Returns whether the queue is empty. */ + public bool Empty() + { + return _stack.Count == 0; + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.Push(x); + * int param_2 = obj.Pop(); + * int param_3 = obj.Peek(); + * bool param_4 = obj.Empty(); + */ +``` + +--- +## Ŀ02ľ + +> - ţ766 +> - Ѷȣ +> - https://leetcode-cn.com/problems/toeplitz-matrix/ + + +ÿһϵµĶԽϵԪضͬô ľ + +һ`M x N`ľ󣬵ҽľʱ`True` + +**ʾ 1:** + +```c +: +matrix = [ + [1,2,3,4], + [5,1,2,3], + [9,5,1,2] +] +: True +: +, ԽΪ: +"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]" +ԽϵԪؾͬ, ˴True +``` + +**ʾ 2:** + +```c +: +matrix = [ + [1,2], + [2,2] +] +: False +: +Խ"[1, 2]"ϵԪزͬ +``` + +**˵:** + +1. `matrix`һĶά顣 +2. `matrix` [1, 20]Χڡ +3. `matrix[i][j]` [0, 99]Χڡ + + +**:** + +1. 洢ڴϣҴڴ޵ģһֻܽһоصڴУô죿 +2. ֻ̫һνмصڴУô죿 + +**˼·**жԽϵԪض$a_{11} = a_{00}, a_{22} = a_{11}, a_{kk} = a_{k-1k-1}$ڶԽϵԪ˵ǰԪزǵһֵԪأôǰԪһڵǰԪصϽǡƳλ$(r, c)$ϵԪأֻҪ`r == 0 OR c == 0 OR matrix[r-1][c-1] == matrix[r][c]`Ϳˡ + +**ο** + +- ִнͨ +- ִʱ128 ms, C# ύл 47.50% û +- ڴģ27.5 MB, C# ύл 16.13% û + +```c +public class Solution +{ + public bool IsToeplitzMatrix(int[][] matrix) + { + for (int i = 1; i < matrix.Length; i++) + { + int[] row = matrix[i]; + for (int j = 1; j < row.Length; j++) + { + if (matrix[i][j] != matrix[i - 1][j - 1]) + return false; + } + } + return true; + } +} +``` + + +--- +## Ŀ03ת + +> - ţ13 +> - Ѷȣ +> - https://leetcode-cn.com/problems/roman-to-integer/ + +ְַ: `I V X LCD``M` + +``` +ַ ֵ +I 1 +V 5 +X 10 +L 50 +C 100 +D 500 +M 1000 +``` + +磬 2 д`II`Ϊе 112 д`XII`Ϊ`X + II` 27 д`XXVII`, Ϊ`XX + V + II` + + +ͨ£СڴֵұߡҲ 4 д`IIII``IV` 1 5 ߣʾڴ 5 С 1 õֵ 4 ͬأ 9 ʾΪ`IX`Ĺֻ + +``` +I Է V (5) X (10) ߣʾ 4 9 +X Է L (50) C (100) ߣʾ 40 90 +C Է D (500) M (1000) ߣʾ 400 900 +``` + +һ֣תȷ 1 3999 ķΧڡ + +**ʾ 1:** + +```c +:"III" +: 3 +``` + +**ʾ 2:** + +```c +: "IV" +: 4 +``` + +**ʾ 3:** +```c +: "IX" +: 9 +``` + +**ʾ 4:** +```c +: "LVIII" +: 58 +: L = 50, V= 5, III = 3. +``` + +**ʾ 5:** +```c +: "MCMXCIV" +: 1994 +: M = 1000, CM = 900, XC = 90, IV = 4. +``` + +**˼·**ֱӷݡСڴֵұߡԼĹֱȥд롣 + +**ο** + +- ִнͨ +- ִʱ96 ms, C# ύл 95.88% û +- ڴģ25.7 MB, C# ύл 5.27% û + +```c +public class Solution +{ + public int RomanToInt(string s) + { + int result = 0; + int count = s.Length; + int i = 0; + while (i < count) + { + char c = s[i]; + int move = 0; + switch (c) + { + case 'I': + result += PreI(i, s, ref move); + break; + case 'V': + result += 5; + move = 1; + break; + case 'X': + result += PreX(i, s, ref move); + break; + case 'L': + result += 50; + move = 1; + break; + case 'C': + result += PreC(i, s, ref move); + break; + case 'D': + result += 500; + move = 1; + break; + case 'M': + result += 1000; + move = 1; + break; + } + i += move; + } + return result; + } + private int PreI(int index, string s, ref int move) + { + //I 1 + //IV 4 + //IX 9 + //II 2 + int result = 0; + int count = s.Length; + if (index + 1 < count) + { + char c = s[index + 1]; + switch (c) + { + case 'V': + result = 4; + move = 2; + break; + case 'X': + result = 9; + move = 2; + break; + case 'I': + result = 2; + move = 2; + break; + } + } + else + { + result = 1; + move = 1; + } + return result; + } + + private int PreX(int index, string s, ref int move) + { + //X 10 + //XL 40 + //XC 90 + int result = 0; + int count = s.Length; + if (index + 1 < count) + { + char c = s[index + 1]; + switch (c) + { + case 'L': + result = 40; + move = 2; + break; + case 'C': + result = 90; + move = 2; + break; + default: + result = 10; + move = 1; + break; + } + } + else + { + result = 10; + move = 1; + } + return result; + } + + private int PreC(int index, string s, ref int move) + { + //C 100 + //CD 400 + //CM 900 + int result = 0; + int count = s.Length; + if (index + 1 < count) + { + char c = s[index + 1]; + switch (c) + { + case 'D': + result = 400; + move = 2; + break; + case 'M': + result = 900; + move = 2; + break; + default: + result = 100; + move = 1; + break; + } + } + else + { + result = 100; + move = 1; + } + return result; + } +} +``` + +--- +## Ŀ04ǰ׺ + +> - ţ14 +> - Ѷȣ +> - https://leetcode-cn.com/problems/longest-common-prefix/ + +дһַеǰ׺ + +ڹǰ׺ؿַ "" + +ʾ 1: +```c +: ["flower","flow","flight"] +: "fl" +``` + +ʾ 2: +```c +: ["dog","racecar","car"] +: "" +: 벻ڹǰ׺ +``` + +˵: + +ֻСдĸ a-z + + +**ο** + +- ״̬ͨ +- ִʱ: 144 ms, C# ύл 94.92% û +- ڴ: 23.4 MB, C# ύл 11.69% û + + +```c +public class Solution { + public string LongestCommonPrefix(string[] strs) + { + if (strs.Length == 0) + return string.Empty; + + string result = strs[0]; + for (int i = 1; i < strs.Length; i++) + { + result = Prefix(result, strs[i]); + if (string.IsNullOrEmpty(result)) + break; + } + return result; + } + + public string Prefix(string str1, string str2) + { + int len1 = str1.Length; + int len2 = str2.Length; + int len = Math.Min(len1, len2); + int i = 0; + for (; i < len; i++) + { + if (str1[i] != str2[i]) + break; + } + return i == 0 ? string.Empty : str1.Substring(0, i); + } +} +``` + +--- +## Ŀ05תַ + +> - ţ344 +> - Ѷȣ +> - https://leetcode-cn.com/problems/reverse-string/ + + + +дһǽַתַַ char[] ʽ + +ҪĿռ䣬ԭ ޸ʹ O(1) Ķռһ⡣ + +Լеַ ASCII еĿɴӡַ + + +ʾ 1 +```c +룺["h","e","l","l","o"] +["o","l","l","e","h"] +``` + +ʾ 2 +```c +룺["H","a","n","n","a","h"] +["h","a","n","n","a","H"] +``` + + + +**ο룺** + +- ״̬ͨ +- ִʱ: 572 ms, C# ύл 94.94% û +- ڴ: 33.6 MB, C# ύл 5.05% û + +```c +public class Solution { + public void ReverseString(char[] s) { + int i = 0; + int j = s.Length-1; + while (i < j) + { + char c = s[i]; + s[i] = s[j]; + s[j] = c; + i++; + j--; + } + } +} +``` + +--- +## Ŀ06ظַӴ + +> - ţ3 +> - Ѷȣе +> - https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ + +һַҳвظַ **Ӵ** ijȡ + +**ʾ 1:** + +```c +: "abcabcbb" +: 3 +: ΪظַӴ "abc"䳤Ϊ 3 +``` + +**ʾ 2:** + +```c +: "bbbbb" +: 1 +: ΪظַӴ "b"䳤Ϊ 1 +``` + +**ʾ 3:** +```c +: "pwwkew" +: 3 +: ΪظַӴ "wke"䳤Ϊ 3 + ע⣬Ĵ𰸱 Ӵ ijȣ"pwke" һУӴ +``` + +**˼·** + +̬滮˼·ǰÿλΪֹλãظӴijȣ֮Щȵֵɡ + +λ`index`ظӴijΪ`result[index] = min{k1,k2}``k1 = result[index-1] + 1``k2`Ϊ`index`λǰֱ`index`λõַ`index=0`ΪֹӴȡ + + +**ο룺** + +- ִнͨ +- ִʱ96 ms, C# ύл 82.81% û +- ڴģ25.2 MB, C# ύл 25.52% û + +```c +public class Solution +{ + public int LengthOfLongestSubstring(string s) + { + if (string.IsNullOrEmpty(s)) + return 0; + int[] result = new int[s.Length]; + result[0] = 1; + + for (int i = 1; i < s.Length; i++) + { + int count = GetLength(i, s); + result[i] = result[i-1] < count ? result[i-1]+1 : count; + } + return result.Max(); + } + private int GetLength(int index,string s) + { + char c = s[index]; + int result = 1; + for (int i = index-1; i >= 0; i--) + { + if (s[i] != c) + result += 1; + else + break; + } + return result; + } +} +``` + +--- +## Ŀ07Ӵ + +> - ţ5 +> - Ѷȣе +> - https://leetcode-cn.com/problems/longest-palindromic-substring/ + + +һַ sҵ s ĻӴԼ s 󳤶Ϊ 1000 + +ʾ 1 +```c +: "babad" +: "bab" +ע: "aba" ҲһЧ𰸡 +``` + +ʾ 2 +```c +: "cbbd" +: "bb" +``` + +ʾ 3 +```c +: "a" +: "a" +``` + +**˼·** ö̬滮ķ + +һͷַͬ磬abaǻģabcǡ + +̬滮㷨ηƣ˼Ҳǽֽɸ⣬⣬ȻЩĽõԭĽ⡣ + +ηͬǣʺö̬滮⣬ֽõǻģһӽ׶εǽһӽ׶εĽĻϣнһ⣩÷η⣬ֽõĿ̫࣬Щⱻظ˺ܶΡܹѽĴ𰸣ҪʱҳõĴ𰸣ͿԱظ㣬ʡʱ + +ǿһ¼ѽĴ𰸡ܸԺǷõֻҪͽСǶ̬滮Ļ˼· + +Ķ̬滮㷨ֶǾͬʽ + +ʹüǺ `s[l, r]` ʾԭʼַһӴ`l``r` ֱұֵ߽ʹաұʾұ߽ȡ + +`dp[l, r]` ʾӴ `s[l, r]`Ҷ˵㣩Ƿ񹹳ɻĴһά顣 + +- Ӵֻ 1 ַһǻӴ +- Ӵ 2 ַʱ`s[l, r]` һĴôĴ߸һַԵĻӴ `s[l + 1, r - 1]` ҲһǻĴ + +ʣ `s[l] == s[r]` ʱ`dp[l, r]`ֵ `dp[l + 1, r - l]` ﻹҪٶ࿼һ㣺ԭַȥұ߽硱Ӵı߽ + +- ԭַԪظΪ 3 ʱұ߽ȣôȥԺֻʣ 1 ַһǻĴԭַҲһǻĴ +- ԭַԪظΪ 2 ʱұ߽ȣôȥԺֻʣ 0 ַȻԭַҲһǻĴ + +ϣһַұ߽ȣжΪֻ¶֮һɣ +- ȥұ߽Ժַ䣬`s[l + 1, r - 1]`Ԫ2`r - l <= 2` +- ȥұ߽ԺַǻĴ`dp[l + 1, r - 1] == true` + +![](https://img-blog.csdnimg.cn/20200317155934443.png) + +**ο** + +- ״̬ͨ +- ִʱ: 232 ms, C# ύл 46.79% û +- ڴ: 40.9 MB, C# ύл 5.43% û + + +```c +public class Solution { + public string LongestPalindrome(string s) + { + if (string.IsNullOrEmpty(s)) + return string.Empty; + int len = s.Length; + if (len == 1) + return s; + int longestPalindromelen = 1; + string longestPalindromeStr = s.Substring(0, 1); + bool[,] dp = new bool[len, len]; + + for (int r = 1; r < len; r++) + { + for (int l = 0; l < r; l++) + { + if (s[r] == s[l] && (r - l <= 2 || dp[l + 1, r - 1] == true)) + { + dp[l, r] = true; + if (longestPalindromelen < r - l + 1) + { + longestPalindromelen = r - l + 1; + longestPalindromeStr = s.Substring(l, r - l + 1); + } + } + } + } + return longestPalindromeStr; + } +} +``` + + +--- +## Ŀ08ַ + +> - ţ43 +> - Ѷȣе +> - https://leetcode-cn.com/problems/multiply-strings/ + + +ַʽʾķǸ`num1``num2``num1``num2`ij˻ǵij˻ҲʾΪַʽ + +ʾ 1: +```c +: num1 = "2", num2 = "3" +: "6" +``` + +ʾ 2: +```c +: num1 = "123", num2 = "456" +: "56088" +``` + + + +ʾ 3: +```c +: num1 = "498828660196", num2 = "840477629533" +: "419254329864656431168468" +``` + + +˵ + +- `num1``num2`ijС110 +- `num1``num2`ֻ 0-9 +- `num1``num2`㿪ͷ 0 +- ʹκα׼Ĵͣ BigIntegerֱӽתΪ + + +**ο** + +- ״̬ͨ +- ִʱ132 ms, C# ύл 94.74% û +- ڴģ24.1 MB, C# ύл 31.82% û + +```c +public class Solution { + public string Multiply(string num1, string num2) { + if (num1 == "0" || num2 == "0") + return "0"; + int len1 = num1.Length; + int len2 = num2.Length; + int len = len1 + len2; + int[] temp = new int[len]; + + for (int i = len2 - 1; i >= 0; i--) + { + int k = len2 - i; + int b = num2[i] - '0'; + for (int j = len1 - 1; j >= 0; j--) + { + int a = num1[j] - '0'; + int c = a*b; + + temp[len - k] += c%10; + if (temp[len - k] >= 10) + { + temp[len - k] = temp[len - k]%10; + temp[len - k - 1]++; + } + + temp[len - k - 1] += c/10; + if (temp[len - k - 1] >= 10) + { + temp[len - k - 1] = temp[len - k - 1]%10; + temp[len - k - 2]++; + } + k++; + } + } + + StringBuilder sb = new StringBuilder(); + int s = temp[0] == 0 ? 1 : 0; + while (s < len) + { + sb.Append(temp[s]); + s++; + } + return sb.ToString(); + } +} +``` + + + + + + +--- +## Ŀ09ʽƥ + +> - ţ10 +> - Ѷȣ +> - https://leetcode-cn.com/problems/regular-expression-matching/ + +һַ`s`һַ`p`ʵһ֧ '.' '*' ʽƥ䡣 + +```c +'.' ƥⵥַ +'*' ƥǰһԪ +``` + +νƥ䣬Ҫ **** ַ`s`ģDzַ + +**˵:** + +- `s`Ϊգֻ a-z Сдĸ +- `p`Ϊգֻ a-z СдĸԼַ '.' '*' + +**ʾ 1:** + +```c +: +s = "aa" +p = "a" +: false +: "a" ޷ƥ "aa" ַ +``` +**ʾ 2:** + +```c +: +s = "aa" +p = "a*" +: true +: Ϊ '*' ƥǰһԪ, ǰԪؾ 'a'ˣַ "aa" ɱΪ 'a' ظһΡ +``` + +**ʾ 3:** + +```c +: +s = "ab" +p = ".*" +: true +: ".*" ʾƥ'*'ַ'.' +``` + +**ʾ 4:** + +```c +: +s = "aab" +p = "c*a*b" +: true +: Ϊ '*' ʾ 'c' Ϊ 0 , 'a' ظһΡ˿ƥַ "aab" +``` + +**ʾ 5:** + +```c +: +s = "mississippi" +p = "mis*is*p*." +: false +``` + +**˼·**ݷ + +ƥ˼·ʵDzϵؼ`s``p`ĿƥײֱһַΪյʱ򣬸óۡ + +ַֻͨƥ䣬Ƚϼɣ + +```c +if (s[i] == p[i]) +``` + +ʽַpֻһ"."һǣȻǰȽϼ + +```c +if (s[i] == p[i] || p[i] == '.') +``` + +ʵʱҪжַȺַпյIJ + +ǣ"*"ַҪ⴦pĵiԪصһԪǺʱ + +- `i`ԪҪ0ΣǾͱ`s`䣬`p`ļԪأ`IsMatch``sbcpa*bc`Ǿͱ`s`䣬`p`"a*"`IsMatch(s:bc,p:bc)` +- `i`ԪҪһλΣȱȽ`i`Ԫغ`s`Ԫأ򱣳`p`䣬`s`Ԫأ`IsMatch``saabbpa*bb`ͱ`p`䣬`s`Ԫأ`IsMatch(s:abb,p:a*bb)` + +ʱһЩҪ˼`sabbpa*abb`ַʽ + +- ڶȽ`i`Ԫغ`s`ԪأȾͻ`s`ַ`IsMatch(s:bb,p:a*abb)`ڰһȥ`p`Ԫأ`IsMatch(s:bb,p:abb)`յ`false` +- ֱӰһȥ`p`Ԫأ`IsMatch(s:abb,p:abb)`յ`true` + +˵һֱὫеһߣǷڿƥ + + + + +**ο** + +- ִнͨ +- ִʱ768 ms, C# ύл 10.69% û +- ڴģ25.6 MB, C# ύл 5.00% û + +```c +public class Solution +{ + public bool IsMatch(string s, string p) + { + //pΪմsΪմƥɹsΪմƥʧܡ + if (string.IsNullOrEmpty(p)) + return string.IsNullOrEmpty(s) ? true : false; + + //жspַǷƥ䣬עҪжsΪ + bool headMatched = string.IsNullOrEmpty(s) == false + && (s[0] == p[0] || p[0] == '.'); + + if (p.Length >= 2 && p[1] == '*') + { + //pĵһԪصһԪ*ֱж + return IsMatch(s, p.Substring(2)) || + (headMatched && IsMatch(s.Substring(1), p)); + } + else if (headMatched) + { + //spַ + return IsMatch(s.Substring(1), p.Substring(1)); + } + else + { + return false; + } + } +} +``` + +## Ŀ10ͨƥ + + +> - ţ44 +> - Ѷȣ +> - https://leetcode-cn.com/problems/wildcard-matching/ + + +һַ(`s`) һַģʽ(`p`) ʵһ֧`'?'``'*'`ͨƥ䡣 + +``` +'?' ƥκεַ +'*' ƥַַ +ַȫƥƥɹ +``` + +**˵:** + +- `s`Ϊգֻ`a-z`Сдĸ +- `p`Ϊգֻ`a-z`СдĸԼַ`?``*` + +**ʾ 1:** + +```c +: +s = "aa" +p = "a" +: false +: "a" ޷ƥ "aa" ַ +``` +**ʾ 2:** + +```c +: +s = "aa" +p = "*" +: true +: '*' ƥַ +``` + +**ʾ 3:** + +```c +: +s = "cb" +p = "?a" +: false +: '?' ƥ 'c', ڶ 'a' ޷ƥ 'b' +``` + +**ʾ 4:** + +```c +: +s = "adceb" +p = "*a*b" +: true +: һ '*' ƥַ, ڶ '*' ƥַ "dce". +``` + +**ʾ 5:** + +```c +: +s = "acdcb" +p = "a*c?b" +: false +``` + +**ʾ 6:** + +```c +룺 +"abefcdgiescdfimde" +"ab*cd?i*de" +true +``` + +**ʾ 7:** + +```c +룺 +"aaaa" +"***a" +true +``` + + +**˼·**˫ + +`i``j`ֱ`s``p`ĵһַ±꣬ʼΪ0`istart``jstart`ֱ`s``p``'*'`ƥλãʼΪ`-1` + +ַͨƥ˼·࣬ѾƥɹIJ־ͲٿˣҪ`i``j`ǵǰڱȽϵַƥ`'*'`ܻᱻظʹȥƥַҪ`istart``jstart`ֱ`s``p`ƥ`'*'`λá + +1. `i``j`ǵַȻ`j`ַ`'?'`ƥɹ"Ƴ"`i``j`Ԫأ`i``j` +2. `j`ַ`'*'`Ȼƥɹ`istart``jstart`ֱ`i`Ԫغ`j`Ԫأ`j` +3. ٷ`istart>-1`˵֮ǰƥ`'*'`Ϊ`'*'`ƥַҪٴƥ`'*'`ƥַƶ`i``istart`һַ`istart`±`i`Ԫأͬʱƶ`j``jstart`һַ +4. 㣬ƥʧܣ`false` + +`s`еַжϣΪ`s`ΪգʱҪ`p`Ϊջ`p`ֻʣǺŵʱ򣬲ܳɹƥ䡣 + +**ο** + +- ִнͨ +- ִʱ92 ms, C# ύл 95.00% û +- ڴģ25.7 MB, C# ύл 66.67% û + +```c +public class Solution +{ + public bool IsMatch(string s, string p) + { + //pΪմsΪմƥɹsΪմƥʧܡ + if (string.IsNullOrEmpty(p)) + return string.IsNullOrEmpty(s) ? true : false; + + int i = 0, j = 0, istart = -1, jstart = -1, plen = p.Length; + + //жsַǷƥ + while (i < s.Length) + { + //ƥɹԼƥʧܷfalse + if (j < plen && (s[i] == p[j] || p[j] == '?')) + { + i++; + j++; + } + else if (j < plen && p[j] == '*') + { + istart = i; + jstart = j; + j++; + } + else if (istart > -1) + { + i = istart + 1; + istart = i; + j = jstart + 1; + } + else + { + return false; + } + } + //sеַжϣΪsΪ + //ʱҪpΪջpֻʣǺŵʱ򣬲ܳɹƥ䡣 + //pʣĶ*Ƴʣ* + while (j < plen && p[j] == '*') + { + j++; + } + return j == plen; + } +} +``` +