From af0612af006d18e7a657fee43f0a68dca4543a4a Mon Sep 17 00:00:00 2001 From: yanglbme Date: 2023年12月11日 19:34:31 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2697 No.2697.Lexicographically Smallest Palindrome --- .../README.md | 54 +++++------------ .../README_EN.md | 58 +++++++------------ .../Solution.cpp | 18 +++--- .../Solution.go | 9 +-- .../Solution.java | 18 +++--- .../Solution.py | 17 +++--- .../Solution.rs | 24 +++----- .../Solution.ts | 4 +- .../README_EN.md | 6 ++ 9 files changed, 77 insertions(+), 131 deletions(-) diff --git a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/README.md b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/README.md index f03dcd4cb5c27..5eda0088bf66a 100644 --- a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/README.md +++ b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/README.md @@ -54,7 +54,7 @@ **方法一:贪心 + 双指针** -我们用两个指针 $i$ 和 $j$ 分别指向字符串的首尾,初始时 $i=0,j=n-1,ドル其中 $n$ 是字符串的长度。每次比较 $s[i]$ 和 $s[j],ドル如果二者不相同,则将其中较大的字符修改为较小的字符,使得两者相同。这样在修改之后,原字符串 $s$ 就变成了一个回文串。 +我们用两个指针 $i$ 和 $j$ 分别指向字符串的首尾,初始时 $i = 0,ドル $j = n - 1$。每一次,我们将 $s[i]$ 和 $s[j]$ 都修改为其中较小的那个字符,使得它们相等。修改之后,原字符串 $s$ 变成了一个回文串。 时间复杂度 $O(n),ドル其中 $n$ 是字符串的长度。我们只需要遍历一遍字符串即可。忽略答案的空间消耗,空间复杂度 $O(1)$。 @@ -67,11 +67,10 @@ ```python class Solution: def makeSmallestPalindrome(self, s: str) -> str: - i, j = 0, len(s) - 1 cs = list(s) + i, j = 0, len(s) - 1 while i < j: - if s[i] != s[j]: - cs[i] = cs[j] = min(s[i], s[j]) + cs[i] = cs[j] = min(cs[i], cs[j]) i, j = i + 1, j - 1 return "".join(cs) ``` @@ -85,11 +84,9 @@ class Solution { public String makeSmallestPalindrome(String s) { char[] cs = s.toCharArray(); for (int i = 0, j = cs.length - 1; i < j; ++i, --j) { - if (cs[i] != cs[j]) { - cs[i] = cs[j] = cs[i] < cs[j] ? cs[i] : cs[j]; - } + cs[i] = cs[j] = (char) Math.min(cs[i], cs[j]); } - return String.valueOf(cs); + return new String(cs); } } ``` @@ -101,9 +98,7 @@ class Solution { public: string makeSmallestPalindrome(string s) { for (int i = 0, j = s.size() - 1; i < j; ++i, --j) { - if (s[i] != s[j]) { - s[i] = s[j] = s[i] < s[j] ? s[i] : s[j]; - } + s[i] = s[j] = min(s[i], s[j]); } return s; } @@ -116,13 +111,8 @@ public: func makeSmallestPalindrome(s string) string { cs := []byte(s) for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { - if cs[i] != cs[j] { - if cs[i] < cs[j] { - cs[j] = cs[i] - } else { - cs[i] = cs[j] - } - } + cs[i] = min(cs[i], cs[j]) + cs[j] = cs[i] } return string(cs) } @@ -134,9 +124,7 @@ func makeSmallestPalindrome(s string) string { function makeSmallestPalindrome(s: string): string { const cs = s.split(''); for (let i = 0, j = s.length - 1; i < j; ++i, --j) { - if (s[i] !== s[j]) { - cs[i] = cs[j] = s[i] < s[j] ? s[i] : s[j]; - } + cs[i] = cs[j] = String.fromCharCode(Math.min(cs[i].charCodeAt(0), cs[j].charCodeAt(0))); } return cs.join(''); } @@ -147,24 +135,14 @@ function makeSmallestPalindrome(s: string): string { ```rust impl Solution { pub fn make_smallest_palindrome(s: String) -> String { - let mut b: Vec = s.bytes().collect(); - let mut i = 0; - let mut j = b.len() - 1; - - while i < j { - if b[i] != b[j] { - if b[i] < b[j] { - b[j] = b[i]; - } else { - b[i] = b[j]; - } - } - - i += 1; - j -= 1; + let mut cs: Vec = s.chars().collect(); + let n = cs.len(); + for i in 0..n / 2 { + let j = n - 1 - i; + cs[i] = std::cmp::min(cs[i], cs[j]); + cs[j] = cs[i]; } - - String::from_utf8(b).unwrap() + cs.into_iter().collect() } } ``` diff --git a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/README_EN.md b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/README_EN.md index ee3ab208df99a..85ec129d7aa88 100644 --- a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/README_EN.md +++ b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/README_EN.md @@ -47,6 +47,12 @@ ## Solutions +**Solution 1: Greedy + Two Pointers** + +We use two pointers $i$ and $j$ to point to the beginning and end of the string, initially $i=0,j=n-1,ドル where $n$ is the length of the string. Each time we compare $s[i]$ and $s[j],ドル if they are not the same, we modify the larger character to the smaller one to make them the same. After the modification, the original string $s$ becomes a palindrome. + +The time complexity is $O(n),ドル where $n$ is the length of the string. We only need to traverse the string once. Ignoring the space consumption of the answer, the space complexity is $O(1)$. + ### **Python3** @@ -54,11 +60,10 @@ ```python class Solution: def makeSmallestPalindrome(self, s: str) -> str: - i, j = 0, len(s) - 1 cs = list(s) + i, j = 0, len(s) - 1 while i < j: - if s[i] != s[j]: - cs[i] = cs[j] = min(s[i], s[j]) + cs[i] = cs[j] = min(cs[i], cs[j]) i, j = i + 1, j - 1 return "".join(cs) ``` @@ -70,11 +75,9 @@ class Solution { public String makeSmallestPalindrome(String s) { char[] cs = s.toCharArray(); for (int i = 0, j = cs.length - 1; i < j; ++i, --j) { - if (cs[i] != cs[j]) { - cs[i] = cs[j] = cs[i] < cs[j] ? cs[i] : cs[j]; - } + cs[i] = cs[j] = (char) Math.min(cs[i], cs[j]); } - return String.valueOf(cs); + return new String(cs); } } ``` @@ -86,9 +89,7 @@ class Solution { public: string makeSmallestPalindrome(string s) { for (int i = 0, j = s.size() - 1; i < j; ++i, --j) { - if (s[i] != s[j]) { - s[i] = s[j] = s[i] < s[j] ? s[i] : s[j]; - } + s[i] = s[j] = min(s[i], s[j]); } return s; } @@ -101,13 +102,8 @@ public: func makeSmallestPalindrome(s string) string { cs := []byte(s) for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { - if cs[i] != cs[j] { - if cs[i] < cs[j] { - cs[j] = cs[i] - } else { - cs[i] = cs[j] - } - } + cs[i] = min(cs[i], cs[j]) + cs[j] = cs[i] } return string(cs) } @@ -119,9 +115,7 @@ func makeSmallestPalindrome(s string) string { function makeSmallestPalindrome(s: string): string { const cs = s.split(''); for (let i = 0, j = s.length - 1; i < j; ++i, --j) { - if (s[i] !== s[j]) { - cs[i] = cs[j] = s[i] < s[j] ? s[i] : s[j]; - } + cs[i] = cs[j] = String.fromCharCode(Math.min(cs[i].charCodeAt(0), cs[j].charCodeAt(0))); } return cs.join(''); } @@ -132,24 +126,14 @@ function makeSmallestPalindrome(s: string): string { ```rust impl Solution { pub fn make_smallest_palindrome(s: String) -> String { - let mut b: Vec = s.bytes().collect(); - let mut i = 0; - let mut j = b.len() - 1; - - while i < j { - if b[i] != b[j] { - if b[i] < b[j] { - b[j] = b[i]; - } else { - b[i] = b[j]; - } - } - - i += 1; - j -= 1; + let mut cs: Vec = s.chars().collect(); + let n = cs.len(); + for i in 0..n / 2 { + let j = n - 1 - i; + cs[i] = std::cmp::min(cs[i], cs[j]); + cs[j] = cs[i]; } - - String::from_utf8(b).unwrap() + cs.into_iter().collect() } } ``` diff --git a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.cpp b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.cpp index b9e4c08676ae6..6d18d9f4c7795 100644 --- a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.cpp +++ b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.cpp @@ -1,11 +1,9 @@ -class Solution { -public: - string makeSmallestPalindrome(string s) { - for (int i = 0, j = s.size() - 1; i < j; ++i, --j) { - if (s[i] != s[j]) { - s[i] = s[j] = s[i] < s[j] ? s[i] : s[j]; - } - } - return s; - } +class Solution { +public: + string makeSmallestPalindrome(string s) { + for (int i = 0, j = s.size() - 1; i < j; ++i, --j) { + s[i] = s[j] = min(s[i], s[j]); + } + return s; + } }; \ No newline at end of file diff --git a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.go b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.go index 5b36cc6802faa..c38e58839d78a 100644 --- a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.go +++ b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.go @@ -1,13 +1,8 @@ func makeSmallestPalindrome(s string) string { cs := []byte(s) for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { - if cs[i] != cs[j] { - if cs[i] < cs[j] { - cs[j] = cs[i] - } else { - cs[i] = cs[j] - } - } + cs[i] = min(cs[i], cs[j]) + cs[j] = cs[i] } return string(cs) } \ No newline at end of file diff --git a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.java b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.java index ad01278102f6a..0896d09f92443 100644 --- a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.java +++ b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.java @@ -1,11 +1,9 @@ -class Solution { - public String makeSmallestPalindrome(String s) { - char[] cs = s.toCharArray(); - for (int i = 0, j = cs.length - 1; i < j; ++i, --j) { - if (cs[i] != cs[j]) { - cs[i] = cs[j] = cs[i] < cs[j] ? cs[i] : cs[j]; - } - } - return String.valueOf(cs); - } +class Solution { + public String makeSmallestPalindrome(String s) { + char[] cs = s.toCharArray(); + for (int i = 0, j = cs.length - 1; i < j; ++i, --j) { + cs[i] = cs[j] = (char) Math.min(cs[i], cs[j]); + } + return new String(cs); + } } \ No newline at end of file diff --git a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.py b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.py index 073b517ffb7a9..b374713ad852b 100644 --- a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.py +++ b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.py @@ -1,9 +1,8 @@ -class Solution: - def makeSmallestPalindrome(self, s: str) -> str: - i, j = 0, len(s) - 1 - cs = list(s) - while i < j: - if s[i] != s[j]: - cs[i] = cs[j] = min(s[i], s[j]) - i, j = i + 1, j - 1 - return "".join(cs) +class Solution: + def makeSmallestPalindrome(self, s: str) -> str: + cs = list(s) + i, j = 0, len(s) - 1 + while i < j: + cs[i] = cs[j] = min(cs[i], cs[j]) + i, j = i + 1, j - 1 + return "".join(cs) diff --git a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.rs b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.rs index 6925d311c162b..2d8eec2abe900 100644 --- a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.rs +++ b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.rs @@ -1,22 +1,12 @@ impl Solution { pub fn make_smallest_palindrome(s: String) -> String { - let mut b: Vec = s.bytes().collect(); - let mut i = 0; - let mut j = b.len() - 1; - - while i < j { - if b[i] != b[j] { - if b[i] < b[j] { - b[j] = b[i]; - } else { - b[i] = b[j]; - } - } - - i += 1; - j -= 1; + let mut cs: Vec = s.chars().collect(); + let n = cs.len(); + for i in 0..n / 2 { + let j = n - 1 - i; + cs[i] = std::cmp::min(cs[i], cs[j]); + cs[j] = cs[i]; } - - String::from_utf8(b).unwrap() + cs.into_iter().collect() } } diff --git a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.ts b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.ts index 9b507864642ec..4d4f567aef53a 100644 --- a/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.ts +++ b/solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.ts @@ -1,9 +1,7 @@ function makeSmallestPalindrome(s: string): string { const cs = s.split(''); for (let i = 0, j = s.length - 1; i < j; ++i, --j) { - if (s[i] !== s[j]) { - cs[i] = cs[j] = s[i] < s[j] ? s[i] : s[j]; - } + cs[i] = cs[j] = String.fromCharCode(Math.min(cs[i].charCodeAt(0), cs[j].charCodeAt(0))); } return cs.join(''); } diff --git a/solution/2700-2799/2702.Minimum Operations to Make Numbers Non-positive/README_EN.md b/solution/2700-2799/2702.Minimum Operations to Make Numbers Non-positive/README_EN.md index 619b355cb5499..37bef1e3edb99 100644 --- a/solution/2700-2799/2702.Minimum Operations to Make Numbers Non-positive/README_EN.md +++ b/solution/2700-2799/2702.Minimum Operations to Make Numbers Non-positive/README_EN.md @@ -45,6 +45,12 @@ Now, all the numbers in nums are non-positive. Therefore, we return 3. ## Solutions +**Solution 1: Binary Search** + +We notice that if an operation count $t$ can make all numbers less than or equal to 0ドル,ドル then for any $t'> t,ドル the operation count $t'$ can also make all numbers less than or equal to 0ドル$. Therefore, we can use binary search to find the minimum operation count. + +We define the left boundary of the binary search as $l=0,ドル and the right boundary as $r=\max(nums)$. Each time we perform a binary search, we find the middle value $mid=\lfloor\frac{l+r}{2}\rfloor,ドル and then determine whether there exists an operation method that does not exceed $mid$ and makes all numbers less than or equal to 0ドル$. If it exists, we update the right boundary $r = mid,ドル otherwise, we update the left boundary + ### **Python3**

AltStyle によって変換されたページ (->オリジナル) /