diff --git a/solution/1800-1899/1842.Next Palindrome Using Same Digits/README.md b/solution/1800-1899/1842.Next Palindrome Using Same Digits/README.md index f47434daeb281..202179c5c907b 100644 --- a/solution/1800-1899/1842.Next Palindrome Using Same Digits/README.md +++ b/solution/1800-1899/1842.Next Palindrome Using Same Digits/README.md @@ -51,6 +51,12 @@ +**方法一:求前一半的下一个排列** + +根据题目描述,我们只需要求出前一半的下一个排列,然后遍历前一半,对称赋值后半部分即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串长度。 + ### **Python3** @@ -58,7 +64,29 @@ ```python - +class Solution: + def nextPalindrome(self, num: str) -> str: + def next_permutation(nums: List[str]) -> bool: + n = len(nums) // 2 + i = n - 2 + while i >= 0 and nums[i] >= nums[i + 1]: + i -= 1 + if i < 0: + return False + j = n - 1 + while j >= 0 and nums[j] <= nums[i]: + j -= 1 + nums[i], nums[j] = nums[j], nums[i] + nums[i + 1 : n] = nums[i + 1 : n][::-1] + return True + + nums = list(num) + if not next_permutation(nums): + return "" + n = len(nums) + for i in range(n // 2): + nums[n - i - 1] = nums[i] + return "".join(nums) ``` ### **Java** @@ -66,7 +94,137 @@ ```java +class Solution { + public String nextPalindrome(String num) { + char[] nums = num.toCharArray(); + if (!nextPermutation(nums)) { + return ""; + } + int n = nums.length; + for (int i = 0; i < n / 2; ++i) { + nums[n - 1 - i] = nums[i]; + } + return String.valueOf(nums); + } + + private boolean nextPermutation(char[] nums) { + int n = nums.length / 2; + int i = n - 2; + while (i >= 0 && nums[i] >= nums[i + 1]) { + --i; + } + if (i < 0) { + return false; + } + int j = n - 1; + while (j >= 0 && nums[i] >= nums[j]) { + --j; + } + swap(nums, i++, j); + for (j = n - 1; i < j; ++i, --j) { + swap(nums, i, j); + } + return true; + } + + private void swap(char[] nums, int i, int j) { + char t = nums[i]; + nums[i] = nums[j]; + nums[j] = t; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + string nextPalindrome(string num) { + int n = num.size(); + string nums = num.substr(0, n / 2); + if (!next_permutation(begin(nums), end(nums))) { + return ""; + } + for (int i = 0; i < n / 2; ++i) { + num[i] = nums[i]; + num[n - i - 1] = nums[i]; + } + return num; + } +}; +``` + +### **Go** + +```go +func nextPalindrome(num string) string { + nums := []byte(num) + n := len(nums) + if !nextPermutation(nums) { + return "" + } + for i := 0; i < n/2; i++ { + nums[n-1-i] = nums[i] + } + return string(nums) +} + +func nextPermutation(nums []byte) bool { + n := len(nums) / 2 + i := n - 2 + for i >= 0 && nums[i] >= nums[i+1] { + i-- + } + if i < 0 { + return false + } + j := n - 1 + for j >= 0 && nums[j] <= nums[i] { + j-- + } + nums[i], nums[j] = nums[j], nums[i] + for i, j = i+1, n-1; i < j; i, j = i+1, j-1 { + nums[i], nums[j] = nums[j], nums[i] + } + return true +} +``` +### **TypeScript** + +```ts +function nextPalindrome(num: string): string { + const nums = num.split(''); + const n = nums.length; + if (!nextPermutation(nums)) { + return ''; + } + for (let i = 0; i < n >> 1; ++i) { + nums[n - 1 - i] = nums[i]; + } + return nums.join(''); +} + +function nextPermutation(nums: string[]): boolean { + const n = nums.length >> 1; + let i = n - 2; + while (i >= 0 && nums[i] >= nums[i + 1]) { + i--; + } + if (i < 0) { + return false; + } + let j = n - 1; + while (j >= 0 && nums[i] >= nums[j]) { + j--; + } + [nums[i], nums[j]] = [nums[j], nums[i]]; + for (i = i + 1, j = n - 1; i < j; ++i, --j) { + [nums[i], nums[j]] = [nums[j], nums[i]]; + } + return true; +} ``` ### **...** diff --git a/solution/1800-1899/1842.Next Palindrome Using Same Digits/README_EN.md b/solution/1800-1899/1842.Next Palindrome Using Same Digits/README_EN.md index 34c6f7866e807..8c44f4dd9e8a6 100644 --- a/solution/1800-1899/1842.Next Palindrome Using Same Digits/README_EN.md +++ b/solution/1800-1899/1842.Next Palindrome Using Same Digits/README_EN.md @@ -45,18 +45,176 @@ ## Solutions +**Solution 1: Find the Next Permutation of the First Half** + +According to the problem description, we only need to find the next permutation of the first half of the string, then traverse the first half and symmetrically assign values to the second half. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string. + ### **Python3** ```python - +class Solution: + def nextPalindrome(self, num: str) -> str: + def next_permutation(nums: List[str]) -> bool: + n = len(nums) // 2 + i = n - 2 + while i >= 0 and nums[i] >= nums[i + 1]: + i -= 1 + if i < 0: + return False + j = n - 1 + while j >= 0 and nums[j] <= nums[i]: + j -= 1 + nums[i], nums[j] = nums[j], nums[i] + nums[i + 1 : n] = nums[i + 1 : n][::-1] + return True + + nums = list(num) + if not next_permutation(nums): + return "" + n = len(nums) + for i in range(n // 2): + nums[n - i - 1] = nums[i] + return "".join(nums) ``` ### **Java** ```java +class Solution { + public String nextPalindrome(String num) { + char[] nums = num.toCharArray(); + if (!nextPermutation(nums)) { + return ""; + } + int n = nums.length; + for (int i = 0; i < n / 2; ++i) { + nums[n - 1 - i] = nums[i]; + } + return String.valueOf(nums); + } + + private boolean nextPermutation(char[] nums) { + int n = nums.length / 2; + int i = n - 2; + while (i >= 0 && nums[i] >= nums[i + 1]) { + --i; + } + if (i < 0) { + return false; + } + int j = n - 1; + while (j >= 0 && nums[i] >= nums[j]) { + --j; + } + swap(nums, i++, j); + for (j = n - 1; i < j; ++i, --j) { + swap(nums, i, j); + } + return true; + } + + private void swap(char[] nums, int i, int j) { + char t = nums[i]; + nums[i] = nums[j]; + nums[j] = t; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + string nextPalindrome(string num) { + int n = num.size(); + string nums = num.substr(0, n / 2); + if (!next_permutation(begin(nums), end(nums))) { + return ""; + } + for (int i = 0; i < n / 2; ++i) { + num[i] = nums[i]; + num[n - i - 1] = nums[i]; + } + return num; + } +}; +``` + +### **Go** + +```go +func nextPalindrome(num string) string { + nums := []byte(num) + n := len(nums) + if !nextPermutation(nums) { + return "" + } + for i := 0; i < n/2; i++ { + nums[n-1-i] = nums[i] + } + return string(nums) +} + +func nextPermutation(nums []byte) bool { + n := len(nums) / 2 + i := n - 2 + for i >= 0 && nums[i] >= nums[i+1] { + i-- + } + if i < 0 { + return false + } + j := n - 1 + for j >= 0 && nums[j] <= nums[i] { + j-- + } + nums[i], nums[j] = nums[j], nums[i] + for i, j = i+1, n-1; i < j; i, j = i+1, j-1 { + nums[i], nums[j] = nums[j], nums[i] + } + return true +} +``` +### **TypeScript** + +```ts +function nextPalindrome(num: string): string { + const nums = num.split(''); + const n = nums.length; + if (!nextPermutation(nums)) { + return ''; + } + for (let i = 0; i < n >> 1; ++i) { + nums[n - 1 - i] = nums[i]; + } + return nums.join(''); +} + +function nextPermutation(nums: string[]): boolean { + const n = nums.length >> 1; + let i = n - 2; + while (i >= 0 && nums[i] >= nums[i + 1]) { + i--; + } + if (i < 0) { + return false; + } + let j = n - 1; + while (j >= 0 && nums[i] >= nums[j]) { + j--; + } + [nums[i], nums[j]] = [nums[j], nums[i]]; + for (i = i + 1, j = n - 1; i < j; ++i, --j) { + [nums[i], nums[j]] = [nums[j], nums[i]]; + } + return true; +} ``` ### **...** diff --git a/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.cpp b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.cpp new file mode 100644 index 0000000000000..90cd4d6a6c389 --- /dev/null +++ b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + string nextPalindrome(string num) { + int n = num.size(); + string nums = num.substr(0, n / 2); + if (!next_permutation(begin(nums), end(nums))) { + return ""; + } + for (int i = 0; i < n / 2; ++i) { + num[i] = nums[i]; + num[n - i - 1] = nums[i]; + } + return num; + } +}; \ No newline at end of file diff --git a/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.go b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.go new file mode 100644 index 0000000000000..e9bc199395ddd --- /dev/null +++ b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.go @@ -0,0 +1,31 @@ +func nextPalindrome(num string) string { + nums := []byte(num) + n := len(nums) + if !nextPermutation(nums) { + return "" + } + for i := 0; i < n/2; i++ { + nums[n-1-i] = nums[i] + } + return string(nums) +} + +func nextPermutation(nums []byte) bool { + n := len(nums) / 2 + i := n - 2 + for i >= 0 && nums[i] >= nums[i+1] { + i-- + } + if i < 0 { + return false + } + j := n - 1 + for j >= 0 && nums[j] <= nums[i] { + j-- + } + nums[i], nums[j] = nums[j], nums[i] + for i, j = i+1, n-1; i < j; i, j = i+1, j-1 { + nums[i], nums[j] = nums[j], nums[i] + } + return true +} \ No newline at end of file diff --git a/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.java b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.java new file mode 100644 index 0000000000000..b2abe6bbff2e3 --- /dev/null +++ b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.java @@ -0,0 +1,39 @@ +class Solution { + public String nextPalindrome(String num) { + char[] nums = num.toCharArray(); + if (!nextPermutation(nums)) { + return ""; + } + int n = nums.length; + for (int i = 0; i < n / 2; ++i) { + nums[n - 1 - i] = nums[i]; + } + return String.valueOf(nums); + } + + private boolean nextPermutation(char[] nums) { + int n = nums.length / 2; + int i = n - 2; + while (i >= 0 && nums[i] >= nums[i + 1]) { + --i; + } + if (i < 0) { + return false; + } + int j = n - 1; + while (j >= 0 && nums[i] >= nums[j]) { + --j; + } + swap(nums, i++, j); + for (j = n - 1; i < j; ++i, --j) { + swap(nums, i, j); + } + return true; + } + + private void swap(char[] nums, int i, int j) { + char t = nums[i]; + nums[i] = nums[j]; + nums[j] = t; + } +} \ No newline at end of file diff --git a/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.py b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.py new file mode 100644 index 0000000000000..a555ffdf7a0e9 --- /dev/null +++ b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.py @@ -0,0 +1,23 @@ +class Solution: + def nextPalindrome(self, num: str) -> str: + def next_permutation(nums: List[str]) -> bool: + n = len(nums) // 2 + i = n - 2 + while i >= 0 and nums[i] >= nums[i + 1]: + i -= 1 + if i < 0: + return False + j = n - 1 + while j >= 0 and nums[j] <= nums[i]: + j -= 1 + nums[i], nums[j] = nums[j], nums[i] + nums[i + 1 : n] = nums[i + 1 : n][::-1] + return True + + nums = list(num) + if not next_permutation(nums): + return "" + n = len(nums) + for i in range(n // 2): + nums[n - i - 1] = nums[i] + return "".join(nums) diff --git a/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.ts b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.ts new file mode 100644 index 0000000000000..3639a33d59a95 --- /dev/null +++ b/solution/1800-1899/1842.Next Palindrome Using Same Digits/Solution.ts @@ -0,0 +1,31 @@ +function nextPalindrome(num: string): string { + const nums = num.split(''); + const n = nums.length; + if (!nextPermutation(nums)) { + return ''; + } + for (let i = 0; i < n >> 1; ++i) { + nums[n - 1 - i] = nums[i]; + } + return nums.join(''); +} + +function nextPermutation(nums: string[]): boolean { + const n = nums.length >> 1; + let i = n - 2; + while (i >= 0 && nums[i] >= nums[i + 1]) { + i--; + } + if (i < 0) { + return false; + } + let j = n - 1; + while (j >= 0 && nums[i] >= nums[j]) { + j--; + } + [nums[i], nums[j]] = [nums[j], nums[i]]; + for (i = i + 1, j = n - 1; i < j; ++i, --j) { + [nums[i], nums[j]] = [nums[j], nums[i]]; + } + return true; +}