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 from 1 to N 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的更多相关文章

  1. 【Leetcode_easy】788. Rotated Digits

    problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...

  2. LeetCode 788 Rotated Digits 解题报告

    题目要求 X is a good number if after rotating each digit individually by 180 degrees, we get a valid num ...

  3. #Leetcode# 788. Rotated Digits

    https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...

  4. [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 ...

  5. 788. Rotated Digits 旋转数字

    [抄题]: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...

  6. LeetCode 788. Rotated Digits (旋转数字)

    X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...

  7. 【LeetCode】788. Rotated Digits 解题报告(Python)

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

  8. LeetCode 788. 旋转数字(Rotated Digits) 36

    788. 旋转数字 788. Rotated Digits 题目描述 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字 ...

  9. LeetCode算法题-Rotated Digits(Java实现)

    这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...

随机推荐

  1. 阿里云ODPS <====>蚂蚁大数据

    1.命令行客户端工具的安装参考文档:http://repo.aliyun.com/odpscmd/?spm=a2c4g.11186623.2.17.5c185c23zHshCq 2.创建和查看表:ht ...

  2. 更改mysql 和jinkins目录

    更改MySQL数据目录 1.修改my.cnf,注销原datadir,增加新的数据目录 #datadir=/var/lib/mysqldatadir=/mysql-data/mysql 2.修改启动脚本 ...

  3. k8s定义Deployment,和service

    定义一个Deployment和service做个简单的笔记 有时候我们需要开放Pod的多个端口,比如nginx的80和443端口,那如何定义Deployment文件呢,定义单个端口如下 apiVers ...

  4. spring boot+kafka整合

    springboot版本是2.0.4 首先,在maven中引入spring-kafka的jar包 <dependency> <groupId>org.springframewo ...

  5. JQuery/JS插件 数组转换为Table

    //数组 转换为 table var arr = [{ "D_AlarmValue": 7.00, "D_Code": "002", &qu ...

  6. 记账本,C,Github,Dao

    package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSe ...

  7. selenium下打开Chrome报错解决

    错误如下: [22516:20196:0704/024642.979:ERROR:install_util.cc(597)] Unable to read registry value HKLM\SO ...

  8. sed 笔记

    sed是一个非交互式文本编辑器,他可以对文本文件和标准输入进行编辑,标准输入可以是来自键盘,文件重定向,字符串,变量甚至来自于管道的文本.sed适用于以下三种场合: 编辑相对交互式文本编辑器而言太大的 ...

  9. Anaconda3(python3.5.2)中安装opencv3

    1 安装Visual C++2015 redistributable 我是64位和32的都安装了,如果你电脑中已经安装了17的,就先卸载了,不然安装不上. 2 安装依赖包Numpy.Scipy Num ...

  10. Linux - 操作系统

    操作系统(科普章节) 目标 了解操作系统及作用 1. 操作系统(Operation System,OS) 操作系统作为接口的示意图 没有安装操作系统的计算机,通常被称为 裸机 如果想在 裸机 上运行自 ...