File tree Expand file tree Collapse file tree 6 files changed +222
-2
lines changed
solution/0400-0499/0479.Largest Palindrome Product Expand file tree Collapse file tree 6 files changed +222
-2
lines changed Original file line number Diff line number Diff line change 44
44
<!-- 这里可写当前语言的特殊实现逻辑 -->
45
45
46
46
``` 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
48
61
```
49
62
50
63
### ** Java**
51
64
52
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
53
66
54
67
``` 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
+ ```
55
113
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
+ }
56
132
```
57
133
58
134
### ** ...**
Original file line number Diff line number Diff line change @@ -36,13 +36,89 @@ Explanation: 99 x 91 = 9009, 9009 % 1337 = 987
36
36
### ** Python3**
37
37
38
38
``` 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
40
53
```
41
54
42
55
### ** Java**
43
56
44
57
``` 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
+ ```
45
103
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
+ }
46
122
```
47
123
48
124
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments