A report on the problem of sword-pointing offer (Java Edition)--Poker Shun Zi 44

Source: Internet
Author: User

??

Analyze problems

??

The key point of this topic is that the size of the king can be regarded as arbitrary numbers, then we think of this arbitrary number as 0 is also possible, because poker in 1-15 have numbers, so you can use 0 to represent the size of the king

??

So we convert the problem to 5 numbers from 0-15 to see if the problem is continuous, because 0 can be considered as arbitrary numbers, so we can use 0 to fill the number of vacancies, such as 0,1,3 is also a continuous

??

To remember the key point or this arbitrary number, consider a problem, that is, if the number of 5 in the number of 0 is greater than the number of vacancies, then the pit must be able to fill up, so must be continuous, if the number of 0 is less than the number of vacancies, then 0 the number is not enough to fill all the pits, so it is discontinuous

??

This problem is converted to look at a sorted array of more than 0 or interval number of the problem, see, the problem is constantly in the conversion, and step by step cobwebs the final answer slowly surfaced

??

Solve the problem

??

First of all, we first sorted out the number of 5 , Arrays.sort (array), and then iterate through the array of 0 of the number of arrays, in addition, if you find two zeros, there is no need to continue, you can jump out of the loop

??

for (int i=0;i<array.length;i++) {

if (array[i]==0) numof0++;

if (numof0==2) break;

}

??

Alternatively, you can put the if judgment statement in the For loop, so that you can enter the number of times the loop body, reduce the number of traversal

??

for (int i=0;i<array.length&& (array[i]==0) && (numof0!=2); i++) numof0++;

??

Then start with the first index1 not 0 , int index1=numof0; compare index1 and index2=index1+1 gaps between adjacent two

??

while (Index2<array.length) {

// If there are two of the same, then the instructions must not be continuous

if (Array[index1]==array[index2]) {

return false;

}

Numofdif+=array[index2]-array[index1]-1;

// implement two adjacent comparisons to move back

Index1=index2;

index2++;

}

??

Finally, the number of 0 and the number of pits return (numof0>numofdif?true:false) are compared.

A report on the problem of sword-pointing offer (Java Edition)--Poker Shun Zi 44

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.