问题描述:
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相关推荐

  1. LeetCode 788. Rotated Digits

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

  2. C#LeetCode刷题之#788-旋转数字(Rotated Digits)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3967 访问. 我们称一个数 X 为好数, 如果它的每位数字逐个地 ...

  3. LeetCode Remove K Digits

    原题链接在这里:https://leetcode.com/problems/remove-k-digits/description/ 题目: Given a non-negative integer  ...

  4. [Swift]LeetCode788. 旋转数字 | Rotated Digits

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  5. [leetcode][math] Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  6. LeetCode之Add Digits

    1.题目 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...

  7. LeetCode 258 Add Digits

    leetcode 上做了一题比较有意思,记录一下,传送门:https://leetcode.com/problems/add-digits/description/ 题目意思是:给一个正整数a,让其个 ...

  8. LeetCode 788. 旋转数字

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

  9. LeetCode 258. Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

最新文章

  1. c语言函数库哪里keyk,[精品]C语言库函数(字母G-K)-教案.doc
  2. CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
  3. javascript 模式学习篇---基础
  4. ESXi上的固态硬盘识别为非SSD
  5. 如何看exe文件源代码_杀进程、删文件...看新型勒索软件RobbinHood如何干掉杀毒软件...
  6. 6kyu Steps in k-prime
  7. 安装Cornerstone3.1注意点
  8. 将select中的项从一个移动到另一个select中
  9. Mysql 中 delete 与 left join 的问题
  10. Java8 Stream详解~遍历/匹配(foreach/find/match)
  11. Spring入门篇——第4章 Spring Bean装配(下)
  12. 数据挖掘与python实践心得体会_数据挖掘心得体会
  13. 数学之美系列—布隆过滤器(Bloom Filter)
  14. 安卓虚拟pc悬浮键盘_PC端鼠标vs手机端手势:两种交互有何异同点?
  15. STM32F103C8T6 操作矩阵键盘
  16. 图数据库 Dgraph 学习笔记
  17. html 怎么获取焦点的位置,jQuery怎么获取焦点?
  18. UG NX 12 点集
  19. Mysql数据库知识点归纳汇总
  20. Flutter 画面渲染的全面解析

热门文章

  1. 【BFS】C++羊和狼
  2. 5只猫5天吃5只老鼠,200分钟捉200只老鼠,需要多少只猫
  3. js 下载服务器上的文件
  4. Altium Designer 蛇行 等长 布线
  5. 【财务】FMS财务管理系统---质保金与预付款
  6. 第三方支付接口,银行接口(附下载),third party bank interface
  7. 人机界面设备行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  8. 消息加密和解密(一)
  9. 2019款艾力绅的噪音测试软件,本田艾力绅高速噪音测试
  10. 1.14组会 ------ Multi-modal Graph Learning 论文精读