zoukankan      html  css  js  c++  java
  • [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. 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.

    Now given a positive number N, how many numbers X from 1 to N are good?

    Example:
    Input: 10
    Output: 4
    Explanation: 
    There are four good numbers in the range [1, 10] : 2, 5, 6, 9.
    Note that 1 and 10 are not good numbers, since they remain unchanged after rotating.
    

    Note:

    • N  will be in range [1, 10000].

    思路:

    将区间内每一个数都依次判断是否是good number。

    对于一个数,比如125,变换后为152,则是good number。判断每一位的情况。

    map<int,int>mp;
    
    bool isgood(int n)
    {
        int temp = n;
        int t = 0,newnum = 0;    
        //if  vailid
        int power = 0;
        while(n > 0)
        {
            t = n % 10;
            n /= 10;        
            if( t == 3 || t == 4 || t == 7)return false;
            t = mp[t];
            newnum += t * pow(10,power);
            power++;
        }    
        if(temp != newnum)return true;
        return false;
    }
    int rotatedDigits(int N)
    {mp[0] = 0,mp[1] = 1,mp[8] = 8,mp[2] = 5,mp[5] = 2,mp[6] = 9,mp[9] = 6;
        int cnt = 0;
        for(int i = 1;i <= N;i++)
        if(isgood(i))cnt++;
        return cnt;
    }
  • 相关阅读:
    hadoop集群无法找到datanode节点问题解决
    Startup.A51说明(上)
    UCOSII基础之数据结构
    FPGA之难度
    UCOSII学习笔记【二】
    (转)PCB中各层的含义(protel中)
    UCOSII学习笔记 一
    查看51汇编,解决奇怪的问题
    滑雪
    HMM的理解
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/8474209.html
Copyright © 2011-2022 走看看