leetCode刷题记录之788. Rotated Digits

题目

X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.
A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.
Now given a positive number N, how many numbers X from 1 to N are good?

题目大意

给定一个数,将它的各个位上的数翻转180度后会得到一个新数,如果新数和原数不相等则这个数是一个好数,注意数字2翻转后得到5,6翻转后得到9,如果有数字不能翻转,则显然它不是一个“好数”,比如数字3,4,7均不可翻转,现在给定一个正整数N,要求返回区间【1,N】之间“好数”的个数

我的代码

/**
 * @param {number} N
 * @return {number}
 */
var rotatedDigits = function(N) {
    let isGoodNum = function(num) {
	let numArray = new String(num).split('');
	let desStr = '';
	for(let index = 0; index < numArray.length; index++)
	{
		switch(numArray[index])
		{
			case '0':
			{
				desStr += '0';
				break;
			}
			case '1':
			{
				desStr += '1';
				break;
			}
			case '2':
			{
				desStr += '5';
				break;
			}
			case '3':
			{
				return false;
			}
			case '4':
			{
				return false;
			}
			case '5':
			{
				desStr += '2';
				break;
			}
			case '6':
			{
				desStr += '9';
				break;
			}
			case '7':
			{
				return false;
			}
			case '8':
			{
				desStr += '8';
				break;
			}
			case '9':
			{
				desStr += '6';
				break;
			}
		}
	}
	if(desStr != numArray.join(''))
	{
		return true;
	}	
	else
	{
		return false;
	}	
    }
    let count = 0;
    for(let i = 1; i <= N; i++)
    {
        if(isGoodNum(i))
        {
            count++;
        }	
    }
    return count;
};
发布了236 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/gunsmoke/article/details/104704149