这个题有个坑,不要使用while(nums!=0)来计算火柴数量
查看原帖
这个题有个坑,不要使用while(nums!=0)来计算火柴数量
732415
Molie楼主2023/6/17 21:40
public static int help1(int nums) {
		//这种方法会导致和为0的时候得不到计算
		int ans = 0;
		while (nums != 0) {
        //arr是0-9的使用火柴数量
			ans += arr[nums % 10];
			nums /= 10;
		}
		return ans;
	}
    
//推荐使用下面计算和的花费棒数
public static int help(int nums) {
		String s = nums + "";
		int ans = 0;
		for (int i = 0; i < s.length(); ++i) ans += (arr[s.charAt(i) - '0']);
		return ans;
	}
2023/6/17 21:40
加载中...