1.剑指 Offer 14- II. 剪绳子 II
1 class Solution {
2 public:
3 int cuttingRope(int n) {
4 if(n <= 3) return n - 1;
5 long res = 1;
6 while(n > 4){
7 n -= 3;
8 res = (res * 3) % 1000000007;
9 }
10 res = (res * n) % 1000000007;
11 return (int)res;
12 }
13 };
2.剑指 Offer 43. 1~n 整数中 1 出现的次数
1 class Solution {
2 public:
3 int countDigitOne(int n) {
4 long digit = 1;
5 int high = n / 10,cur = n % 10,low = 0,res = 0;
6 while(high != 0 || cur != 0){
7 if(cur == 0) res += high * digit;
8 else if(cur == 1) res += (high * digit) + low + 1;
9 else res += (high + 1) * digit;
10 low += cur * digit;
11 cur = high % 10;
12 high /= 10;
13 digit *= 10;
14 }
15 return res;
16 }
17 };
3.剑指 Offer 44. 数字序列中某一位的数字
1 class Solution {
2 public:
3 int findNthDigit(int n) {
4 int digit = 1;
5 long start = 1;
6 long count = 9;
7 while(n > count){
8 n -= count;
9 digit += 1;
10 start *= 10;
11 count = start * 9 * digit;
12 }
13 int num = start + (n - 1) / digit;
14 return to_string(num)[(n - 1) % digit] - '0';
15 }
16 };