斗地主最大顺子查找

 

package cn.longxingtx.number;

import java.util.ArrayList;

public class Straight {
	
	private int[] straight_arr;
	
	public int[] getStraight_arr() {
		return straight_arr;
	}

	public void setStraight_arr(int[] straightArr) {
		straight_arr = straightArr;
	}

	private void sort() {
		int a;
		for(int i = 0; i < straight_arr.length; i++) {
			for (int j = i+1; j < straight_arr.length; j++) {
				if(straight_arr[i] > straight_arr[j]) {
					a = straight_arr[i];
					straight_arr[i] = straight_arr[j];
					straight_arr[j] = a;
				}
			}
		}
	}
	
	public ArrayList straightArray(int _num) {
		sort();
		ArrayList list = getStraight(_num);
		return list;
	}
	
	private ArrayList getStraight(int _num) {
		ArrayList arraylist = new ArrayList();
		ArrayList arraylist2 = new ArrayList();
		int num = _num;
		int k = 0;
		
		int size = 5;
		if(num == 1) {
			size = 5*num;
		} else if(num == 2) {
			size = 3*num;
		} else if(num == 3) {
			size = 2*num;
		}
		for (int i = 0; i < straight_arr.length; i++) {
			if(i == 0) {
				arraylist.add(straight_arr[i]);
				k ++;
			} else {
				if(k == num) {
					if(straight_arr[i] == straight_arr[i - 1]) {
						continue;
					} 
					if(straight_arr[i] != straight_arr[i - 1] + 1) {
						if(arraylist.size() >= size && arraylist2.size() < arraylist.size()) {
							arraylist2.clear();
							arraylist2.addAll(arraylist);
						}
						arraylist.clear();
					}
					arraylist.add(straight_arr[i]);
					k = 1;
				} else {
					if(straight_arr[i] != straight_arr[i - 1]) {
						for(int m = 0; m < k; m++) {
							arraylist.remove(arraylist.size() - 1);
						}
						if(arraylist.size() >= size && arraylist2.size() < arraylist.size()) {
							arraylist2.clear();
							arraylist2.addAll(arraylist);
						}
						arraylist.clear();
						k = 0;
					}
					arraylist.add(straight_arr[i]);
					k ++;
				}
			}
		}
		return arraylist2;
	}
	
	public static void main(String[] args) {
		Straight st = new Straight();
		int[] array = {1, 2, 5, 3, 4, 3, 2, 2, 8, 9, 5, 6, 12, 13, 6, 8, 9, 4, 5, 7, 2, 7, 9, 11, 13, 5, 6, 7};
		st.setStraight_arr(array);
		System.out.println(st.straightArray(1));
		System.out.println(st.straightArray(2));
		System.out.println(st.straightArray(3));
	}
	
}

你可能感兴趣的:(算法)