问题描述:
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. 解决sql2014的distribution系统库distribution.mdf过大问题
  2. 安卓收取费用_作为自由职业者应收取的费用:以价值为基础的定价是否能达到炒作的目的?...
  3. php aapt apk 包名,aapt 命令可应用于查看apk包名、主activity、版本等很多信息
  4. windows7 来了
  5. [mybatis]缓存_缓存有关的设置以及属性
  6. MySQL入门之扩展匹配符、order by、limit、union、group by、having子句
  7. 复习vue笔记第一天
  8. [Ext JS 4] 布局之实战一 - 中间区块不会自动伸展 (tab)
  9. IDEA开发vue.js卡顿
  10. 在linux中docker安装mysql_Linux系统:用docker安装mysql。
  11. 达观RPA自动订单处理机器人, 助力制造业订单处理效率提升
  12. Spring Configuration Check ----Unmapped Spring configuration files found.
  13. 揭秘青岛富二代接班路线 曝红领集团小美女总裁(图)-青青岛社区
  14. linux嵌入式主要学什么软件,嵌入式软件工程师需要学什么?嵌入式Linux开发的工作内容...
  15. 英特尔cpu发布时间表_英特尔延长CPU发布周期:摩尔定律终结
  16. Javascript 8421
  17. 第1131期AI100_机器学习日报(2017-10-23)
  18. 淘宝能承受几百上亿的访问点击,而铁道部的网站为啥分分钟崩溃?
  19. 编译原理第二版6.2答案
  20. go操作MongoDB

热门文章

  1. java毕业设计房产置购门户网站Mybatis+系统+数据库+调试部署
  2. [一起学习pytorch吧]之torch.sign函数
  3. 弘辽科技:淘宝突然下架宝贝是什么原因而导致的呢?
  4. webrtc QoS -服务质量总结
  5. 个人信息管理系统c语言作业,个人物品信息管理系统 C语言.doc
  6. python调用接口返回401_python接口测试之401错误的分析和解决(十六)
  7. 7-9 哈利·波特的考试(C语言)
  8. css颜色的渐变百分比 linear-gradient
  9. 【Linux内核分析与驱动编程】添加内核补丁
  10. [游戏代码]求生之路插件:倒计时写法