机试题总结
1.实现括号的匹配
package test; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Stack; /** * 括号匹配==()(){[]{}} * * @author Administrator * */ public class BracketMatch { public static void main(String[] args) { // 利用栈进行匹配,左括号就进栈,遇到右括号出栈,匹配出栈的和右括号是否匹配 String testStr = "{{3534{{}}ertr4534[543](364())"; char[] charArray = testStr.toCharArray(); boolean result = true; // 括号规则 Map<Character, Character> rules = new HashMap<>(); rules.put('(', ')'); rules.put('[', ']'); rules.put('{', '}'); Set<Character> keySet = rules.keySet(); Collection<Character> values = rules.values(); Stack<Character> stack = new Stack<>(); for (Character c : charArray) { if (c == null) { continue; } // key包含的进行入栈 if (keySet.contains(c)) { stack.push(c); continue; } // 值包含的进行出栈 if (values.contains(c)) { if (stack.isEmpty()) { result = false; break; } Character popedC = stack.pop(); // 判断是否相等 boolean result1 = validateNotSure(c, popedC, ')', '('); boolean result2 = validateNotSure(c, popedC, '}', '{'); boolean result3 = validateNotSure(c, popedC, ']', '['); if (!result1 && !result2 && !result3) { result = false; break; } } } // 判断栈是否有数据 result = result && stack.isEmpty(); System.out.println(result); } private static boolean validateNotSure(Character c, Character popedC, Character cValue, Character popedCValue) { return c.equals(cValue) && popedC.equals(popedCValue); } }
2.合并两个有序链表
package test; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; import lombok.AllArgsConstructor; import lombok.Data; import lombok.Getter; import lombok.Setter; @Getter @Setter public class NodeList<T> { private Node<T> first; private Node<T> last; private int size; public boolean add(T value) { if (last == null) { addFirst(value); return true; } addNormalNode(value); return true; } private void addNormalNode(T value) { Node<T> node = new Node<>(last, value, null); last.setNext(node); this.last = node; size++; } private void addFirst(T value) { Node<T> first = new Node<>(null, value, null); this.first = first; this.last = first; size++; } @Override public String toString() { if (this.first == null) { return "[]"; } Node<T> current = first; List<T> result = new ArrayList<>(); while (current != null) { result.add(current.getValue()); current = current.getNext(); } return StringUtils.join(result); } public void combineNode(NodeList<T> nodes) { if (nodes == null || nodes.size == 0) { return; } Node<T> current = nodes.getFirst(); while (current != null) { add(current.getValue()); current = current.getNext(); } } @Data @AllArgsConstructor class Node<T> { private Node<T> prev; private T value; private Node<T> next; } public static void main(String[] args) { NodeList<String> list = new NodeList<>(); list.add("1"); list.add("2"); list.add("3"); NodeList<String> list2 = new NodeList<>(); list2.add("2"); list2.add("3"); list2.add("4"); list.combineNode(list2); System.out.println(list); } }
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】