♥A、♥2...
package swp.kaifamiao.codes.Java.d0824;
/*
定义两个数组,一个数组存储扑克牌花色,另一个数组存储扑克牌(A~K),输出52张扑克牌(除大小王)
*/
/**
* {class description}
*
* @author SWP
* @version 1.0.0
*/
public class Poker {
public static void main(String[] args) {
String[] arr1 = {"♥️","♦️","♠️","♣️"};
String[] arr2 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
System.out.printf(arr1[i] + arr2[j] + "\t");
}
System.out.println();
}
}
}
有效字符串需满足:
示例1:
输入: "()"
输出: true
示例2:
输入: "()()()"
输出: true
示例3:
输入: "((()))"
输出: true
示例4:
输入: "(()())"
输出: true
示例5:
输入: "((((((()))" ")()("
输出: false
package swp.kaifamiao.codes.Java.d0824;
import java.util.Scanner;
/**
* {class description}
*
* @author SWP
* @version 1.0.0
*/
public class Brackets {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.printf("请输入只包括“(“与“)”的字符串:");
String str = input.next();
String result = isJudge(str);
System.out.println(result);
}
public static String isJudge(String str){
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == '('){
count++;
} else if (ch == ')'){
count--;
}
if (count < 0){
return "无效的字符串!";
}
}
return count == 0 ? "有效的字符串!" : "无效的字符串!";
}
}
输入: "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
package swp.kaifamiao.codes.Java.d0824;
import java.util.Scanner;
/**
* {class description}
*
* @author SWP
* @version 1.0.0
*/
public class MaxSubstring {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String str = input.next();
String sub = subString(str);
int result = sub.length();
System.out.println("最长子串是:" + sub);
System.out.println("最长子串的长度是:" + result);
}
/**
* 给定一个字符串,请你找出其中不含有重复字符的 **最长子串** 的长度。
* @param str
* @return
*/
public static String subString(String str){
// 存储最长子串
String max = "";
// 子串的起始下标
int start = 0;
for (int end = start + 1; end < str.length(); end++) {
// 子串的范围
String sub = str.substring(start, end);
// 当前位置的字符
String current = str.substring(end, end + 1);
// 当前位置的字符是否在子串中存在
if (sub.indexOf(current) != -1){
// 当前子串和目前最大子串谁大
if (sub.length() > max.length()) {
max = sub;
}
// 找下一个子串的开始坐标
start = str.indexOf(current, start) + 1;
// 最长子串比剩下的长度还长
if (max.length() > str.length() - start - 1){
break;
}
}
}
return max;
}
}
示例1:
输入:strs = ["flower","flow","flight"]
输出:"fl"
示例2:
输入:strs = ["dog","racecar","car"]
输出:""
解释:输入不存在公共前缀。
package swp.kaifamiao.codes.Java.d0824;
import swp.kaifamiao.codes.Java.d0817.Main;
import java.util.PrimitiveIterator;
import java.util.Scanner;
/**
* {class description}
*
* @author SWP
* @version 1.0.0
*/
public class Prefix {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个字符串单词:");
String str1 = input.next();
System.out.print("请输入第二个字符串单词:");
String str2 = input.next();
System.out.print("请输入第三个字符串单词:");
String str3 = input.next();
String[] arr = {str1,str2,str3};
String prefix = commonPrefix(arr);
System.out.println("最长公共前缀是:" + prefix);
}
public static String commonPrefix(String[] arr){
if (arr == null || arr.length == 0){
return "";
}
//将第一个字符串作为初始的公共前缀
String prefix = arr[0];
for (int i = 0; i < arr.length; i++) {
String current = arr[i];
// 从头开始比较当前字符串和公共前缀,更新公共前缀直到找到最长公共前缀或者遍历完所有字符串
int j = 0;
while (j < prefix.length() && j < current.length() && prefix.charAt(j) == current.charAt(j)){
j++;
}
//更新公共前缀
prefix = prefix.substring(0,j);
if (prefix.isEmpty()){
break;
}
}
return prefix;
}
}
例如:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
示例1:
输入:columnNumber = 1
输出:"A"
示例2:
输入:columnNumber = 28
输出:"AB"
示例3:
输入:columnNumber = 701
输出:"ZY"
示例4:
输入:columnNumber = 2147483647
输出:"FXSHRXW"
package swp.kaifamiao.codes.Java.d0824;
//给你一个整数 columnNumber ,返回它在 Excel 表中相对应的列名称。
import swp.kaifamiao.codes.Java.d0817.Main;
import java.util.Scanner;
/**
* {class description}
*
* @author SWP
* @version 1.0.0
*/
public class Excel {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入整数:");
int columnNumber = input.nextInt();
String str = numberColumn(columnNumber);
System.out.println(str);
}
public static String numberColumn(int columnNumber){
StringBuilder sb = new StringBuilder();
// 生成大写字母的字符集合
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (columnNumber >0) {
int remainder = ((columnNumber - 1) % 26);
char ch = characters.charAt(remainder);
sb.insert(0, ch);
columnNumber = (columnNumber - 1) / 26;
}
return sb.toString();
}
}