From e4ea6b83a6886a28b6a2250e33cb2766b5e80f70 Mon Sep 17 00:00:00 2001 From: halfrost Date: Fri, 10 Dec 2021 21:21:21 +0800 Subject: [PATCH] Update 1446 solution --- .../1446.Consecutive Characters.go | 4 +- .../1446.Consecutive-Characters/README.md | 7 +- ...s-That-Can-Form-Two-Arrays-of-Equal-XOR.md | 2 +- .../1400~1499/1446.Consecutive-Characters.md | 82 +++++++++++++++++++ ...s-As-a-Prefix-of-Any-Word-in-a-Sentence.md | 2 +- website/content/ChapterTwo/Array.md | 2 +- .../content/ChapterTwo/Dynamic_Programming.md | 2 +- website/content/ChapterTwo/Math.md | 2 +- website/content/ChapterTwo/String.md | 1 + 9 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 website/content/ChapterFour/1400~1499/1446.Consecutive-Characters.md diff --git a/leetcode/1446.Consecutive-Characters/1446.Consecutive Characters.go b/leetcode/1446.Consecutive-Characters/1446.Consecutive Characters.go index afd69bdc..9da00e7c 100644 --- a/leetcode/1446.Consecutive-Characters/1446.Consecutive Characters.go +++ b/leetcode/1446.Consecutive-Characters/1446.Consecutive Characters.go @@ -1,9 +1,7 @@ package leetcode func maxPower(s string) int { - cur := s[0] - cnt := 1 - ans := 1 + cur, cnt, ans := s[0], 1, 1 for i := 1; i < len(s); i++ { if cur == s[i] { cnt++ diff --git a/leetcode/1446.Consecutive-Characters/README.md b/leetcode/1446.Consecutive-Characters/README.md index 483bd012..78a6527a 100644 --- a/leetcode/1446.Consecutive-Characters/README.md +++ b/leetcode/1446.Consecutive-Characters/README.md @@ -1,4 +1,4 @@ -# [1446. Consecutive Characters](https://leetcode-cn.com/problems/consecutive-characters/) +# [1446. Consecutive Characters](https://leetcode.com/problems/consecutive-characters/) ## 题目 @@ -54,9 +54,7 @@ Given a string s, return the power of s. package leetcode func maxPower(s string) int { - cur := s[0] - cnt := 1 - ans := 1 + cur, cnt, ans := s[0], 1, 1 for i := 1; i < len(s); i++ { if cur == s[i] { cnt++ @@ -73,4 +71,5 @@ func maxPower(s string) int { } return ans } + ``` \ No newline at end of file diff --git a/website/content/ChapterFour/1400~1499/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR.md b/website/content/ChapterFour/1400~1499/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR.md index 47759730..e8fcfcce 100644 --- a/website/content/ChapterFour/1400~1499/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR.md +++ b/website/content/ChapterFour/1400~1499/1442.Count-Triplets-That-Can-Form-Two-Arrays-of-Equal-XOR.md @@ -100,5 +100,5 @@ func countTriplets(arr []int) int { ----------------------------------------------

⬅️上一页

-

下一页➡️

+

下一页➡️

diff --git a/website/content/ChapterFour/1400~1499/1446.Consecutive-Characters.md b/website/content/ChapterFour/1400~1499/1446.Consecutive-Characters.md new file mode 100644 index 00000000..1d1beb7f --- /dev/null +++ b/website/content/ChapterFour/1400~1499/1446.Consecutive-Characters.md @@ -0,0 +1,82 @@ +# [1446. Consecutive Characters](https://leetcode.com/problems/consecutive-characters/) + +## 题目 + +The power of the string is the maximum length of a non-empty substring that contains only one unique character. + +Given a string s, return the power of s. + +**Example 1**: + + Input: s = "leetcode" + Output: 2 + Explanation: The substring "ee" is of length 2 with the character 'e' only. + +**Example 2**: + + Input: s = "abbcccddddeeeeedcba" + Output: 5 + Explanation: The substring "eeeee" is of length 5 with the character 'e' only. + +**Example 3**: + + Input: s = "triplepillooooow" + Output: 5 + +**Example 4**: + + Input: s = "hooraaaaaaaaaaay" + Output: 11 + +**Example 5**: + + Input: s = "tourist" + Output: 1 + +**Constraints:** + +- 1 <= s.length <= 500 +- s consists of only lowercase English letters. + +## 题目大意 + +给你一个字符串 s ,字符串的「能量」定义为:只包含一种字符的最长非空子字符串的长度。 + +请你返回字符串的能量。 + +## 解题思路 + +- 顺序遍历进行统计 + +## 代码 + +```go +package leetcode + +func maxPower(s string) int { + cur, cnt, ans := s[0], 1, 1 + for i := 1; i < len(s); i++ { + if cur == s[i] { + cnt++ + } else { + if cnt > ans { + ans = cnt + } + cur = s[i] + cnt = 1 + } + } + if cnt > ans { + ans = cnt + } + return ans +} + +``` + + +---------------------------------------------- +
+

⬅️上一页

+

下一页➡️

+
diff --git a/website/content/ChapterFour/1400~1499/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md b/website/content/ChapterFour/1400~1499/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md index cd97d1e3..3bba6e43 100644 --- a/website/content/ChapterFour/1400~1499/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md +++ b/website/content/ChapterFour/1400~1499/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md @@ -100,6 +100,6 @@ func isPrefixOfWord(sentence string, searchWord string) int { ----------------------------------------------
-

⬅️上一页

+

⬅️上一页

下一页➡️

diff --git a/website/content/ChapterTwo/Array.md b/website/content/ChapterTwo/Array.md index 54304b0b..c307e941 100644 --- a/website/content/ChapterTwo/Array.md +++ b/website/content/ChapterTwo/Array.md @@ -231,7 +231,7 @@ weight: 1 |0870|Advantage Shuffle|[Go]({{< relref "/ChapterFour/0800~0899/0870.Advantage-Shuffle.md" >}})|Medium||||51.0%| |0874|Walking Robot Simulation|[Go]({{< relref "/ChapterFour/0800~0899/0874.Walking-Robot-Simulation.md" >}})|Medium||||37.4%| |0875|Koko Eating Bananas|[Go]({{< relref "/ChapterFour/0800~0899/0875.Koko-Eating-Bananas.md" >}})|Medium||||54.3%| -|0877|Stone Game|[Go]({{< relref "/ChapterFour/0800~0899/0877.Stone-Game.md" >}})|Medium||||68.7%| +|0877|Stone Game|[Go]({{< relref "/ChapterFour/0800~0899/0877.Stone-Game.md" >}})|Medium||||68.8%| |0881|Boats to Save People|[Go]({{< relref "/ChapterFour/0800~0899/0881.Boats-to-Save-People.md" >}})|Medium||||49.6%| |0885|Spiral Matrix III|[Go]({{< relref "/ChapterFour/0800~0899/0885.Spiral-Matrix-III.md" >}})|Medium||||71.9%| |0888|Fair Candy Swap|[Go]({{< relref "/ChapterFour/0800~0899/0888.Fair-Candy-Swap.md" >}})|Easy||||60.1%| diff --git a/website/content/ChapterTwo/Dynamic_Programming.md b/website/content/ChapterTwo/Dynamic_Programming.md index 1cd1c818..7014f1cd 100644 --- a/website/content/ChapterTwo/Dynamic_Programming.md +++ b/website/content/ChapterTwo/Dynamic_Programming.md @@ -77,7 +77,7 @@ weight: 7 |0834|Sum of Distances in Tree|[Go]({{< relref "/ChapterFour/0800~0899/0834.Sum-of-Distances-in-Tree.md" >}})|Hard||||52.5%| |0838|Push Dominoes|[Go]({{< relref "/ChapterFour/0800~0899/0838.Push-Dominoes.md" >}})|Medium| O(n)| O(n)||52.0%| |0845|Longest Mountain in Array|[Go]({{< relref "/ChapterFour/0800~0899/0845.Longest-Mountain-in-Array.md" >}})|Medium||||39.4%| -|0877|Stone Game|[Go]({{< relref "/ChapterFour/0800~0899/0877.Stone-Game.md" >}})|Medium||||68.7%| +|0877|Stone Game|[Go]({{< relref "/ChapterFour/0800~0899/0877.Stone-Game.md" >}})|Medium||||68.8%| |0887|Super Egg Drop|[Go]({{< relref "/ChapterFour/0800~0899/0887.Super-Egg-Drop.md" >}})|Hard||||27.0%| |0898|Bitwise ORs of Subarrays|[Go]({{< relref "/ChapterFour/0800~0899/0898.Bitwise-ORs-of-Subarrays.md" >}})|Medium||||36.2%| |0907|Sum of Subarray Minimums|[Go]({{< relref "/ChapterFour/0900~0999/0907.Sum-of-Subarray-Minimums.md" >}})|Medium||||33.1%| diff --git a/website/content/ChapterTwo/Math.md b/website/content/ChapterTwo/Math.md index c330a397..5e3fb52c 100644 --- a/website/content/ChapterTwo/Math.md +++ b/website/content/ChapterTwo/Math.md @@ -87,7 +87,7 @@ weight: 12 |0812|Largest Triangle Area|[Go]({{< relref "/ChapterFour/0800~0899/0812.Largest-Triangle-Area.md" >}})|Easy||||59.5%| |0836|Rectangle Overlap|[Go]({{< relref "/ChapterFour/0800~0899/0836.Rectangle-Overlap.md" >}})|Easy||||43.0%| |0869|Reordered Power of 2|[Go]({{< relref "/ChapterFour/0800~0899/0869.Reordered-Power-of-2.md" >}})|Medium||||61.3%| -|0877|Stone Game|[Go]({{< relref "/ChapterFour/0800~0899/0877.Stone-Game.md" >}})|Medium||||68.7%| +|0877|Stone Game|[Go]({{< relref "/ChapterFour/0800~0899/0877.Stone-Game.md" >}})|Medium||||68.8%| |0878|Nth Magical Number|[Go]({{< relref "/ChapterFour/0800~0899/0878.Nth-Magical-Number.md" >}})|Hard||||35.8%| |0887|Super Egg Drop|[Go]({{< relref "/ChapterFour/0800~0899/0887.Super-Egg-Drop.md" >}})|Hard||||27.0%| |0891|Sum of Subsequence Widths|[Go]({{< relref "/ChapterFour/0800~0899/0891.Sum-of-Subsequence-Widths.md" >}})|Hard| O(n log n)| O(1)||34.2%| diff --git a/website/content/ChapterTwo/String.md b/website/content/ChapterTwo/String.md index 7bc887ef..470bed02 100644 --- a/website/content/ChapterTwo/String.md +++ b/website/content/ChapterTwo/String.md @@ -160,6 +160,7 @@ weight: 2 |1268|Search Suggestions System|[Go]({{< relref "/ChapterFour/1200~1299/1268.Search-Suggestions-System.md" >}})|Medium||||65.8%| |1332|Remove Palindromic Subsequences|[Go]({{< relref "/ChapterFour/1300~1399/1332.Remove-Palindromic-Subsequences.md" >}})|Easy||||69.2%| |1396|Design Underground System|[Go]({{< relref "/ChapterFour/1300~1399/1396.Design-Underground-System.md" >}})|Medium||||71.6%| +|1446|Consecutive Characters|[Go]({{< relref "/ChapterFour/1400~1499/1446.Consecutive-Characters.md" >}})|Easy||||62.0%| |1455|Check If a Word Occurs As a Prefix of Any Word in a Sentence|[Go]({{< relref "/ChapterFour/1400~1499/1455.Check-If-a-Word-Occurs-As-a-Prefix-of-Any-Word-in-a-Sentence.md" >}})|Easy||||64.4%| |1461|Check If a String Contains All Binary Codes of Size K|[Go]({{< relref "/ChapterFour/1400~1499/1461.Check-If-a-String-Contains-All-Binary-Codes-of-Size-K.md" >}})|Medium||||54.5%| |1573|Number of Ways to Split a String|[Go]({{< relref "/ChapterFour/1500~1599/1573.Number-of-Ways-to-Split-a-String.md" >}})|Medium||||31.7%|