31 11 times the number of integer binary 1 appearing in 1

31 to obtain the number of integer of 1 to 13 1 arise, and calculates the number of times an integer from 100 to 1300 in 1 occur? To this end he especially counted about 1 to 13 contains the digits 1 has 1,10,11,12,13 therefore appear a total of six times, but for the problem behind him Meizhe. ACMer hope you will help him, and the problem is more generalized, the number of non-negative integer in the range 1 appears (the number of occurrences from 1 to n. 1) can quickly find any.
 
A method of: summarized rules to solve problems
Feeling to see problem-solving ideas, I spent a half life ah
Links: https://www.nowcoder.com/questionTerminal/bd7f978302044eee894445e244c7eee6
Source: Cattle customer network

/ * set N = abcde, where abcde respectively decimal numbers on you.
If you want to calculate the number 1 appears on one hundred, it is influenced by three areas: the numbers on the one hundred, one hundred or less (lower) figures, more than one hundred (high) number.
① If one hundred on the number is 0 the number 1 position is determined by the higher may appear on one hundred. For example: 12013, you can know the situation one hundred appear 1 might be: 100 ~ 199,1100 ~ 1199,2100 ~ 2199 ,, ..., 11100 - 11199, a total of 1200. It can be seen (12) is determined by the higher digit, and is equal to the higher digits (12) multiply the current number of bits (100).
② If one hundred on the number 1 number 1 may appear on one hundred not only by the higher position but also by the impact of low impact. For example: 12113, then you know that one hundred cases by the high impact occurs is: 100 ~ 199,1100 ~ 1199,2100 ~ 2199 ,, ...., 11100 - 11199, a total of 1200. And as in the case above, and is equal to the higher digits (12) multiply the current number of bits (100). But also by the low impact, the case 1 occurs one hundred are: 12100 - 12113, a total of 114, equivalent to the lower digit (113) +1 .
③ If the number one hundred greater than 1 (2 to 9) , the case 1 occurs only on one hundred bits determined by a higher, such as 12,213, it is a case of occurrence of one hundred is: 1199,2100 ~ ~ 100 ~ 199,1100 2199, ..., 11100 ~ 12199 ~ 11199,12100, a total of 1300, and equal to higher digit + 1 (12 + 1) multiply the current number of bits (100). * / 
 
Here  X- [ . 1 , . 9 ], since the  X- = 0 does not comply with the following rule needs to be calculated separately.
 
Subject to the law can be summarized to rise: in solving integer number X 
 
14790 (position of 5/4/3/2/1)

 1 public class Solution {
 2     public int NumberOf1Between1AndN_Solution(int n) {
 3         int result = 0;//总的1的个数
 4         int i = 1;//当前位
 5         int current = 0,after = 0,before = 0;
 6         while((n/i)!= 0){           
 7             current = (n/i)%10; //当前位数字
 8             before = n/(i*10); //高位数字
 9             after = n-(n/i)*i; //低位数字
10             //如果为0,出现1的次数由高位决定,等于高位数字 * 当前位数
11             if (current == 0)
12                 result += before*i;
13             //如果为1,出现1的次数由高位和低位决定,高位*当前位+低位+1
14             else if(current == 1)
15                 result += before * i + after + 1;
16             //如果大于1,出现1的次数由高位决定,//(高位数字+1)* 当前位数
17             else{
18                 result += (before + 1) * i;
19             }    
20             //前移一位
21             i = i*10;
22         }
23         return result;
24     }
25 }

 程序中的1可以改成1--9任意一个数组

 
 
方法二:暴力求解
也能通过牛客的代码,但实际使用中不推荐
依次遍历每个数,判断每个数里面是否包含1
 1 public class Solution {
 2     public int NumberOf1Between1AndN_Solution(int n) {
 3         int result = 0;
 4         for(int i=1;i<=n;i++) result += countOne(i);
 5         return result;
 6     }
 7     public int countOne(int N){
 8         int temp =0;
 9         while(N>0){
10             if(  (N%10) ==  1) temp++; //一个数%10,能保留下最后一位
11             N /= 10;                   //一个数/10,能够舍弃掉最后一位
12         }
13         return temp;
14     }
15 }

 

方法三:转换成字符串,然后去判断每一位是否是1 
 
 1 public class Solution {
 2     public int NumberOf1Between1AndN_Solution(int n) {
 3         int res = 0;
 4         StringBuffer s = new StringBuffer();
 5         for(int i = 1; i<=n; i++){
 6             s.append(i);
 7         }
 8         String str = s.toString();
 9         for(int i=0; i<str.length(); i++){
10             if(str.charAt(i) == '1')
11                 res++;
12         }
13         return res;
14     }
15 }

 

11 二进制中1的个数 

 

 

Guess you like

Origin www.cnblogs.com/shareidea94/p/11102921.html