Weekly Contest Leetcode 788. Rotated Digits

Source: Internet
Author: User
Tags valid

X is a good number if after rotating each digit individually by the degrees, we get a valid number that's 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 all other; 6 and 9 rotate to all other, and the rest of the numbers does not rotate to any other number.

Now given a positive number N, what many numbers X from 1 to N is good?

Example:
input:10
output:4
Explanation: 
There is four good numbers in the range [1]: 2, 5, 6, 9.
  note that 1 and is not good numbers, since they remain unchanged after rotating.

Note:n'll is in range [1, 10000]. This problem means that the condition of a number valid is that the number does not contain 3, 4, 7 and contains at least one 2, 5, 6, 9.

I originally intended to use a combination of permutations to determine how many of the number can meet the conditions, but found that the arrangement of the combination of ideas too complex, and put themselves around, in this contest or not time out of the O (n) solution compared insurance.

Instead of traversing directly from 1 to N, determine whether each number satisfies the valid condition. After all, it's an easy question.

public class rotated_digits_788 {
	//018//
	at least one 2569
	//cannot have 347 public
	int rotateddigits (int N) {
		int count=0;
		for (int i=1;i<=n;i++) {
			if (isValid (i)) {
				count++;
			}
		}
		return count;
	}
	
	public boolean isValid (int n) {
		Boolean find2569=false;
		while (n>0) {
			int digit=n%10;
			if (digit==2| | digit==5| | digit==6| | digit==9) {
				find2569=true;
			}
			if (digit==3| | digit==4| | digit==7) {
				return false;
			}
			N=N/10;
		}
		return find2569;
	}

	public static void Main (string[] args) {
		rotated_digits_788 r=new rotated_digits_788 ();
		System.out.println (R.rotateddigits (857));
	}
}

Look at the discuss, there is a great God with the regular do. The regular is quite clear, but the performance is a bit bad.

public int rotateddigits (int N) {
    int count = 0;
    for (int i = 1; I <= N; i++) {
        if (integer.tostring (i). Matches ("^ ([018]*[2569]+[018]*) +$"))
            count++;
    }
    return count;
}

One line in Java 8: public
int rotateddigits (int N) {return Intstream.range (1, n+1). Map (integer.tostring (i) . Matches ("^ ([018]*[2569]+[018]*) +$")? 1:0). sum ();}

But there should be a log (n) solution (written in permutations), and a link to learn: https://leetcode.com/problems/rotated-digits/discuss/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.