788. Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.
Now given a positive number
N
, how many numbers X from1
toN
are good?
- Example:
- Input: 10
- Output: 4
- Explanation:
- There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
- Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
Note:
- N will be in range
[1, 10000]
.
Approach #1: DP. [Java]
- class Solution {
- public int rotatedDigits(int N) {
- int[] dp = new int[N+1];
- int count = 0;
- for (int i = 0; i <= N; ++i) {
- if (i < 10) {
- if (i == 0 || i == 1 || i == 8) dp[i] = 1;
- if (i == 2 || i == 5 || i == 6 || i == 9) {
- dp[i] = 2;
- count++;
- }
- } else {
- int a = dp[i/10], b = dp[i%10];
- if (a == 1 && b == 1) dp[i] = 1;
- else if (a >= 1 && b >= 1) {
- dp[i] = 2;
- count++;
- }
- }
- }
- return count;
- }
- }
Analysis:
dp[0] : invalid number
dp[1]: valid and same number
dp[2]: valid and difference number
Reference:
https://leetcode.com/problems/rotated-digits/discuss/117975/Java-dp-solution-9ms
788. Rotated Digits的更多相关文章
- 【Leetcode_easy】788. Rotated Digits
problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...
- LeetCode 788 Rotated Digits 解题报告
题目要求 X is a good number if after rotating each digit individually by 180 degrees, we get a valid num ...
- #Leetcode# 788. Rotated Digits
https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...
- [LeetCode&Python] Problem 788. Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- 788. Rotated Digits 旋转数字
[抄题]: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...
- LeetCode 788. Rotated Digits (旋转数字)
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- 【LeetCode】788. Rotated Digits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 788. 旋转数字(Rotated Digits) 36
788. 旋转数字 788. Rotated Digits 题目描述 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字 ...
- LeetCode算法题-Rotated Digits(Java实现)
这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...
随机推荐
- pyton 模块之 pysmb 文件上传和下载(linux)
首先安装pysmb模块 下载文件 from smb.SMBConnection import SMBConnection conn = SMBConnection('anonymous', '', ' ...
- xhprof 安装详解
准备工作1.xhprof不支持php7,需要php7以下版本2.php扩展模块xhprof下载地址: http://pecl.php.net/get/xhprof-0.9.4.tgz xhprof安装 ...
- AltiumDesigner印制导线的走向及形状
印制导线的走向及形状.在PCB布线时,相邻层的走线方向成正交结构,应避免将不同的信号线在相邻走成同一方向,以减少不必要的层间窜扰.当PCB布线受到结构限制(如某种背板)难以避免出现平行布线时,特别是当 ...
- c#: WebBrowser控制台输出
还是处理视频下载所相关的问题. 有些网站,它的页面代码是由页面加载后js动态生成,那么其原始的html便不能用.页面渲染后的代码,是我们需要的 c#中,我用WebBrowser这个控件处理.设置项目类 ...
- ROLAP、MOLAP和HOLAP区别
对没有使用过数据仓库的人,对这三个概念确实是有点混淆不清.包括我自己本身不是做数据仓库出身,所以实际上是从实践出发,理论基础是有点匮乏的. 一.基本概念 1. OLAP OLAP(on-Line An ...
- [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- js、jquery、jsp的区别
1.JSP全称是java server page JS全称是javaScript 2.最主要的区别是运行位置不同. JSP运行在后台服务器上,混合在HTML中的java程序段用于控制HTML的动 ...
- 4412 uboot启动分析
感谢sea1105, https://blog.csdn.net/sea1105/article/details/52142772 在学习过程中,由于tiny4412资料太过于少,因此参考210的视屏 ...
- (转载)Android开发——Android中常见的4种线程池(保证你能看懂并理解)
0.前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52415337 使用线程池可以给我们带来很多好处,首先通过线程池中线程的重用 ...
- CentOS_mini下安装docker 之 安装docker CE
警告:切勿在没有配置 Docker YUM 源的情况下直接使用 yum 命令安装 Docker. 原文地址:https://yeasy.gitbooks.io/docker_practice/inst ...