面试题——大家进来交流下


还望大家积极评论,说出自己的想法,这样大家互相促进。。。。


题目如下:

The 2020 is a number where index value is equal to number of times occurrence of index in number. for eg: for 2020
index(0)=2 (as 0 occured 2 times in 2020)
index(1)=0 (as 1 occured 0 times)
index(2)=2 (as 2 occured 2 times)
index(3)=0 (as 3 occured 0 times)

Now input given is a number, determine whether the given number posses property same as 2020 (given as example in above question)


因为时间紧张,我很快的写了一个算法,还有很多可以改进的地方,还请大家斧正。

java代码如下:

/**
	 * 因为如果 我们去求这个数的位数会比较费事,所以我们可以把这个数先转换成一个String,然后
	 * 利用字符查找的方法来解决这个问题。
	 */
	public static boolean numberCheck(int num){
		String numStr = ""+num; //先把这个数字包装到字符串中,以方便用每个字符查找
		if(numStr.length()>10){
			return false; //显然如果这个数是十一位数或者更大,根本不能满足
		}
		//char[] numChars  = numStr.toCharArray();
		for(int i =0;i<numStr.length();i++){
			int times = Integer.parseInt(numStr.substring(i,i+1)); //i在num出现的次数
			int count =0; 
			for(int j = 0;j<numStr.length();j++){
				if((String.valueOf(i).equals(String.valueOf(numStr.charAt(j))))){
					++count;
				}
			}
			if(times!=count){
				return false;
			}
		}
		return true;
	}


请大家多多指下,把你的算法也贴出来大家互相学习。。~~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值