1, 给定一个字符串, 已知字符串只包含'(', ')', '[', ']'四种括号字符, 要求: 写一个程序, 输入随意的字符串, 判断括号是否匹配
例:
输入: "()"
返回: "true"
输入: "(]"
返回: "false"
输入: "()[()]"
返回: "true"
输入: "()[()()]([][])[()[[[[]]]]]"
返回: "true"
1 package test1; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class TestStack { 7 8 private List<Character> elements = new ArrayList<>(); 9 10 private void in(Character o) { 11 this.elements.add(o); 12 } 13 14 private Character out() { 15 int length = this.elements.size(); 16 if (length > 0) { 17 return elements.remove(length - 1); 18 } 19 return null; 20 } 21 22 public int size() { 23 return this.elements.size(); 24 } 25 26 public boolean valid(String str) { 27 this.elements.clear(); 28 if (str != null && str.length() > 0) { 29 char[] chars = str.toCharArray(); 30 for (char c : chars) { 31 char left1 = '('; 32 if (c == left1) { 33 this.in(c); 34 } 35 char right1 = '['; 36 if (c == right1) { 37 this.in(c); 38 } 39 char left2 = ')'; 40 if (c == left2) { 41 char outElement = this.out(); 42 boolean r = outElement == left1; 43 if (!r) { 44 return false; 45 } 46 } 47 char right2 = ']'; 48 if (c == right2) { 49 char outElement = this.out(); 50 boolean r = outElement == right1; 51 if (!r) { 52 return false; 53 } 54 } 55 } 56 } 57 return this.elements.size() == 0; 58 } 59 }
1 package test1; 2 3 public class TestDemo { 4 public static void main(String[] args) { 5 String str1 = "()"; 6 String str2 = "[]"; 7 String str3 = "(]"; 8 String str4 = "((()"; 9 String str5 = "()[()]"; 10 String str6 = "([)]"; 11 String str7 = "()[()()]([][])[()[[[[]]]]]"; 12 13 TestStack testStack = new TestStack(); 14 System.out.println(str1 + ": " + testStack.valid(str1)); 15 System.out.println(str2 + ": " + testStack.valid(str2)); 16 System.out.println(str3 + ": " + testStack.valid(str3)); 17 System.out.println(str4 + ": " + testStack.valid(str4)); 18 System.out.println(str5 + ": " + testStack.valid(str5)); 19 System.out.println(str6 + ": " + testStack.valid(str6)); 20 System.out.println(str7 + ": " + testStack.valid(str7)); 21 } 22 }
判断括号匹配的另一种解法
public class Main { private static String f1 = "(}"; private static String f2 = "{)"; public static void main(String[] args) { String src = "({((){()()}((){}){()()})})"; System.out.println(valid(src)); } public static boolean valid(String src) { if ("".equals(src)) { return false; } return _valid(src); } public static boolean _valid(String src) { if (src != null && src.trim().length() > 0) { if (src.trim().length() % 2 != 0) { return false; } if (!src.contains(f1) && !src.contains(f2)) { src = src.replace("()", "").replace("{}", ""); return valid(src); } return false; } return true; } }
2, 给定两个数组, 两个数组长度大于0(两个数组的长度可能相同也可能不同), 并且各自已经按照由小到大排序, 要求: 合并两个数组形成一个新的数组, 并且不需要重新排序, 新的数组也按照由小到大排列
1 package test2; 2 3 import java.util.Arrays; 4 5 public class TestDemo { 6 public static void main(String[] args) { 7 int[] arr1 = {1, 3, 5, 9, 15}; 8 int[] arr2 = {2, 5, 7, 8, 12, 13, 19}; 9 // int[] arr1 = {1, 2, 3, 4}; 10 // int[] arr2 = {1, 2, 3}; 11 // int[] arr1 = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; 12 // int[] arr2 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 13 14 int[] newArr = new int[arr1.length + arr2.length]; 15 int point1 = 0; 16 int point2 = 0; 17 18 for (int i = 0; i < newArr.length; i++) { 19 if (point1 >= arr1.length && point2 < arr2.length) { 20 newArr[i] = arr2[point2++]; 21 continue; 22 } 23 if (point1 < arr1.length && point2 >= arr2.length) { 24 newArr[i] = arr1[point1++]; 25 continue; 26 } 27 int result = Integer.compare(arr1[point1], arr2[point2]); 28 switch (result) { 29 case -1: 30 newArr[i] = arr1[point1]; 31 point1++; 32 break; 33 case 0: 34 newArr[i++] = arr1[point1]; 35 newArr[i] = arr2[point2]; 36 point1++; 37 point2++; 38 break; 39 case 1: 40 newArr[i] = arr2[point2]; 41 point2++; 42 break; 43 } 44 } 45 System.out.println(Arrays.toString(newArr)); 46 } 47 }