-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.2697 (#2084)
No.2697.Lexicographically Smallest Palindrome
- Loading branch information
Showing
9 changed files
with
77 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 8 additions & 10 deletions
18
solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; |
9 changes: 2 additions & 7 deletions
9
solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
18 changes: 8 additions & 10 deletions
18
solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
17 changes: 8 additions & 9 deletions
17
solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
24 changes: 7 additions & 17 deletions
24
solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,12 @@ | ||
impl Solution { | ||
pub fn make_smallest_palindrome(s: String) -> String { | ||
let mut b: Vec<u8> = 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<char> = 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() | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
solution/2600-2699/2697.Lexicographically Smallest Palindrome/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters