day31
作者:互联网
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 出现的次数
https://leetcode.cn/leetbook/read/illustration-of-algorithm/57nyhd/
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 };
标签:10,digit,cur,int,res,day31,high 来源: https://www.cnblogs.com/balabalabubalabala/p/16529617.html