1. 判断括号有效性
public static boolean fun5(String str) {
HashMap<Character, Character> hashMap = new HashMap<>();
hashMap.put(')', '(');
hashMap.put(']', '[');
hashMap.put('}', '{');
LinkedList<Character> stack = new LinkedList<>();
for (char c : str.toCharArray()) {
if (hashMap.containsKey(c)) {
if (stack.peek() != hashMap.get(c)) {
return false;
}
stack.pop();
} else {
stack.push(c);
}
}
return stack.isEmpty();
}
2. 输出字符串可能的组合次数
package com.example.helloworld;
import java.util.HashMap;
public class Main1
{
public static void main (String[] args)
{
// 输出字符串可能的组合次数
String str = "AABBCC";
System.out.println(fun(str));
}
public static int fun(String str) {
int count = 0;
if (str == null || "".equals(str)) {
return count;
}
HashMap<Character, Integer> hashMap = new HashMap<>();
for (Character ch : str.toCharArray()) {
if (hashMap.containsKey(ch)) {
int a = hashMap.get(ch);
hashMap.put(ch,++a);
} else {
hashMap.put(ch, 1);
}
}
int len = calculate(str.length());
int result = 1;
for (Integer integer :hashMap.values()){
result = calculate(integer) * result;
}
count = len / result;
return count;
}
public static int calculate(int value) {
if (value == 0) {
return 0;
}
if (value == 1) {
return 1;
}
return value * calculate(value - 1);
}
}
3. 大小写转换
package com.example.helloworld;标签:Java,hashMap,int,代码,hm1,hm2,面试,str,put From: https://www.cnblogs.com/ZZZZhang/p/16775136.html
import java.util.HashMap;
import java.util.Map;
public class Main
{
public static void main (String[] args)
{
/**
* 题目大意
* 定义每个字母的位置:A->1 B->2 …Z->26… a->1 b->2 … z->26
* 给一个只包含大小写字母的字符串,做以下操作
* 1.将字符串升序排序
* 2.对应一个字符,将其位置值的平方除26取余数+1得到新位置,转换成对应位置字符,大写的和大写的转,小写的和小写的转
* 样例
* 样例1:输入:bA 输出:Be
* 解释:bA排序Ab,A对应1,(1 * 1) % 26 + 1 = 2,A转换成B, b对应2,(2 * 2) % 26 + 1 = 5,5对应e,所以最后输出Be
* 样例2: 输入:HuaweiIsBest 输出:EMDbzzdxxkzj
*/
String str= "HuaweiIsBest";
System.out.println(fun(str));
}
public static String fun(String str) {
HashMap<Character, Integer> hm1 = new HashMap<>();
hm1.put('A', 1);
hm1.put('B', 2);
hm1.put('C', 3);
hm1.put('D', 4);
hm1.put('E', 5);
hm1.put('F', 6);
hm1.put('G', 7);
hm1.put('H', 8);
hm1.put('I', 9);
hm1.put('J', 10);
hm1.put('K', 11);
hm1.put('L', 12);
hm1.put('M', 13);
hm1.put('N', 14);
hm1.put('O', 15);
hm1.put('P', 16);
hm1.put('Q', 17);
hm1.put('R', 18);
hm1.put('S', 19);
hm1.put('T', 20);
hm1.put('U', 21);
hm1.put('V', 22);
hm1.put('W', 23);
hm1.put('X', 24);
hm1.put('Y', 25);
hm1.put('Z', 26);
HashMap<Character, Integer> hm2 = new HashMap<>();
hm2.put('a', 1);
hm2.put('b', 2);
hm2.put('c', 3);
hm2.put('d', 4);
hm2.put('e', 5);
hm2.put('f', 6);
hm2.put('g', 7);
hm2.put('h', 8);
hm2.put('i', 9);
hm2.put('j', 10);
hm2.put('k', 11);
hm2.put('l', 12);
hm2.put('m', 13);
hm2.put('n', 14);
hm2.put('o', 15);
hm2.put('p', 16);
hm2.put('q', 17);
hm2.put('r', 18);
hm2.put('s', 19);
hm2.put('t', 20);
hm2.put('u', 21);
hm2.put('v', 22);
hm2.put('w', 23);
hm2.put('x', 24);
hm2.put('y', 25);
hm2.put('z', 26);
char[] chars= str.toCharArray();
for (int i = 0; i < chars.length; i++) {
for (int j = i + 1; j < chars.length; j++) {
if (chars[i] > chars[j]) {
char t = chars[i];
chars[i] = chars[j];
chars[j] = t;
}
}
}
//将其位置值的平方除26取余数+1得到新位置,转换成对应位置字符,大写的和大写的转,小写的和小写的转
StringBuilder sb = new StringBuilder();
for (Character ch : chars) {
if (hm1.containsKey(ch)) {
int v = hm1.get(ch);
int newInt = calculate(v);
for (Map.Entry<Character, Integer> entry : hm1.entrySet()) {
if (entry.getValue() == newInt) {
sb.append(entry.getKey());
}
}
} else if (hm2.containsKey(ch)) {
int v = hm2.get(ch);
int newInt = calculate(v);
for (Map.Entry<Character, Integer> entry : hm2.entrySet()) {
if (entry.getValue() == newInt) {
sb.append(entry.getKey());
}
}
}
}
return sb.toString();
}
public static int calculate(int val){
double v = Math.pow(val, 2) % (double)26;
return (int)(Math.round(v) +1);
}
}