what if remove number 7?


  • 5

    is there any one rememeber there is a leetcode question about how to calculate how many number which contains like number #7?


  • 0

    Good question and I am thinking about this too. As far as I think, since whenever there is a '9' before it there should always be a '7'. So, I think we should only take those '7' and '8' where '9' do not cover into consideration compared to 'Remove 9'.


  • 8

    I think remove 7 is same as remove 9 with mapping 7->8, 8->9. For example, you get answer as 18317 and the final answer would be 19318.


  • 0

    @roc571 yes you are right, it is a good way to modify the input


  • 1
    K

    @roc571 Could you help me understand why remove 7 is the same? The remaining numbers are different, they are not 7-based numbers.


  • 2

    @knightzf removing any number is still 9-base. Instead of using 0-8, you need to use 0-9 without the special number. Therefore, remapping would be easy way.


  • 0
    This post is deleted!

  • 0
    V

    @roc571 said in what if remove number 7?:

    I think remove 7 is same as remove 9 with mapping 7->8, 8->9. For example, you get answer as 18317 and the final answer would be 19318.

    This isn't same as remove 9. Here we are dealing with remainders and number space can shrink from ends alone. You can not create holes and later map the digits, they've value and play role in determining the final value.


  • 0
    C

    @roc571 said in what if remove number 7?:

    I think remove 7 is same as remove 9 with mapping 7->8, 8->9. For example, you get answer as 18317 and the final answer would be 19318.

    It is correct. Written in C++ and tested for [1, 1000].

    int helper(int n) {
        int ans = 0, base = 1;
        while(n != 0) {
            int rem = n % 9;
            n /= 9;
            if(rem >= 7) rem++;
            ans = ans + rem * base;
            base *= 10;
        }
        return ans;
    }
    
    int main(int argc, const char * argv[]) {
        // insert code here...
        for(int i = 1; i <= 1000; i++)
            cout << i << " " << helper(i) << endl;
        return 0;
    }
    

  • 0

    @theCodeMonkey.John How did you test it? Your code just prints it out.


  • 0
    J

    As long as we just remove one number, the answer will always be similar to remove 9, but we need to remap the numbers after the removed one. For instance, after removing 7, then 8 will "be considered as 7", "9 considered as 8".
    Thus, for remove 9, a streak like
    ...15,16,17,18,20...
    will just be transformed to
    ...15,16,18,19,20...


  • 0
    M

    Just do the same, then convert all 7s to 8, all 8s to 9


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.