Leetcode: 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 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].
class Solution {
public int rotatedDigits(int N) {
int count = 0;
for (int i = 1; i <= N; i ++) {
if (isValid(i)) count ++;
}
return count;
}
public boolean isValid(int N) {
/*
Valid if N contains ATLEAST ONE 2, 5, 6, 9
AND NO 3, 4 or 7s
*/
boolean validFound = false;
while (N > 0) {
if (N % 10 == 2) validFound = true;
if (N % 10 == 5) validFound = true;
if (N % 10 == 6) validFound = true;
if (N % 10 == 9) validFound = true;
if (N % 10 == 3) return false;
if (N % 10 == 4) return false;
if (N % 10 == 7) return false;
N = N / 10;
}
return validFound;
}
}
Leetcode: Rotated Digits的更多相关文章
- [LeetCode] 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) 36
788. 旋转数字 788. Rotated Digits 题目描述 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字 ...
- 【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 788. Rotated Digits (旋转数字)
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- LeetCode算法题-Rotated Digits(Java实现)
这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...
- [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 ...
- C#LeetCode刷题之#788-旋转数字(Rotated Digits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3967 访问. 我们称一个数 X 为好数, 如果它的每位数字逐个地 ...
随机推荐
- nginx 代理 websocket
nginx 代理 websocket nginx 首先确认版本必须是1.3以上 map指令的作用: 该作用主要是根据客户端请求中$http_upgrade 的值,来构造改变$connection_up ...
- jvm整理
我 2.JVM内存区域 p{ text-align: center; font-size: 12px; margin: 4px 0 0 0; } .nav-thumb >div{ positio ...
- 分布式调度平台XXL-JOB源码分析-时序图
整体流程 初始化 任务调度 任务执行 任务回调 注册心跳
- LG5325 【模板】Min_25筛
P5325 [模板]Min_25筛 题目背景 模板题,无背景. 题目描述 定义积性函数$f(x)$,且$f(p^k)=p^k(p^k-1)$($p$是一个质数),求 $$\sum_{i=1}^n f( ...
- jpg/jpeg/png格式的区别与应用场景
注:在存储图像时采用JPG还是PNG主要依据图像上的色彩层次和颜色数量进行选择 一..jpg/jpeg格式的图片(jpg全名:jpeg) JPG(或是JPEG): 优点: (1)占用内存小,网页加载速 ...
- linux 下查看进程占用端口和端口号占用进程命令
linux 下查看进程占用端口:(1)查看程序对应的进程号: ps -ef | grep 进程名字 (2)查看进程号所占用的端口号: netstat -nltp | grep 进程号 ubuntu ...
- neo4j安装部署
链接: https://blog.csdn.net/u013946356/article/details/81736232
- 洛谷 P2279 [HNOI2003]消防局的设立 题解
每日一题 day34 打卡 Analysis 这道题的正解本来是树形dp,但要设5个状态,太麻烦了.于是我就用贪心试图做出此题,没想到还真做出来了. 考虑当前深度最大的叶子结点,你肯定要有一个消防局去 ...
- C# 字符串String相关
是否可以继承String类 不能,因为从定义上看String类是sealed类[密封]故不可以继承.当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承.若硬要写,则编译不通过 字符 ...
- C语言实现的简单银行存取款程序 请输入如下数字命令
#include <stdio.h> int main(void) { //提供变量 cmd balance(余额) deposit(存款) withdraw(取款) //利用while做 ...