2025-02-16 回文串、斗地主

This commit is contained in:
liangjinglin 2025-02-16 16:43:57 +08:00
parent 7897698f68
commit f9affbdd48
2 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package leetcode;
public class Huiwenchuan {
private String longestPalindrome(String str){
int length = str.length();
char[] charArray = str.toCharArray();
boolean[][] dp = new boolean[length][length];
for(int i = 0; i<length; i++){
dp[i][i] = true;
}
int begin = 0;
int max = 1;
for(int i = 0; i<length; i++){
for(int j=0; j<i; j++){
if(charArray[i] != charArray[j]){
dp[i][j] = false;
} else {
if(i-1-(j+1)<2){
dp[i][j] = true;
} else {
dp[i][j] = dp[i-1][j+1];
}
}
if(dp[i][j] && i-j+1>max){
begin = j;
max = i-j+1;
}
}
}
return str.substring(begin, begin+max);
}
public static void main(String[] args) {
Huiwenchuan huiwenchuan = new Huiwenchuan();
System.out.println(huiwenchuan.longestPalindrome("abacab"));
}
}

View File

@ -0,0 +1,95 @@
import java.util.*;
public class Doudizhu {
private void shunzi(String pai){
String[] strArr = pai.split(" ");
int[] arr = new int[strArr.length];
for(int i = 0; i<strArr.length; i++){
if (strArr[i].equals("J")){
arr[i] = 11;
}
else if (strArr[i].equals("Q")){
arr[i] = 12;
}
else if (strArr[i].equals("K")){
arr[i] = 13;
}
else if (strArr[i].equals("A")){
arr[i] = 14;
}
else if (strArr[i].equals("2")){
arr[i] = 15;
}
else {
arr[i] = Integer.parseInt(strArr[i]);
}
}
for(int i = 1; i<arr.length; i++){
int index = i - 1;
int temp = arr[i];
while(index >= 0){
if(arr[index] > temp){
arr[index + 1] = arr[index];
index --;
} else {
break;
}
}
arr[index + 1] = temp;
}
// 打印扑克
for(int i = 0; i<arr.length; i++) {
if (arr[i] == 11) {
System.out.print("J ");
} else if (arr[i] == 12) {
System.out.print("Q ");
} else if (arr[i] == 13) {
System.out.print("K ");
} else if (arr[i] == 14) {
System.out.print("A ");
} else if (arr[i] == 15) {
System.out.print("2 ");
} else {
System.out.print(arr[i] + " ");
}
}
Map<Integer, Object> map = new HashMap<>();
int start = 0;
while(start < arr.length){
List<Integer> shunzilist = new ArrayList<>();
shunzilist.add(arr[start]);
int cur = start + 1;
while(cur < arr.length){
if(arr[cur] - arr[cur - 1] == 1){
shunzilist.add(arr[cur]);
cur ++;
} else if(arr[cur] == arr[cur - 1]){
cur ++;
} else {
start = cur;
break;
}
}
map.put(start, shunzilist);
start = cur;
}
for(Map.Entry<Integer, Object> entry : map.entrySet()){
List<Integer> list = (List)entry.getValue();
System.out.println("顺子:");
if(list.size() < 5){
continue;
}
for(int i:list){
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
Doudizhu doudizhu = new Doudizhu();
doudizhu.shunzi("2 9 J 2 3 4 K A 7 9 A 5 6");
}
}