金典——2的个数____

来源:互联网 发布:网络p2p理财安全吗 编辑:程序博客网 时间:2024/05/17 06:06

题目描述

请编写一个方法,输出0到n(包括n)中数字2出现了几次。

给定一个正整数n,请返回0到n的数字中2出现了几次。

测试样例:
10
返回:1

正解:

import java.util.*;public class Count2 {    public int countNumberOf2s(int n) {        // write code here    int count = 0;    for (int i = 1; i <= n; i *= 10) {        int a = n / i,b = n % i;        //之所以补8,是因为当百位为0,则a/10==(a+8)/10,        //当百位>=2,补8会产生进位位,效果等同于(a/10+1)        count += (a + 7) / 10 * i + ((a % 10 == 2) ? b + 1 : 0);    }    return count;    }}




内存不够:

import java.util.*;public class Count2 {    public int countNumberOf2s(int n) {        // write code here         String string2 = String.valueOf(2);        int count = 0;        int index = 0;        String string ;        while(index<=n){        string = String.valueOf(index);        if(string.contains(string2))count++;        index++;        }        return count;    }}


时间不够:

import java.util.*;public class Count2 {    public int countNumberOf2s(int n) {        // write code here int count=0,i;        if(n<2) return 0;        else if(n<=10) return 1;        else        for(int j=2;j<=n;j++){           i=j;          while(i>0){              if(i%10==2)                  count++;                  i/=10;               if(i==0) break;          }          }        return count;    }}



阅读全文
0 0