原题链接在这里:https://leetcode.com/problems/largest-palindrome-product/description/

题目:

Find the largest palindrome made from the product of two n-digit numbers.

Since the result could be very large, you should return the largest palindrome mod 1337.

Example:

Input: 2

Output: 987

Explanation: 99 x 91 = 9009, 9009 % 1337 = 987

Note:

The range of n is [1,8].

题解:

当n=2时,数字可选区间就是(9, 99]. 最大的product 就是 99*99 = 9801. 最大的位数是2n 肯定是个偶数.

取前一半firstHalf '98', 再拼接'98'的reverse '89'组成新的palindrome '9889'. 然后i在(9,99]之间挨个试palindrome % i == 0.

试不出来firstHalf--, 变'97'重来.

Time Complexity: O((upperBound*upperBound)/10^n  * (upperBound*lowerBound)), upperBound = 10^n, lowerBound = upperBound/10.

Space: O(1).

AC Java:

 class Solution {
public int largestPalindrome(int n) {
if(n == 1){
return 9;
} int upperBound = (int)Math.pow(10, n) - 1;
int lowerBound = upperBound/10;
long max = (long)upperBound * (long)upperBound; int firstHalf = (int)(max/(long)Math.pow(10,n));
boolean palFound = false;
long pal = 0;
while(!palFound){
pal = createPal(firstHalf); for(long i = upperBound;; i--){
if(pal/i>max || i*i<pal){
break;
}
if(pal%i == 0){
palFound = true;
break;
}
} firstHalf--;
}
return (int)(pal%1337);
} private long createPal(int n){
String s = n + new StringBuilder().append(n).reverse().toString();
return Long.valueOf(s);
}
}

LeetCode Largest Palindrome Product的更多相关文章

  1. [LeetCode] Largest Palindrome Product 最大回文串乘积

    Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...

  2. 【欧拉计划4】Largest palindrome product

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/1371281760.html 原创:[欧 ...

  3. 【LeetCode】479. Largest Palindrome Product 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [Swift]LeetCode479. 最大回文数乘积 | Largest Palindrome Product

    Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...

  5. Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

  6. 欧拉计划之Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

  7. (Problem 4)Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

  8. 【easy】479. Largest Palindrome Product

    Find the largest palindrome made from the product of two n-digit numbers Since the result could be v ...

  9. Problem 4: Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...

随机推荐

  1. 本地yum源建立

    一.openstack(ocata)本地yum源的建立: 1.配置yum缓存: vi /etc/yum.conf 把yum.conf配置改为: [main] cachedir=/var/cache/y ...

  2. 建议47:使用logging记录日志信息

    # -*- coding:utf-8 -*- ''' Python中自带的logging 模块提供了日志功能,它将logger 的level 分为5 个级别 DEBUG 详细的信息,在追踪问题的时候使 ...

  3. JSP笔记05——生命周期(转)

    原始内容:https://www.tutorialspoint.com/jsp/jsp_life_cycle.htm 在这一章中,我们将讨论JSP的生命周期. 理解JSP低层次功能的关键在于——理解它 ...

  4. Java中系统时间的获取_currentTimeMillis()函数应用解读

    快速解读 System.currentTimeMillis()+time*1000) 的含义 一.时间的单位转换 1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)1秒=1,000,000 微 ...

  5. C#多线程学习之Thread

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. iOS清除缓存功能开发

    在APP开发中,大量的图片或消息占用系统内存,造成一堆垃圾信息,所以开发清除缓存功能就显得必不可少了. 代码段1:获取文件的大小 - (long long) fileSizeAtPath:(NSStr ...

  7. unbunto关闭触摸屏

    sudo rmmod psmouse 这个是禁用的 sudo modprobe psmouse 这个是启用的

  8. 监控pbs运行状况

    # 监控内存使用情况 job_id=163997workdir=/share_bio/echo "population_sizes" >> $workdir/pbs/p ...

  9. 【P1369】矩形(贪心)

    蒟蒻现在连DP都做不出来了,就只能做一些XJB贪心题,这个题题目向非常友好,100的数据范围一看就是让你跑O(n^4)的做法的,然而实际上并不是那么多,大约是,额,反正要快不少. 没什么好说的,直接枚 ...

  10. Spark常用算子-value数据类型的算子

    package com.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; im ...