Skip to content

Commit 9f57576

Browse files
committed
feat: add solutions to lc problem: No.0479
No.0479.Largest Palindrome Product
1 parent 4ff1f34 commit 9f57576

File tree

6 files changed

+222
-2
lines changed

6 files changed

+222
-2
lines changed

solution/0400-0499/0479.Largest Palindrome Product/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,91 @@
4444
<!-- 这里可写当前语言的特殊实现逻辑 -->
4545

4646
```python
47-
47+
class Solution:
48+
def largestPalindrome(self, n: int) -> int:
49+
mx = 10**n - 1
50+
for a in range(mx, mx // 10, -1):
51+
b = x = a
52+
while b:
53+
x = x * 10 + b % 10
54+
b //= 10
55+
t = mx
56+
while t * t >= x:
57+
if x % t == 0:
58+
return x % 1337
59+
t -= 1
60+
return 9
4861
```
4962

5063
### **Java**
5164

5265
<!-- 这里可写当前语言的特殊实现逻辑 -->
5366

5467
```java
68+
class Solution {
69+
public int largestPalindrome(int n) {
70+
int mx = (int) Math.pow(10, n) - 1;
71+
for (int a = mx; a > mx / 10; --a) {
72+
int b = a;
73+
long x = a;
74+
while (b != 0) {
75+
x = x * 10 + b % 10;
76+
b /= 10;
77+
}
78+
for (long t = mx; t * t >= x; --t) {
79+
if (x % t == 0) {
80+
return (int) (x % 1337);
81+
}
82+
}
83+
}
84+
return 9;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int largestPalindrome(int n) {
95+
int mx = pow(10, n) - 1;
96+
for (int a = mx; a > mx / 10; --a)
97+
{
98+
int b = a;
99+
long x = a;
100+
while (b)
101+
{
102+
x = x * 10 + b % 10;
103+
b /= 10;
104+
}
105+
for (long t = mx; t * t >= x; --t)
106+
if (x % t == 0)
107+
return x % 1337;
108+
}
109+
return 9;
110+
}
111+
};
112+
```
55113
114+
### **Go**
115+
116+
```go
117+
func largestPalindrome(n int) int {
118+
mx := int(math.Pow10(n)) - 1
119+
for a := mx; a > mx/10; a-- {
120+
x := a
121+
for b := a; b != 0; b /= 10 {
122+
x = x*10 + b%10
123+
}
124+
for t := mx; t*t >= x; t-- {
125+
if x%t == 0 {
126+
return x % 1337
127+
}
128+
}
129+
}
130+
return 9
131+
}
56132
```
57133

58134
### **...**

solution/0400-0499/0479.Largest Palindrome Product/README_EN.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,89 @@ Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
3636
### **Python3**
3737

3838
```python
39-
39+
class Solution:
40+
def largestPalindrome(self, n: int) -> int:
41+
mx = 10**n - 1
42+
for a in range(mx, mx // 10, -1):
43+
b = x = a
44+
while b:
45+
x = x * 10 + b % 10
46+
b //= 10
47+
t = mx
48+
while t * t >= x:
49+
if x % t == 0:
50+
return x % 1337
51+
t -= 1
52+
return 9
4053
```
4154

4255
### **Java**
4356

4457
```java
58+
class Solution {
59+
public int largestPalindrome(int n) {
60+
int mx = (int) Math.pow(10, n) - 1;
61+
for (int a = mx; a > mx / 10; --a) {
62+
int b = a;
63+
long x = a;
64+
while (b != 0) {
65+
x = x * 10 + b % 10;
66+
b /= 10;
67+
}
68+
for (long t = mx; t * t >= x; --t) {
69+
if (x % t == 0) {
70+
return (int) (x % 1337);
71+
}
72+
}
73+
}
74+
return 9;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int largestPalindrome(int n) {
85+
int mx = pow(10, n) - 1;
86+
for (int a = mx; a > mx / 10; --a)
87+
{
88+
int b = a;
89+
long x = a;
90+
while (b)
91+
{
92+
x = x * 10 + b % 10;
93+
b /= 10;
94+
}
95+
for (long t = mx; t * t >= x; --t)
96+
if (x % t == 0)
97+
return x % 1337;
98+
}
99+
return 9;
100+
}
101+
};
102+
```
45103
104+
### **Go**
105+
106+
```go
107+
func largestPalindrome(n int) int {
108+
mx := int(math.Pow10(n)) - 1
109+
for a := mx; a > mx/10; a-- {
110+
x := a
111+
for b := a; b != 0; b /= 10 {
112+
x = x*10 + b%10
113+
}
114+
for t := mx; t*t >= x; t-- {
115+
if x%t == 0 {
116+
return x % 1337
117+
}
118+
}
119+
}
120+
return 9
121+
}
46122
```
47123

48124
### **...**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int largestPalindrome(int n) {
4+
int mx = pow(10, n) - 1;
5+
for (int a = mx; a > mx / 10; --a)
6+
{
7+
int b = a;
8+
long x = a;
9+
while (b)
10+
{
11+
x = x * 10 + b % 10;
12+
b /= 10;
13+
}
14+
for (long t = mx; t * t >= x; --t)
15+
if (x % t == 0)
16+
return x % 1337;
17+
}
18+
return 9;
19+
}
20+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func largestPalindrome(n int) int {
2+
mx := int(math.Pow10(n)) - 1
3+
for a := mx; a > mx/10; a-- {
4+
x := a
5+
for b := a; b != 0; b /= 10 {
6+
x = x*10 + b%10
7+
}
8+
for t := mx; t*t >= x; t-- {
9+
if x%t == 0 {
10+
return x % 1337
11+
}
12+
}
13+
}
14+
return 9
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int largestPalindrome(int n) {
3+
int mx = (int) Math.pow(10, n) - 1;
4+
for (int a = mx; a > mx / 10; --a) {
5+
int b = a;
6+
long x = a;
7+
while (b != 0) {
8+
x = x * 10 + b % 10;
9+
b /= 10;
10+
}
11+
for (long t = mx; t * t >= x; --t) {
12+
if (x % t == 0) {
13+
return (int) (x % 1337);
14+
}
15+
}
16+
}
17+
return 9;
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def largestPalindrome(self, n: int) -> int:
3+
mx = 10**n - 1
4+
for a in range(mx, mx // 10, -1):
5+
b = x = a
6+
while b:
7+
x = x * 10 + b % 10
8+
b //= 10
9+
t = mx
10+
while t * t >= x:
11+
if x % t == 0:
12+
return x % 1337
13+
t -= 1
14+
return 9

0 commit comments

Comments
 (0)