随笔分类 - 剑指Offer
一共就这么点题,刷透刷透刷透它!!!!
摘要:// Encodes a tree to a single string. public String serialize(TreeNode root) { if(root == null) return "[]"; Deque<TreeNode> deque = new LinkedList<>(
阅读全文
摘要:class Solution { HashMap<Node,Node> map = new HashMap<>(); public Node copyRandomList(Node head) { if(head == null) return null; Node p = head; while(
阅读全文
摘要:List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> pathSum(TreeNode root, int sum) { List<Integer> list = new ArrayList<>(); trac
阅读全文
摘要:class Solution { public boolean verifyPostorder(int[] postorder) { int n = postorder.length; if(n<=1) return true; int j = n-1; for(;j>=0;j--){ if(pos
阅读全文
摘要:List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> levelOrder(TreeNode root) { if(root == null) return res; Deque<TreeNode> queue
阅读全文
摘要:class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if(root == null) return res; Queu
阅读全文
摘要:public int[] levelOrder(TreeNode root) { Deque<TreeNode> deque = new ArrayDeque<>(); ArrayList<Integer> res = new ArrayList<>(); if(root != null){ deq
阅读全文
摘要:public boolean validateStackSequences(int[] pushed, int[] popped) { int m = pushed.length; int n = popped.length; if(m!=n) return false; if(m == 1) re
阅读全文
摘要:public class MinStack { /** initialize your data structure here. */ Stack<Integer> stack = new Stack<>(); private int min = Integer.MIN_VALUE; List<In
阅读全文
摘要:class Solution { public int[] spiralOrder(int[][] matrix) { int m = matrix.length; if(m == 0) return new int[]{}; int n = matrix[0].length; if(m == 1)
阅读全文
摘要:递归 public boolean isSymmetric(TreeNode root) { return root == null || isSymmetric(root.left,root.right); } private boolean isSymmetric(TreeNode left,
阅读全文
摘要:class Solution { public TreeNode mirrorTree(TreeNode root) { if(root == null) return null; TreeNode temp = root.left; root.left = root.right; root.rig
阅读全文
摘要:class Solution { public boolean isSubStructure(TreeNode A, TreeNode B) { if(A==null || B==null) return false; if(A.val != B.val){ return isSubStructur
阅读全文
摘要:class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode dump = new ListNode(-1); ListNode p = dump; while(l1!=null && l2!=
阅读全文
摘要:class Solution { public ListNode getKthFromEnd(ListNode head, int k) { ListNode p = head; int count = 0; while(p!=null){ count++; p = p.next; } if(k>c
阅读全文
摘要:双指针 public int[] exchange(int[] nums) { int len = nums.length; int[] res = new int[len]; int j = 0, k = len-1; for (int num : nums) { if ((num & 0x1)
阅读全文
摘要:class Solution { int i = 0; public boolean isNumber(String s) { s = s.trim(); int n = s.length(); if(n == 0) return false; boolean b = scanInteger(s);
阅读全文
摘要:class Solution { public boolean isMatch(String s, String p) { int m = s.length(); int n = p.length(); int i = 0,j = 0; char[] s1 = s.toCharArray(); ch
阅读全文
摘要:没什么难度。 class Solution { public ListNode deleteNode(ListNode head, int val) { ListNode dump = new ListNode(-1); dump.next = head; ListNode pre = dump;
阅读全文
摘要:class Solution { public int[] printNumbers(int n) { int m = (int)Math.pow(10,n); int[] nums = new int[m-1]; for(int i = 1;i<m;i++){ nums[i-1] = i; } r
阅读全文

浙公网安备 33010602011771号