Leetcode 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 (on this case they are rotated in a different direction, in other words 2 or 5 gets mirrored); 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?
解释说明:我们要把一个数的每一位旋转180度,若旋转后的数字依然是数字,而且与原数字不同,则我们认为原数字是好数字。
对应关系:1->1, 2->5, 3->inValid, 4->inVaild, 5->2, 6->9, 7->inValid, 8->8, 9->6, 0->0
比如说126旋转以后是159, 且159!=126,所以126是好数字;232含有不能有效旋转的3,故不是好数字;8811旋转后还是8811,故也不是好数字
代码如下:
class Solution {public int rotatedDigits(int n) {int counter=0;for(int i=1; i<=n; i++){if(isValid(i)) counter++;}return counter;}private boolean isValid(int n){String str_original=Integer.toString(n);String str_new="";for(int i=0; i<str_original.length(); i++){if((str_original.charAt(i)=='3')||(str_original.charAt(i)=='4')||(str_original.charAt(i)=='7')){return false;}else{str_new=str_new+rotating(str_original.charAt(i));}}return !str_original.equals(str_new);}private char rotating(char a){if(a=='0') return '0';else if(a=='1') return '1';else if(a=='2') return '5';else if(a=='5') return '2';else if(a=='6') return '9';else if(a=='8') return '8';else return '6';}
}
时间复杂度: 因为每个数字n的长度与其数值是log(n)关系,所以时间复杂度是log1+log2+log3+log4+…+logn=log(n!)=O(nlogn)
Leetcode 788: Rotated Digits相关推荐
- LeetCode 788. Rotated Digits
题目: X is a good number if after rotating each digit individually by 180 degrees, we get a valid numb ...
- C#LeetCode刷题之#788-旋转数字(Rotated Digits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3967 访问. 我们称一个数 X 为好数, 如果它的每位数字逐个地 ...
- LeetCode Remove K Digits
原题链接在这里:https://leetcode.com/problems/remove-k-digits/description/ 题目: Given a non-negative integer ...
- [Swift]LeetCode788. 旋转数字 | Rotated Digits
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- [leetcode][math] Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- LeetCode之Add Digits
1.题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
- LeetCode 258 Add Digits
leetcode 上做了一题比较有意思,记录一下,传送门:https://leetcode.com/problems/add-digits/description/ 题目意思是:给一个正整数a,让其个 ...
- LeetCode 788. 旋转数字
1. 题目 我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数.要求每位数字都要被旋转. 如果一个数的每位数字被旋转以后仍然还是一个数 ...
- LeetCode 258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
最新文章
- c语言函数库哪里keyk,[精品]C语言库函数(字母G-K)-教案.doc
- CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
- javascript 模式学习篇---基础
- ESXi上的固态硬盘识别为非SSD
- 如何看exe文件源代码_杀进程、删文件...看新型勒索软件RobbinHood如何干掉杀毒软件...
- 6kyu Steps in k-prime
- 安装Cornerstone3.1注意点
- 将select中的项从一个移动到另一个select中
- Mysql 中 delete 与 left join 的问题
- Java8 Stream详解~遍历/匹配(foreach/find/match)
- Spring入门篇——第4章 Spring Bean装配(下)
- 数据挖掘与python实践心得体会_数据挖掘心得体会
- 数学之美系列—布隆过滤器(Bloom Filter)
- 安卓虚拟pc悬浮键盘_PC端鼠标vs手机端手势:两种交互有何异同点?
- STM32F103C8T6 操作矩阵键盘
- 图数据库 Dgraph 学习笔记
- html 怎么获取焦点的位置,jQuery怎么获取焦点?
- UG NX 12 点集
- Mysql数据库知识点归纳汇总
- Flutter 画面渲染的全面解析
热门文章
- 【BFS】C++羊和狼
- 5只猫5天吃5只老鼠,200分钟捉200只老鼠,需要多少只猫
- js 下载服务器上的文件
- Altium Designer 蛇行 等长 布线
- 【财务】FMS财务管理系统---质保金与预付款
- 第三方支付接口,银行接口(附下载),third party bank interface
- 人机界面设备行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
- 消息加密和解密(一)
- 2019款艾力绅的噪音测试软件,本田艾力绅高速噪音测试
- 1.14组会 ------ Multi-modal Graph Learning 论文精读