Sword refers to offer-44-Poker Straight-Java

Questions and tests

package sword044;
/*扑克牌的顺子
从扑克牌中随机抽出5张牌,判断是不是一个顺子,即这五张牌是不是连续的。
2——10为数字本身,A为1,J为11,Q为12,K为13,而大小王为任意数字。
*/


public class main {
	
	public static void main(String[] args) {
		int[][] testTable = {
   
   {1,2,3,2,5},{1,2,3,4,5},{1,2,0,0,4},{1,6,2,7,1}};
		for (int[] ito : testTable) {
			test(ito);
		}
	}
		 
	private static void test(int[] ito) {
		Solution solution = new Solution();
		boolean rtn;
		long begin = System.currentTimeMillis();
		for (int i = 0; i < ito.length; i++) {
		    System.out.print(ito[i]+" ");		    
		}
		System.out.println();
		//开始时打印数组
		
		rtn = solution.IsContinuous(ito);//执行程序
		long end = System.currentTimeMillis();	
		
		//System.out.println(ito + ": rtn=" + rtn);
		System.out.println("rtn=" +rtn);

		
		System.out.println();
		System.out.println("耗时:" + (end - begin) + "ms");
		System.out.println("-------------------");
	}

}

Solution 1

We need to abstract the background of playing cards into computer language. It is not difficult to imagine that we can think of five cards as an array of five numbers. Big and small kings are special numbers, we might as well define them as 0 so that they can be distinguished from other cards.

Next, we analyze and judge whether the 5 numbers are continuous, and the most intuitive way is to sort the array. It is worth noting that since 0 can be used as any number, we can use 0 to fill the gaps in the array. If the sorted array is not continuous, that is, the two adjacent numbers are like several numbers, but as long as we have enough 0 to fill the two vacant numbers, the array is actually continuous. For example, the array is sorted into {0, 1, 3, 4, 5}, and a 2 is vacant between 1 and 3. It happens that we have a 0, that is, we can use it as a 2 to fill the gap.

So we need to do three things: first sort the array, then count the number of 0s in the array, and finally count the total number of vacancies between adjacent numbers in the array after sorting. If the number of vacancies is less than or equal to 0, then the array is continuous; otherwise, it is not continuous.

Finally, we need to pay attention to one point: if the non-zero numbers in the array appear repeatedly, the array is not continuous. The description of changing to playing cards is that if a pair of cards contains a pair, it cannot be a straight.

package sword044;

import java.util.Arrays;

public class Solution {
	public boolean IsContinuous(int[] nums) {
		int length = nums.length;
		if(length != 5) {
			return false;
		}
		Arrays.sort(nums);
		int zeroNum = 0;
		// 先计算0的数目
		for(int i=0;i<5;i++) {
			if(nums[i] == 0) {
				zeroNum++;
			}else {
				break;
			}			
		}
		if(zeroNum == 5 || zeroNum == 4) {
			return true;
		}
		int zeroNeed = 0;
		int prev = nums[zeroNum];
		for(int i= zeroNum+1;i<5;i++) {
			int now = nums[i];
			if(now == prev) {
				return false;
			}
			zeroNeed +=now-prev-1;
			prev = now;
		}
		if(zeroNeed<=zeroNum) {
			return true;
		}else {
			return false;
		}
	}

}

 

Guess you like

Origin blog.csdn.net/xushiyu1996818/article/details/112304319