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度旋转后,变成了一个与原数字不同的 ...
随机推荐
- Rabbitmq(5) 路由模式
设置路由键 发送者 package com.aynu.bootamqp.service; import com.aynu.bootamqp.commons.utils.Amqp; import com ...
- [PAClient Error] Error: E4356 File does not exist armv7
[PAClient Error] Error: E4356 File does not exist: /Users/tt/PAServer/scratch-dir/Administrator-snIO ...
- Java Day26进程01天
Java开启多个线程有两种方法,一种继承Thread类,一种实现Runnable接口.具体示例如下: 01继承Thread类 02实现Runnable接口
- ORM的相关操作
http://www.cnblogs.com/liwenzhou/p/8660826.html
- P1147连续自然数和-(尺取法)
https://www.luogu.org/problemnew/show/P1147 题意:输入一个n,求连续几个数加起来等于n,输出这几个连续的数的第一个和最后一个.10<=n<=20 ...
- linux安装redis-4.0.2
一.下载redis cd /usr/local/src wget http://download.redis.io/releases/redis-4.0.2.tar.gz 二.解压 tar -zxvf ...
- SpringBoot打成jar包的配置方式
pom.xml中添加Springboot插件 <build> <plugins> <plugin> <groupId>org.springframewo ...
- php curl抓取类分享
class UsualFunForNetWorkHelper { /*** * post请求数据 */ public static function HttpsPost($url, $data = n ...
- CSS文本超出指定行数省略显示
单行: overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 2行: font-size: 17px;overflow: hi ...
- EF ++属性会更新实体
var lastBaby = await _babyRepository.FirstOrDefaultAsync(); ++lastBaby.sort; -- sort原本为1 -- 最终会生成一条语 ...