当前位置:网站首页>剑指offer基础版 ----第31天
剑指offer基础版 ----第31天
2022-07-31 05:09:00 【米兰的小红黑】

class Solution {
public int cuttingRope(int n) {
if(n <= 3)
return n - 1;
int b = n % 3, p = 1000000007;
long ret = 1;
int lineNums=n/3; //线段被我们分成以3为大小的小线段个数
for(int i=1;i<lineNums;i++) //从第一段线段开始验算,3的ret次方是否越界。注意是验算lineNums-1次。
ret = 3*ret % p;
if(b == 0)
return (int)(ret * 3 % p); //刚好被3整数的,要算上前一段
if(b == 1)
return (int)(ret * 4 % p); //被3整数余1的,要算上前一段
return (int)(ret * 6 % p); //被3整数余2的,要算上前一段
}
}

class Solution {
public int countDigitOne(int n) {
// mulk 表示 10^k
// 在下面的代码中,可以发现 k 并没有被直接使用到(都是使用 10^k)
// 但为了让代码看起来更加直观,这里保留了 k
long mulk = 1;
int ans = 0;
for (int k = 0; n >= mulk; ++k) {
ans += (n / (mulk * 10)) * mulk + Math.min(Math.max(n % (mulk * 10) - mulk + 1, 0), mulk);
mulk *= 10;
}
return ans;
}
}

class Solution {
public int findNthDigit(int n) {
int digit = 1;
long start = 1;
long count = 9;
while (n > count) {
// 1.
n -= count;
digit += 1;
start *= 10;
count = digit * start * 9;
}
long num = start + (n - 1) / digit; // 2.
return Long.toString(num).charAt((n - 1) % digit) - '0'; // 3.
}
}
边栏推荐
- Apache DButils使用注意事项--with modifiers “public“
- MYSQL一站式学习,看完即学完
- CentOS7 - yum install mysql
- SQL row-column conversion
- Unity mobile game performance optimization series: performance tuning for the CPU side
- pycharm专业版使用
- Temporal线上部署
- 基于web3.0使用钱包Metamask的三方登陆
- 面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
- Flink sink redis 写入Redis
猜你喜欢

Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode

TOGAF之架构标准规范(一)

110 MySQL interview questions and answers (continuously updated)

MySQL transaction isolation level, rounding

分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ

The monitoring of Doris study notes

mysql stored procedure

The interviewer asked me TCP three handshake and four wave, I really

Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构

Summary of MySQL common interview questions (recommended collection!!!)
随机推荐
The monitoring of Doris study notes
有了MVC,为什么还要DDD?
MySQL常见面试题汇总(建议收藏!!!)
Flink sink redis 写入Redis
【LeetCode-SQL每日一练】——2. 第二高的薪水
面试官,不要再问我三次握手和四次挥手
【一起学Rust】Rust的Hello Rust详细解析
<urlopen error [Errno 11001] getaddrinfo failed>的解决、isinstance()函数初略介绍
Unity mobile game performance optimization series: performance tuning for the CPU side
【一起学Rust】Rust学习前准备——注释和格式化输出
Refinement of the four major collection frameworks: Summary of List core knowledge
.NET-9. A mess of theoretical notes (concepts, ideas)
Centos7 install mysql5.7 steps (graphical version)
sql语句之多表查询
MySQL-Explain详解
Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
Three oj questions on leetcode
对list集合进行分页,并将数据显示在页面中
SQL row-column conversion
Redis的初识