LC 660. Remove 9 【lock, hard】

Start from integer 1, remove any integer that contains 9 such as 9, 19, 29...

So now, you will have a new integer sequence: 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ...

Given a positive integer n, you need to return the n-th integer after removing. Note that 1 will be the first integer.

Example 1:

Input: 9
Output: 10

Hint: n will not exceed 9 x 10^8.

找规律题。借鉴网上的思路。

1,2,3,4,5,6,7,8,10,。。。,18,20,。。。28,30

把9拿掉以后,可以把它看成一个九进制的序列。也就是说,9进制中的数字n如果把它当作10进制来看,就是在这个序列中的第n个。

class Solution {
public:
    int newInteger(int n) {
        int base = 1;
        int ans = 0;
        while(n != 0){
            ans = ans + (n % 9) * base;
            n /= 9;
            base *= 10;
        }
        return ans;
    }
};

原文地址:https://www.cnblogs.com/ethanhong/p/10146522.html

时间: 2024-08-30 08:52:14

LC 660. Remove 9 【lock, hard】的相关文章

LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

【C#|.NET】实例看lock(?)

这里不考虑分布式或者多台负载均衡的情况只考虑单台机器,多台服务器可以使用分布式锁.出于线程安全的原因,很多种场景大家可能看代码中看到lock的出现,尤其是在资金类的处理环节. 理论常识不多说,回到业务场景,举个例子我们的需求一般就是在某个订单进入某个安全优先级比较高的流程时要针对这笔订单做到线程互斥.至于原因,这里再插一个概念,大部分orm在做更新操作时,实际上做的是全参数更新,所谓全参数更新,假如一个订单表上有10个字段,我们只需要更新其中的一个金额字段,但是在传统orm框架中实际上在订单的实

【C++设计模式】单件类与DCLP(Double Check Lock Pattern)的风险

[单件类] 保证只能有一个实例化对象,并提供全局的访问入口. [设计注意事项] 1.阻止所有实例化的方法: private 修饰构造函数,赋值构造函数,赋值拷贝函数. 2.定义单实例化对象的方法: a.使用static 修饰 b.使用new+delete的方法 3.多线程版本: 使用双检测锁定,即先检测单实例对象是否存在:不存在,使能"锁",再次判断实例是否存在,不存在就创建该单实例对象. A.单层锁示例: Singleton* Singleton::getInstance() { L

Python之路【第二篇】:Python基础(一)

Python之路[第二篇]:Python基础(一) 入门知识拾遗 一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 1 2 3 if 1==1:     name = 'wupeiqi' print  name 下面的结论对吗? 外层变量,可以被内层变量使用 内层变量,无法被外层变量使用 二.三元运算 1 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为假:result = 值2 三.进制 二进制,01 八进

Python全栈开发【基础二】

Python全栈开发[基础二] 本节内容: Python 运算符(算术运算.比较运算.赋值运算.逻辑运算.成员运算) 基本数据类型(数字.布尔值.字符串.列表.元组.字典) 编码与进制转换 Python 运算符 1.算术运算: 2.比较运算: 3.赋值运算: 4.逻辑运算:  5.成员运算: 基本数据类型 1.数字 int(整型) 1 class int(object): 2 """ 3 int(x=0) -> integer 4 int(x, base=10) -&g

转载 IO、文件、NIO【草案四】

本章目录: 1.IO类相关内容 2.文件和目录 3.文件高级操作  NIO详解[1]——缓冲区(Buffer)[深入理解,总结自<Java-NIO>]: [*:下边的Buffer又指代抽象的缓冲区结构模型,同样代表Java语言里面的Buffer类的实例,这里不区分二者的概念了.] Buffer类基本概念: 一般而言,Buffer的数据结构是一个保存了原始数据的数组,在Java语言里面封装成为一个带引用的对象.Buffer一般称为缓冲区,该缓冲区的优点在于它虽然是一个简单数组,但是它封装了很多数

【MySQL笔记】SQL语言四大类语言

SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 1. 数据查询语言DQL 数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHERE子句组成的查询块: SELECT <字段名表> FROM <表或视图名> WHERE <查询条件> 具体参看:[MySQL笔记]数据库的查询 2 .数据操纵语言DML INSERT - insert data into a table(插入) UPDATE - upda

【DDD-Apwork框架】事件总线和事件聚合器

第一步:事件总线和事件聚合器 [1]事件总线 IEventBus IUnitOfWork.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Keasy5.Infrastructure { /// <summary> /// 表示所有集成于该接口的类型都是Unit Of Work的一种实现.