File tree Expand file tree Collapse file tree 3 files changed +84
-35
lines changed
src/main/java/com/github/chen0040/leetcode/day19/easy Expand file tree Collapse file tree 3 files changed +84
-35
lines changed Original file line number Diff line number Diff line change @@ -403,6 +403,7 @@ codes may not be optimized
403
403
1 . [ Path Sum III] ( https://leetcode.com/problems/path-sum-iii/description/ )
404
404
1 . [ Minimum Moves to Equal Array Elements] ( https://leetcode.com/problems/minimum-moves-to-equal-array-elements/description/ )
405
405
1 . [ Repeated Substring Patterns] ( https://leetcode.com/problems/repeated-substring-pattern/description/ )
406
+ 1 . [ Largest Panlindrome Product] ( https://leetcode.com/problems/largest-palindrome-product/description/ )
406
407
407
408
### Day 19 - Medium
408
409
Original file line number Diff line number Diff line change
1
+ package com .github .chen0040 .leetcode .day19 .easy ;
2
+
3
+
4
+ /**
5
+ * Created by xschen on 14/8/2017.
6
+ *
7
+ * link: https://leetcode.com/problems/largest-palindrome-product/description/
8
+ */
9
+ public class LargestPanlinedromeProduct {
10
+ public class Solution {
11
+ public int largestPalindrome (int n ) {
12
+
13
+ if (n == 1 ) return 9 ;
14
+
15
+ int upperBound = (int )Math .pow (10 , n ) - 1 ;
16
+ int lowerBound = (int )Math .pow (10 , n -1 );
17
+
18
+ long maxNumber = (long )upperBound * (long )upperBound ;
19
+
20
+ int firstHalf = (int )(maxNumber / (long )Math .pow (10 , n ));
21
+ while (true ) {
22
+ long candidate = createPanlindrome (firstHalf );
23
+ for (long i = upperBound ; i >= lowerBound ; --i ) {
24
+ if (i * i < candidate ) {
25
+ break ;
26
+ }
27
+
28
+ if (candidate % i == 0 ) {
29
+ return (int )(candidate % 1337 );
30
+ }
31
+ }
32
+ firstHalf --;
33
+ if (firstHalf < 0 ) break ;
34
+ }
35
+ return -1 ;
36
+ }
37
+
38
+ long createPanlindrome (int firstHalf ) {
39
+ StringBuilder sb = new StringBuilder ();
40
+ sb .append (firstHalf );
41
+ sb .reverse ();
42
+ String str = firstHalf + sb .toString ();
43
+ return Long .parseLong (str );
44
+
45
+ }
46
+ }
47
+ }
You can’t perform that action at this time.
0 commit comments