随笔分类 - 随想录
代码随想录重新刷
摘要:226.翻转二叉树(递归只能前序或者后序,中序不行) class Solution { public TreeNode invertTree(TreeNode root) { if(root == null) return null; swap(root); invertTree(root.left
阅读全文
摘要:二叉树递归三部曲: 1. 确定递归函数的参数和返回值。 2. 确定终止条件 3.确定单层递归的逻辑 144.二叉树的前序遍历:中左右,递归: class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<I
阅读全文
摘要:150. 逆波兰表达式求值:基本stack 的操作 class Solution { public int evalRPN(String[] tokens) { Stack<Integer> stack = new Stack<>(); for(String s: tokens){ if("+".e
阅读全文
摘要:栈:用stack 进栈(push), 出栈(pop) queue:用linkedList: offer(). poll() 232.用栈实现队列 class MyQueue { //用2个stack to implement the queue. //就是用第二个栈存数据。 //为什么不用把out的
阅读全文
摘要:151.翻转字符串里的单词 class Solution { public String reverseWords(String s) { //// 删除首尾空格,分割字符串 String[] str = s.trim().split(" "); StringBuilder sb = new Str
阅读全文
摘要:344. Reverse String class Solution { //extra o1 space public void reverseString(char[] s) { int left = 0; int right = s.length - 1; while(left < right
阅读全文
摘要:454.四数相加II class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { //前两数相加,key是合,次数是value,跟后两数相加的和等于0的话,就取出map里
阅读全文
摘要:242.有效的字母异位词 class Solution { public boolean isAnagram(String s, String t) { int[] record = new int[26]; //a = 97. so a - a = 0, b - a = 1. 直接使用减法,不用记
阅读全文
摘要:24. 两两交换链表中的节点 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { th
阅读全文
摘要:203.移除链表元素 方法一:直接遍历,永远记得处理head, 删除链表必须有前驱。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode()
阅读全文
摘要:209.长度最小的子数组 class Solution { public int minSubArrayLen(int target, int[] nums) { int left = 0; int sum = 0; //int result = Integer.MAX_VALUE;无法编译,改成长
阅读全文
摘要:704. 二分查找 题目链接:https://leetcode.cn/problems/binary-search/ 1,左闭右闭 class Solution { public int search(int[] nums, int target) { //左闭右闭就是left <= right,
阅读全文
浙公网安备 33010602011771号