LeetCode 788. 旋转数字(Rotated Digits) 36
788. 旋转数字
788. Rotated Digits
题目描述
我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数。要求每位数字都要被旋转。
如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个数是有效的。0, 1, 和 8 被旋转后仍然是它们自己;2 和 5 可以互相旋转成对方;6 和 9 同理,除了这些以外其他的数字旋转以后都不再是有效的数字。
现在我们有一个正整数 N, 计算从 1 到 N 中有多少个数 X 是好数?
每日一算法2019/6/8Day 36LeetCode788. Rotated Digits
示例:
输出: 4
解释:
在[1, 10]中有四个好数: 2, 5, 6, 9。
注意 1 和 10 不是好数, 因为他们在旋转之后不变。
注意:
- N 的取值范围是 [1, 10000]。
Java 实现
class Solution {
public int rotatedDigits(int N) {
int res = 0;
for (int i = 1; i <= N; i++) {
if (isGood(i)) {
res++;
}
}
return res;
}
public boolean isGood(int num) {
boolean flag = false;
int c;
while (num != 0) {
c = num % 10;
num = num / 10;
if (c == 3 || c == 4 || c == 7) {
return false;
}
if (c == 2 || c == 5 || c == 6 || c == 9) {
flag = true;
}
}
return flag;
}
}
参考资料
- https://www.cnblogs.com/grandyang/p/9154892.html
- https://leetcode.com/problems/rotated-digits/
- https://leetcode-cn.com/problems/rotated-digits/
LeetCode 788. 旋转数字(Rotated Digits) 36的更多相关文章
- Java实现 LeetCode 788 旋转数字(暴力)
788. 旋转数字 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字都要被旋转. 如果一个数的每位数字被旋转以后仍然还 ...
- [Swift]LeetCode788. 旋转数字 | Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...
- 【Leetcode_easy】788. Rotated Digits
problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- Java for LeetCode 081 Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- 前端与算法 leetcode 189. 旋转数组
目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单 ...
随机推荐
- 学习Spring-Data-Jpa(十四)---自定义Repository
有些时候,我们需要自定义Repository实现一些特殊的业务场景. 1.自定义单个Repository 1.1.首先提供一个片段接口和实现(接口的实现默认要使用Impl为后缀,实现本身不依赖spri ...
- LeetCode 919. Complete Binary Tree Inserter
原题链接在这里:https://leetcode.com/problems/complete-binary-tree-inserter/ 题目: A complete binary tree is a ...
- SpringBoot之使用Druid连接池,SQL监控和spring监控
项目结构 1.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot< ...
- S1_搭建分布式OpenStack集群_10 cinder 存储节点配置
一.安装配置lvm2安装LVM包:# yum install -y lvm2 启动LVM元数据服务,并将其配置为在系统启动时启动:# systemctl enable lvm2-lvmetad.ser ...
- [RN] React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页头部 效果
React Native 使用 React-native-scrollable-tab-view 实现 类头条 新闻页效果 效果如下: 一.安装依赖 npm install react-native- ...
- [USACO06FEB] Stall Reservations 贪心
[USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...
- Ranger部署
一.Apache Ranger是什么? Apache Ranger是一个框架,Hadoop上对于保护数据数据安全性的安全框架.用于在整个Hadoop平台上启用,监视和管理全面的数据安全性. 二.特性 ...
- vim配置无插件
其实,vim插件会影响编辑器的启动速度,虽然有些插件影响不大,我依然觉得不够,其实通过简易的状态栏,可以显示必要的信息,能自定义颜色和背景甚至透明就足够了. 一.自定义状态栏其实以下内容可以写在一行上 ...
- dedecms 模板文件不存在,无法解析文档的终极各种解决办法
dedecms 模板文件不存在,无法解析文档"的终极各种解决办法 方法一:[此对应喜欢把模板文件使用".html"的格式,] /include/arc.archives. ...
- HAProxy+Keepalived高可用负载均衡
一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3 ...