问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3967 访问。

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

如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个数是有效的。0, 1, 和 8 被旋转后仍然是它们自己;2 和 5 可以互相旋转成对方;6 和 9 同理,除了这些以外其他的数字旋转以后都不再是有效的数字。

现在我们有一个正整数 N, 计算从 1 到 N 中有多少个数 X 是好数?

输入: 10

输出: 4

解释: 在[1, 10]中有四个好数: 2, 5, 6, 9。注意 1 和 10 不是好数, 因为他们在旋转之后不变。

注意:N 的取值范围是 [1, 10000]。


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?

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].


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3967 访问。

public class Program {

    public static void Main(string[] args) {
var N = 10; var res = RotatedDigits(N);
Console.WriteLine(res); N = 68; res = RotatedDigits2(N);
Console.WriteLine(res); Console.ReadKey();
} private static int RotatedDigits(int N) {
var res = 0;
for(int i = 0; i < N; i++) {
if(IsGoodDigit(i + 1)) res++;
}
return res;
} private static bool IsGoodDigit(int n) {
//先给出映射列表
var dic = new Dictionary<char, char>() {
{'0', '0'},
{'1', '1'},
{'2', '5'},
{'5', '2'},
{'6', '9'},
{'8', '8'},
{'9', '6'}
};
//创建 StringBuilder 加速字符串运算
var sb = new StringBuilder(n.ToString());
//循环计算所有字符串
for(var i = 0; i < sb.Length; i++) {
//不包含时,根据题意直接返回 false
if(!dic.ContainsKey(sb[i])) return false;
else {
//进行“旋转”
sb[i] = dic[sb[i]];
}
}
//跟原串不一样时返回 true
return n.ToString() != sb.ToString();
} private static int RotatedDigits2(int N) {
var res = 0;
for(int i = 0; i < N; i++) {
res += IsGoodDigit2(i + 1) ? 1 : 0;
}
return res;
} private static bool IsGoodDigit2(int n) {
//转换成字符串
var bit = n.ToString();
//包含3,4,7时,直接判定 false
//因为旋转后无效了
if(bit.Contains('3') || bit.Contains('4') || bit.Contains('7'))
return false;
//包含2,5,6,9时,直接判定 true
//因为旋转后值肯定变了,并且代码执行到此处
//说明原串中不包含3、4、7,不可能会无效
if(bit.Contains('2') || bit.Contains('5') || bit.Contains('6') || bit.Contains('9')) {
return true;
}
//其它所有情况直接判定 false 即可
//包含 0,1,8 却不能被上述代码命中
//肯定不是好数
return false;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3967 访问。

4
28

分析:

显而易见,以上2种算法的时间复杂度均为:  。RotatedDigits2 使用了运行库,所以不能认定它的时间复杂度为:  。

C#LeetCode刷题之#788-旋转数字(Rotated Digits)的更多相关文章

  1. LeetCode 788. 旋转数字(Rotated Digits) 36

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

  2. C#LeetCode刷题之#13-罗马数字转整数(Roman to Integer)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3842 访问. 罗马数字包含以下七种字符: I, V, X, L, ...

  3. C#LeetCode刷题之#268-缺失数字(Missing Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4056 访问. 给定一个包含 0, 1, 2, ..., n 中  ...

  4. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  5. 【leetcode刷题笔记】Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

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

    X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...

  7. C#LeetCode刷题之#136-只出现一次的数字(Single Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4046 访问. 给定一个非空整数数组,除了某个元素只出现一次以外, ...

  8. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  9. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

随机推荐

  1. .NET Core微服务开发服务间调用篇-GRPC

    在单体应用中,相互调用都是在一个进程内部调用,也就是说调用发生在本机内部,因此也被叫做本地方法调用:在微服务中,服务之间调用就变得比较复杂,需要跨网络调用,他们之间的调用相对于与本地方法调用,可称为远 ...

  2. web自动化测试实战之生成测试报告

    同志们,老铁们,继上篇文章 web自动化测试实战之批量执行测试用例 之后我们接着继续往下走,有人说我们运行了所有测试用例,控制台输入的结果,如果很多测试用例那也不能够清晰快速的知道多少用例通过率以及错 ...

  3. webpack源码-依赖收集

    webpack源码-依赖收集 version:3.12.0 程序主要流程: 触发make钩子 Compilation.js 执行EntryOptionPlugin 中注册的make钩子 执行compi ...

  4. JAVA 面向对象 三大特征:继承

    什么是继承 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可. 多个类可以称为子类,单独这个类称为父类.超类或者基类. 子类可以直接访 ...

  5. [spring] -- bean作用域跟生命周期篇

    作用域 singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的. prototype : 每次请求都会创建一个新的 bean 实例. request : 每一次HT ...

  6. flask下直接展示mysql数据库 字段

    在工作中,会导出一份mysql的html来查看,用的是就是路过秋天大神的那个工具,所以想自己用那个样式直接在后端写一个页面做展示! 前端页面 from flask import Flask,reque ...

  7. BUUCTF-web ZJCTF,不过如此

    很明显要利用伪协议读next.php base64解码后查看源码 <?php $id = $_GET['id']; $_SESSION['id'] = $id; function complex ...

  8. Java Web(2)-jQuery下

    一.jQuery的属性操作 html() 它可以设置和获取起始标签和结束标签中的内容,跟 dom 属性 innerHTML 一样. text() 它可以设置和获取起始标签和结束标签中的文本, 跟 do ...

  9. python工业互联网应用实战3—模型层构建

    本章开始我们正式进入到实战项目开发过程,如何从需求分析获得的实体数据转到模型设计中来,变成Django项目中得模型层.当然,第一步还是在VS2019 IDE环境重创建一个工程项目,本文我们把工程名称命 ...

  10. 二手99新iPhone8Plus有锁移动联通版

    原文是维基百科:http://www.bosimedia.com/wiki/IPhone_8#cite_note-1 iPhone8Plus详细介绍 iPhone 8与iPhone 8 Plus 是苹 ...