Skip to content

Commit 02a27fa

Browse files
committed
solve problem Largest Palindrome Product
1 parent 84b1587 commit 02a27fa

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ All solutions will be accepted!
195195
|686|[Repeated String Match](https://leetcode-cn.com/problems/repeated-string-match/description/)|[java/py/js](./algorithms/RepeatedStringMatch)|Easy|
196196
|665|[Non Decreasing Array](https://leetcode-cn.com/problems/non-decreasing-array/description/)|[java/py/js](./algorithms/NonDecreasingArray)|Easy|
197197
|867|[Transpose Matrix](https://leetcode-cn.com/problems/transpose-matrix/description/)|[java/py/js](./algorithms/TransposeMatrix)|Easy|
198+
|479|[Largest Palindrome Product](https://leetcode-cn.com/problems/largest-palindrome-product/description/)|[java/py/js](./algorithms/LargestPalindromeProduct)|Easy|
198199

199200
# Database
200201
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Largest Palindrome Product
2+
Use simple way to avoid TLE of python solution and the Number overflow of javascript
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int largestPalindrome(int n) {
3+
if (n == 1) return 9;
4+
int upper = (int) Math.pow(10,n) - 1;
5+
for (int i = upper; i > upper / 10; i--) {
6+
StringBuffer str = new StringBuffer();
7+
str.append(i);
8+
long palin = Long.valueOf(str.toString() + str.reverse().toString());
9+
for (int j = upper; j > upper / 10; j--) {
10+
if (palin / j > upper)
11+
break;
12+
if (palin % j == 0)
13+
return (int) (palin % 1337);
14+
}
15+
}
16+
return -1;
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var largestPalindrome = function(n) {
6+
return [9, 987, 123, 597, 677, 1218, 877, 475][n - 1]
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution(object):
2+
def largestPalindrome(self, n):
3+
"""
4+
:type n: int
5+
:rtype: int
6+
"""
7+
return [9, 9009, 906609, 99000099, 9966006699, 999000000999, 99956644665999, 9999000000009999][n - 1] % 1337

0 commit comments

Comments
 (0)