Skip to content

Commit 12cf12a

Browse files
authored
feat: add solutions to lc problem: No.2697 (#2084)
No.2697.Lexicographically Smallest Palindrome
1 parent a242d2f commit 12cf12a

File tree

9 files changed

+77
-131
lines changed

9 files changed

+77
-131
lines changed

solution/2600-2699/2697.Lexicographically Smallest Palindrome/README.md

+16-38
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
**方法一:贪心 + 双指针**
5656

57-
我们用两个指针 $i$ 和 $j$ 分别指向字符串的首尾,初始时 $i=0,j=n-1$,其中 $n$ 是字符串的长度。每次比较 $s[i]$ 和 $s[j]$,如果二者不相同,则将其中较大的字符修改为较小的字符,使得两者相同。这样在修改之后,原字符串 $s$ 就变成了一个回文串
57+
我们用两个指针 $i$ 和 $j$ 分别指向字符串的首尾,初始时 $i = 0$, $j = n - 1$。每一次,我们将 $s[i]$ 和 $s[j]$ 都修改为其中较小的那个字符,使得它们相等。修改之后,原字符串 $s$ 变成了一个回文串
5858

5959
时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。我们只需要遍历一遍字符串即可。忽略答案的空间消耗,空间复杂度 $O(1)$。
6060

@@ -67,11 +67,10 @@
6767
```python
6868
class Solution:
6969
def makeSmallestPalindrome(self, s: str) -> str:
70-
i, j = 0, len(s) - 1
7170
cs = list(s)
71+
i, j = 0, len(s) - 1
7272
while i < j:
73-
if s[i] != s[j]:
74-
cs[i] = cs[j] = min(s[i], s[j])
73+
cs[i] = cs[j] = min(cs[i], cs[j])
7574
i, j = i + 1, j - 1
7675
return "".join(cs)
7776
```
@@ -85,11 +84,9 @@ class Solution {
8584
public String makeSmallestPalindrome(String s) {
8685
char[] cs = s.toCharArray();
8786
for (int i = 0, j = cs.length - 1; i < j; ++i, --j) {
88-
if (cs[i] != cs[j]) {
89-
cs[i] = cs[j] = cs[i] < cs[j] ? cs[i] : cs[j];
90-
}
87+
cs[i] = cs[j] = (char) Math.min(cs[i], cs[j]);
9188
}
92-
return String.valueOf(cs);
89+
return new String(cs);
9390
}
9491
}
9592
```
@@ -101,9 +98,7 @@ class Solution {
10198
public:
10299
string makeSmallestPalindrome(string s) {
103100
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
104-
if (s[i] != s[j]) {
105-
s[i] = s[j] = s[i] < s[j] ? s[i] : s[j];
106-
}
101+
s[i] = s[j] = min(s[i], s[j]);
107102
}
108103
return s;
109104
}
@@ -116,13 +111,8 @@ public:
116111
func makeSmallestPalindrome(s string) string {
117112
cs := []byte(s)
118113
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
119-
if cs[i] != cs[j] {
120-
if cs[i] < cs[j] {
121-
cs[j] = cs[i]
122-
} else {
123-
cs[i] = cs[j]
124-
}
125-
}
114+
cs[i] = min(cs[i], cs[j])
115+
cs[j] = cs[i]
126116
}
127117
return string(cs)
128118
}
@@ -134,9 +124,7 @@ func makeSmallestPalindrome(s string) string {
134124
function makeSmallestPalindrome(s: string): string {
135125
const cs = s.split('');
136126
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
137-
if (s[i] !== s[j]) {
138-
cs[i] = cs[j] = s[i] < s[j] ? s[i] : s[j];
139-
}
127+
cs[i] = cs[j] = String.fromCharCode(Math.min(cs[i].charCodeAt(0), cs[j].charCodeAt(0)));
140128
}
141129
return cs.join('');
142130
}
@@ -147,24 +135,14 @@ function makeSmallestPalindrome(s: string): string {
147135
```rust
148136
impl Solution {
149137
pub fn make_smallest_palindrome(s: String) -> String {
150-
let mut b: Vec<u8> = s.bytes().collect();
151-
let mut i = 0;
152-
let mut j = b.len() - 1;
153-
154-
while i < j {
155-
if b[i] != b[j] {
156-
if b[i] < b[j] {
157-
b[j] = b[i];
158-
} else {
159-
b[i] = b[j];
160-
}
161-
}
162-
163-
i += 1;
164-
j -= 1;
138+
let mut cs: Vec<char> = s.chars().collect();
139+
let n = cs.len();
140+
for i in 0..n / 2 {
141+
let j = n - 1 - i;
142+
cs[i] = std::cmp::min(cs[i], cs[j]);
143+
cs[j] = cs[i];
165144
}
166-
167-
String::from_utf8(b).unwrap()
145+
cs.into_iter().collect()
168146
}
169147
}
170148
```

solution/2600-2699/2697.Lexicographically Smallest Palindrome/README_EN.md

+21-37
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,23 @@
4747

4848
## Solutions
4949

50+
**Solution 1: Greedy + Two Pointers**
51+
52+
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.
53+
54+
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)$.
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

5460
```python
5561
class Solution:
5662
def makeSmallestPalindrome(self, s: str) -> str:
57-
i, j = 0, len(s) - 1
5863
cs = list(s)
64+
i, j = 0, len(s) - 1
5965
while i < j:
60-
if s[i] != s[j]:
61-
cs[i] = cs[j] = min(s[i], s[j])
66+
cs[i] = cs[j] = min(cs[i], cs[j])
6267
i, j = i + 1, j - 1
6368
return "".join(cs)
6469
```
@@ -70,11 +75,9 @@ class Solution {
7075
public String makeSmallestPalindrome(String s) {
7176
char[] cs = s.toCharArray();
7277
for (int i = 0, j = cs.length - 1; i < j; ++i, --j) {
73-
if (cs[i] != cs[j]) {
74-
cs[i] = cs[j] = cs[i] < cs[j] ? cs[i] : cs[j];
75-
}
78+
cs[i] = cs[j] = (char) Math.min(cs[i], cs[j]);
7679
}
77-
return String.valueOf(cs);
80+
return new String(cs);
7881
}
7982
}
8083
```
@@ -86,9 +89,7 @@ class Solution {
8689
public:
8790
string makeSmallestPalindrome(string s) {
8891
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
89-
if (s[i] != s[j]) {
90-
s[i] = s[j] = s[i] < s[j] ? s[i] : s[j];
91-
}
92+
s[i] = s[j] = min(s[i], s[j]);
9293
}
9394
return s;
9495
}
@@ -101,13 +102,8 @@ public:
101102
func makeSmallestPalindrome(s string) string {
102103
cs := []byte(s)
103104
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
104-
if cs[i] != cs[j] {
105-
if cs[i] < cs[j] {
106-
cs[j] = cs[i]
107-
} else {
108-
cs[i] = cs[j]
109-
}
110-
}
105+
cs[i] = min(cs[i], cs[j])
106+
cs[j] = cs[i]
111107
}
112108
return string(cs)
113109
}
@@ -119,9 +115,7 @@ func makeSmallestPalindrome(s string) string {
119115
function makeSmallestPalindrome(s: string): string {
120116
const cs = s.split('');
121117
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
122-
if (s[i] !== s[j]) {
123-
cs[i] = cs[j] = s[i] < s[j] ? s[i] : s[j];
124-
}
118+
cs[i] = cs[j] = String.fromCharCode(Math.min(cs[i].charCodeAt(0), cs[j].charCodeAt(0)));
125119
}
126120
return cs.join('');
127121
}
@@ -132,24 +126,14 @@ function makeSmallestPalindrome(s: string): string {
132126
```rust
133127
impl Solution {
134128
pub fn make_smallest_palindrome(s: String) -> String {
135-
let mut b: Vec<u8> = s.bytes().collect();
136-
let mut i = 0;
137-
let mut j = b.len() - 1;
138-
139-
while i < j {
140-
if b[i] != b[j] {
141-
if b[i] < b[j] {
142-
b[j] = b[i];
143-
} else {
144-
b[i] = b[j];
145-
}
146-
}
147-
148-
i += 1;
149-
j -= 1;
129+
let mut cs: Vec<char> = s.chars().collect();
130+
let n = cs.len();
131+
for i in 0..n / 2 {
132+
let j = n - 1 - i;
133+
cs[i] = std::cmp::min(cs[i], cs[j]);
134+
cs[j] = cs[i];
150135
}
151-
152-
String::from_utf8(b).unwrap()
136+
cs.into_iter().collect()
153137
}
154138
}
155139
```
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
class Solution {
2-
public:
3-
string makeSmallestPalindrome(string s) {
4-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
5-
if (s[i] != s[j]) {
6-
s[i] = s[j] = s[i] < s[j] ? s[i] : s[j];
7-
}
8-
}
9-
return s;
10-
}
1+
class Solution {
2+
public:
3+
string makeSmallestPalindrome(string s) {
4+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) {
5+
s[i] = s[j] = min(s[i], s[j]);
6+
}
7+
return s;
8+
}
119
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
func makeSmallestPalindrome(s string) string {
22
cs := []byte(s)
33
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
4-
if cs[i] != cs[j] {
5-
if cs[i] < cs[j] {
6-
cs[j] = cs[i]
7-
} else {
8-
cs[i] = cs[j]
9-
}
10-
}
4+
cs[i] = min(cs[i], cs[j])
5+
cs[j] = cs[i]
116
}
127
return string(cs)
138
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
class Solution {
2-
public String makeSmallestPalindrome(String s) {
3-
char[] cs = s.toCharArray();
4-
for (int i = 0, j = cs.length - 1; i < j; ++i, --j) {
5-
if (cs[i] != cs[j]) {
6-
cs[i] = cs[j] = cs[i] < cs[j] ? cs[i] : cs[j];
7-
}
8-
}
9-
return String.valueOf(cs);
10-
}
1+
class Solution {
2+
public String makeSmallestPalindrome(String s) {
3+
char[] cs = s.toCharArray();
4+
for (int i = 0, j = cs.length - 1; i < j; ++i, --j) {
5+
cs[i] = cs[j] = (char) Math.min(cs[i], cs[j]);
6+
}
7+
return new String(cs);
8+
}
119
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
class Solution:
2-
def makeSmallestPalindrome(self, s: str) -> str:
3-
i, j = 0, len(s) - 1
4-
cs = list(s)
5-
while i < j:
6-
if s[i] != s[j]:
7-
cs[i] = cs[j] = min(s[i], s[j])
8-
i, j = i + 1, j - 1
9-
return "".join(cs)
1+
class Solution:
2+
def makeSmallestPalindrome(self, s: str) -> str:
3+
cs = list(s)
4+
i, j = 0, len(s) - 1
5+
while i < j:
6+
cs[i] = cs[j] = min(cs[i], cs[j])
7+
i, j = i + 1, j - 1
8+
return "".join(cs)
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
impl Solution {
22
pub fn make_smallest_palindrome(s: String) -> String {
3-
let mut b: Vec<u8> = s.bytes().collect();
4-
let mut i = 0;
5-
let mut j = b.len() - 1;
6-
7-
while i < j {
8-
if b[i] != b[j] {
9-
if b[i] < b[j] {
10-
b[j] = b[i];
11-
} else {
12-
b[i] = b[j];
13-
}
14-
}
15-
16-
i += 1;
17-
j -= 1;
3+
let mut cs: Vec<char> = s.chars().collect();
4+
let n = cs.len();
5+
for i in 0..n / 2 {
6+
let j = n - 1 - i;
7+
cs[i] = std::cmp::min(cs[i], cs[j]);
8+
cs[j] = cs[i];
189
}
19-
20-
String::from_utf8(b).unwrap()
10+
cs.into_iter().collect()
2111
}
2212
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
function makeSmallestPalindrome(s: string): string {
22
const cs = s.split('');
33
for (let i = 0, j = s.length - 1; i < j; ++i, --j) {
4-
if (s[i] !== s[j]) {
5-
cs[i] = cs[j] = s[i] < s[j] ? s[i] : s[j];
6-
}
4+
cs[i] = cs[j] = String.fromCharCode(Math.min(cs[i].charCodeAt(0), cs[j].charCodeAt(0)));
75
}
86
return cs.join('');
97
}

solution/2700-2799/2702.Minimum Operations to Make Numbers Non-positive/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ Now, all the numbers in nums are non-positive. Therefore, we return 3.
4545

4646
## Solutions
4747

48+
**Solution 1: Binary Search**
49+
50+
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.
51+
52+
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
53+
4854
<!-- tabs:start -->
4955

5056
### **Python3**

0 commit comments

Comments
 (0)