自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(56)
  • 收藏
  • 关注

原创 200. 岛屿数量(正:1)

题目详情:https://leetcode-cn.com/problems/number-of-islands/class Solution { public int numIslands(char[][] grid) { int res = 0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) {

2020-08-18 22:59:44 83

原创 860.柠檬水找零(正:1)

题目详情见:https://leetcode-cn.com/problems/lemonade-change/class Solution { public boolean lemonadeChange(int[] bills) { int five = 0, ten = 0; for (int money : bills) { //收钱 if (money == 10) ten++; else

2020-08-18 19:39:10 89

原创 455. 分发饼干(正:1)

题目详情:https://leetcode-cn.com/problems/assign-cookies///方法一:排序后遍历//从小的开始分配class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int gk = 0, sk = 0; while (gk < g.length

2020-08-16 23:12:36 80

原创 515. 在每个树行中找最大值(正:1)

题目详情:https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row///方法一:BFSclass Solution { public List<Integer> largestValues(TreeNode root) { List<Integer> res = new LinkedList<Integer>(); if (root == null)

2020-08-15 21:59:42 782

原创 102. 二叉树的层序遍历(正:1)

题目详情见:https://leetcode-cn.com/problems/binary-tree-level-order-traversal///方法一:BFSimport java.util.*; class Solution { public List<List<Integer>> levelOrder(TreeNode root) { if(root==null) return new ArrayList<List<Integer>>()

2020-08-14 23:09:29 78

原创 47. 全排列 II(正:1)

题目详情见:https://leetcode-cn.com/problems/permutations-ii/class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(nums); if (nu

2020-08-13 17:10:24 120

原创 46. 全排列(正:1)

class Solution { public void backtrack(int n, ArrayList<Integer> output, List<List<Integer>> res, int first) { //总共几个元素,一行的结果, 最终结果集, 本次处理第几个位置 // 所有数都填完了 if (first == n) res.ad

2020-08-12 23:40:32 70

原创 17. 电话号码的字母组合(正:1)

题目详情见:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number///方法一:回溯法public class Solution { public List<String> letterCombinations(String digits) { List<String> res = new ArrayList<>(); if (digits =

2020-08-11 23:03:04 73

原创 50. Pow(x, n) (正:1)

题目详情见:https://leetcode-cn.com/problems/powx-n///方法一:分治法class Solution { public double myPow(double x, int n) { if (x == 0) return 0; if (n == 0) return 1; if (n >= 0) { if (n == 1) return x; double

2020-08-09 23:49:53 60

原创 77. 组合(正:1)

题目详情见:https://leetcode-cn.com/problems/combinations/import java.util.ArrayList;import java.util.List;import java.util.Stack;public class Solution { private List<List<Integer>> res = new ArrayList<>(); private void findCombi

2020-08-07 22:45:36 77

原创 105. 从前序与中序遍历序列构造二叉树(正:1)

题目详情见 :https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/public TreeNode buildTree(int[] preorder, int[] inorder) { return buildTreeHelper(preorder, 0, preorder.length, inorder, 0, inorder.length);}private T

2020-08-07 21:57:01 83

原创 297. 二叉树的序列化与反序列化(正:1)

题目详情见:https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree//*方法一:*/public class Codec { private static final String spliter = ","; private static final String NN = "X"; // Encodes a tree to a single string. public Str

2020-08-05 22:00:50 111

原创 104. 二叉树的最大深度(正:1)

题目详情见:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree//*方法一:递归法*/class Solution { public int maxDepth(TreeNode root) { if (root == null) { return 0; } else { int leftHeight = maxDepth(root.left)

2020-08-03 22:35:03 69

原创 98. 验证二叉搜索树(正:1)

题目详情见:https://leetcode-cn.com/problems/validate-binary-search-tree//*方法一:递归法确保不只是左右节点满足二叉搜索树性质,左右子树也要满足。*/class Solution { public boolean isValidBST(TreeNode root) { return helper(root, null, null); } private boolean helper(TreeNo

2020-08-03 21:41:27 71

原创 递归,代码模板

学习笔记:public void recur(int level, int param) {//函数定义 (参数,...){ if (level > MAX_LEVEL) { //递归终结条件; return; } process (level, param); //处理当前层逻辑; recur(level: level + 1, new Param);//下探到下一层; //清理当前层;

2020-08-03 20:52:54 143

原创 226. 翻转二叉树(正:1)

题目详情见:https://leetcode-cn.com/problems/invert-binary-tree//*方法一:递归*/class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) return root; TreeNode point = root.left; root.left = root.right;

2020-08-02 23:49:21 65

原创 22. 括号生成(正:1)

题目详情见:https://leetcode-cn.com/problems/generate-parentheses//*方法一:递归*/class Solution { public List<String> generateParenthesis(int n) { List<String> res = new ArrayList<>(); recur(0, 0, n, res, new String());

2020-08-02 23:34:01 81

原创 70. 爬楼梯(正:1)

题目详情见:https://leetcode-cn.com/problems/climbing-stairs//*方法一:傻递归(会超时)1. 12. 1,1; 23. f(1) + f(2) 只可能从第一阶或第二阶迈上第三阶台阶...n. f(n - 1) + f(n - 2)*/class Solution { public int climbStairs(int n) { if (n == 1) return 1; else if (n == 2)

2020-08-02 23:20:48 78

原创 589. N叉树的前序遍历(正:1)

题目详情见:https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal//*// Definition for a Node.class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } publ

2020-08-01 23:10:29 66

原创 590. N叉树的后序遍历(正:1)

题目详情见:https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal//*// Definition for a Node.class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } pub

2020-08-01 22:28:39 57

原创 144. 二叉树的前序遍历(正:1)

题目详情见:https://leetcode-cn.com/problems/binary-tree-preorder-traversal//*方法一:递归法*/class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); PTB(list, root);

2020-07-31 23:28:02 79

原创 94. 二叉树的中序遍历(正:1)

题目详情见:https://leetcode-cn.com/problems/binary-tree-inorder-traversal//*方法一:递归*/class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); ITB(list, root);

2020-07-31 23:11:01 65

原创 42.接雨水(正:1)

题目详情见:https://leetcode-cn.com/problems/trapping-rain-water//*解法一:按行求一层一层的遍历,先遍历第一层,再第二层,,,*/public int trap(int[] height) { int sum = 0; //记录接的雨水总量 int max = getMax(height); //找到最大的高度,以便遍历 for (int i = 1; i <= max; i++) { boolea

2020-07-30 22:39:42 83

原创 242. 有效的字母异位词(正:1)

题目详情见:https://leetcode-cn.com/problems/valid-anagram//*方法一:暴力法先排序,再比较*/public class Solution { public boolean isAnagram(String s, String t) { char[] s_sort = s.toCharArray(); char[] t_sort = t.toCharArray(); if (s_sort.leng

2020-07-29 23:00:30 101

原创 641.设计循环双端队列(正:1)

题目详情见:https://leetcode-cn.com/problems/design-circular-deque//*方法一:使用数组存储,数组中有一个空位置,用来区分空队列和满队列。*/public class MyCircularDeque { private int capacity; //存放元素数量 private int[] arr; //存放数据的数组 private int front; //头指针 private int rear; //

2020-07-28 22:16:08 64

原创 239. 滑动窗口最大值 (正:1)

题目详情见:https://leetcode-cn.com/problems/sliding-window-maximum/方法一:暴力法:遍历(n - k) * k次public class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums.length - k < 0) return new int[0]; int left = 0, right = k

2020-07-27 22:56:27 122

原创 84. 柱状图中最大的矩形 (正:1)

题目详情见:https://leetcode-cn.com/problems/largest-rectangle-in-histogram/方法一:暴力法:先确定两个边界,再从两个边界中找到最小值;时间复杂度:O(n3),空间复杂度O(n2)public int largestRectangleArea(int[] heights) { int res = 0; for (int i = 0; i < heights.length; i++) {

2020-07-26 23:35:38 39

原创 155.最小栈 (正:1)

题目详情见:https://leetcode-cn.com/problems/min-stack/方法一:同步辅助栈public class MinStack { /** initialize your data structure here. */ Deque<Integer> stack; Deque<Integer> min_stack; public MinStack() { stack = new LinkedList

2020-07-25 23:10:18 69

原创 20. 有效的括号(正:1)

题目详情见:https://leetcode-cn.com/problems/valid-parentheses/方法一:public class Solution { public boolean isValid(String s) { if (s.length() < 1) return true; //判空字符串 char[] str = s.toCharArray(); //转为数组操做 int index = 0;//初始位置

2020-07-25 22:49:17 92

原创 66.加一 (正:1)

题目详情见:https://leetcode-cn.com/problems/plus-one/方法一:class Solution { public int[] plusOne(int[] digits) { for (int i = digits.length - 1; i >= 0; i--) { //处理数组除了digits[0]位 digits[i]++; if (i>0 && digits[i]

2020-07-24 22:47:32 55

原创 88. 合并两个有序数组(正:1)

题目详情见:https://leetcode-cn.com/problems/merge-sorted-array/方法一:把nums2先按序加入nums1后面,再排序。class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { //该代码可替换为: //System.arraycopy(nums2, 0, nums1, m, n); for (int i = 0; i

2020-07-23 23:28:08 66

原创 21. 合并两个有序链表(正:1)

题目详情见:https://leetcode-cn.com/problems/merge-two-sorted-lists/方法一:同时比较遍历两个链表,将较小元素追加到结果链表,最后将剩余的链表追加到结果链表末尾。class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode first = new ListNode(0); //结果链表的辅助头节点 Lis

2020-07-23 22:41:02 46

原创 189.旋转数组 (正:1)

题目详情见:https://leetcode-cn.com/problems/rotate-array/方法一:两部交换法:把整个数组看作一个首尾相接的环,因此右移k位,和右移k=k%nums.length位效果相同。把数组分成a( [0,nums.length-k) ),b([nums.length-k,nums.length-1))两部分。temp = b;a放[k,nums.length-1];b放[0,k)class Solution { public void rotate(in

2020-07-23 21:51:16 54

原创 26. 删除排序数组中的重复项 (正:1)

题目详情见:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/方法一:class Solution { public int removeDuplicates(int[] nums) { if (nums == null || nums.length == 0) return 0; //判空数组 int newlength = 1; //不为空初始长度为1 in

2020-07-22 22:30:15 59

原创 141. 环形链表 (正:1)

题目详情见:https://leetcode-cn.com/problems/linked-list-cycle/方法一:快慢指针法:one指针一次走一步,two指针一次走两步,如果有环,则two反会追上one;public class Solution { public boolean hasCycle(ListNode head) { boolean flag = false; //记录标志位 if (head == null) return flag; /

2020-07-22 22:02:05 76

原创 206.反转链表 (正:1)

题目详情见:https://leetcode-cn.com/problems/reverse-linked-list/方法一:三个指针法:first指针记录已经反转的链表,second指针记录本次待反转的节点,third指针用来记录下一个待反转的节点。初始时,first和second都指向head,当确定head和head.next不为空后,second指向head.next,当second.next不为空时,进循环,third此时可看作中间过渡变量,记录second.next,然后此时second.n

2020-07-22 00:24:41 55

原创 104.二叉树的最大深度。2星

方法一:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { ...

2020-04-11 11:43:32 54

原创 2.两数相加。2星

解题思路:两个数使用链表表示,所以如果想转为数值类型,处理的值的大小可能会超出int甚至long,所以应当仍按照链表处理,掌握好进位即可。方法一:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(i...

2020-04-08 00:07:28 67

原创 94.二叉树的中序遍历。2星

中序遍历:首先遍历左子树,然后访问根节点,最后遍历右子树(左->根->右)顺序:中序遍历左子树->访问根节点->中序遍历右子树方法一:递归遍历:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * ...

2020-04-05 11:47:42 68

原创 1143.最长公共子序列。3星

方法一: public int longestCommonSubsequence(String text1, String text2) { int n1 = text1.length(), n2 = text2.length(); int[][] dp = new int[n1 + 1][n2 + 1]; for (int i = 1; i ...

2020-04-04 16:42:15 76

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除