当前位置:网站首页>剑指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.
}
}
边栏推荐
- Distributed transaction processing solution big PK!
- 账号或密码多次输入错误,进行账号封禁
- 1. Get data - requests.get()
- C语言的文件操作(一)
- MySQL forgot password
- Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
- Minio upload file ssl certificate is not trusted
- MySQL transaction isolation level, rounding
- 对list集合进行分页,并将数据显示在页面中
- matlab abel变换图片处理
猜你喜欢

面试官竟然问我怎么分库分表?幸亏我总结了一套八股文

Interviewer, don't ask me to shake hands three times and wave four times again

A complete introduction to JSqlParse of Sql parsing and conversion

MySQL window function

再见了繁琐的Excel,掌握数据分析处理技术就靠它了

Unity Framework Design Series: How Unity Designs Network Frameworks

目标检测学习笔记

MySQL optimization: from ten seconds to three hundred milliseconds

MySQL事务隔离级别详解

DVWA shooting range environment construction
随机推荐
MySQL forgot password
mysql 的简单运用命令
Unity Framework Design Series: How Unity Designs Network Frameworks
SQL语句中对时间字段进行区间查询
Minesweeper game (written in c language)
Input length must be multiple of 8 when decrypting with padded cipher
sql statement - how to query data in another table based on the data in one table
mysql使用on duplicate key update批量更新数据
matlab simulink欠驱动水面船舶航迹自抗扰控制研究
1. Get data - requests.get()
mysql uses on duplicate key update to update data in batches
Goodbye to the cumbersome Excel, mastering data analysis and processing technology depends on it
Paginate the list collection and display the data on the page
Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
Typec手机有线网卡网线转网口转接口快充方案
DVWA安装教程(懂你的不懂·详细)
MySQL事务(transaction) (有这篇就足够了..)
数据集划分以及交叉验证法
What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?
快速掌握并发编程 --- 基础篇