学习内容
学习了有关栈的相关知识。
回文数
- 什么是回文数
“回文”是指正读反读都能读通的句子,它是古今中外都有的一种修辞方式和文字游戏,如“我为人人,人人为我”等。在数学中也有这样一类数字有这样的特征,成为回文数。 - 第一个函数代码展示
private static void solution01() {
String text = "上海自来水来自海上";
ArrayStack<Character> stack = new ArrayStack<>();
for (int i = 0; i < text.length(); i++) {
if (text.length() % 2 == 1 && i == text.length() / 2) {
continue;
}
char c = text.charAt(i);
if (stack.isEmpty()) {
stack.push(c);
} else {
if (c != stack.peek()) {
stack.push(c);
} else {
stack.pop();
}
}
}
System.out.println(stack.isEmpty());
}
以上函数存在Bug比如112233,也会被认为是回文,因此进行改进。
3.改进后的函数
private static boolean solution02() {
String text = "上海自来水来自海上";
int i = 0;
int j = text.length() - 1;
while (true) {
if (text.charAt(i) == text.charAt(j)) {
i++;
j--;
} else {
return false;
}
if (j <= i) {
return true;
}
}
}
- 完整代码展示:
package p2.XianXingJieGou;
//判断回文
public class JudgingPalindrome {
public static void main(String[] args) {
solution01();
System.out.println(solution02());
}
private static boolean solution02() {
String text = "上海自来水来自海上";
int i = 0;
int j = text.length() - 1;
while (true) {
if (text.charAt(i) == text.charAt(j)) {
i++;
j--;
} else {
return false;
}
if (j <= i) {
return true;
}
}
}
private static void solution01() {
String text = "上海自来水来自海上"; //112233
ArrayStack<Character> stack = new ArrayStack<>();
for (int i = 0; i < text.length(); i++) {
if (text.length() % 2 == 1 && i == text.length() / 2) {
continue;
}
char c = text.charAt(i);
if (stack.isEmpty()) {
stack.push(c);
} else {
if (c != stack.peek()) {
stack.push(c);
} else {
stack.pop();
}
}
}
System.out.println(stack.isEmpty());
}
}
- 运行结果展示:
括号匹配
- 理论
将各括号相互对应匹配。 - 代码展示(两中解决方案):
第一种代码展示
public class MatchBracket {
public static void main(String[] args) {
solution01();
}
private static void solution01() {
String str = "{()[[()]]<>{}()<>}()";
ArrayStack<Character> stack = new ArrayStack<>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (stack.isEmpty()) {
stack.push(c);
} else {
char top = stack.peek();
if (top - c == -1 || top - c == -2) {
stack.pop();
} else {
stack.push(c);
}
}
}
System.out.println(stack.isEmpty());
}
第二种方法,则是用map集合来实现的。
代码展示:
private static void solution02() {
String str = "{()[[()]]<{()>}()";
HashMap<Character,Character> map = new HashMap<>();
map.put('[',']');
map.put('<','>');
map.put('(',')');
map.put('{','}');
ArrayStack<Character> stack = new ArrayStack<>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (stack.isEmpty()) {
stack.push(c);
} else {
char top = stack.peek();
if (map.containsKey(top) && c == map.get(']')) {
stack.pop();
} else {
stack.push(c);
}
}
}
System.out.println(stack.isEmpty());
}
十进制与十六进制互转
- 十进制转十六进制
代码展示:
package p2.XianXingJieGou;
//十进制 转 十六进制
public class DecToHex {
public static void main(String[] args) {
int num = 654321;
ArrayStack<String> stack = new ArrayStack<>();
while (num != 0) {
int a = num % 16;
if (a < 10) {
stack.push(a + "");
} else {
//10-A 11-B 12-C 13-D 14-E 15-F
// 65 66 67 68 69 70
stack.push((char)(a + 55) + "");
}
num /= 16;
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
System.out.println(sb.toString());
}
}
- 十六进制转为十进制:
代码展示:
package p2.XianXingJieGou;
//十六进制 转 十进制
public class HexToDec {
public static void main(String[] args) {
String hex = "9FBF1";
ArrayStack<Character> stack = new ArrayStack<>();
for (int i = 0; i < hex.length(); i++) {
stack.push(hex.charAt(i));
}
int sum = 0;
int mi = 0;
while (!stack.isEmpty()) {
//9 F B F 1
char c = stack.pop(); // '1'
sum += getNumber(c) * Math.pow(16,mi);
mi++;
}
System.out.println(sum);
}
private static int getNumber(char c) {
if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F')) {
throw new IllegalArgumentException("wrong char!");
}
if (c >= '0' && c <= '9') {
return c - '0';
} else {
return c - 'A' + 10;
}
}
}