自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 只出现一次的数字

只出现一次的数字class Solution { public int singleNumber(int[] nums) { int res = 0; for (int num : nums) { res ^= num; } return res; }}

2021-03-23 10:00:03 68

原创 最长连续序列

最长连续序列class Solution { public int longestConsecutive(int[] nums) { int res = 0; HashSet<Integer> hashSet = new HashSet<>(); for (int num : nums) { hashSet.add(num); } for (int num : nums)

2021-03-22 11:03:46 96

原创 单词搜索

单词搜索class Solution { public boolean exist(char[][] board, String word) { for (int i = 0; i < board.length; i++) { for (int i1 = 0; i1 < board[0].length; i1++) { if (dfs(i, i1, word, board, 0)) return .

2021-03-20 14:52:15 73

原创 二叉树中的最大路径和

二叉树中的最大路径和/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode

2021-03-20 14:48:06 61

原创 合并二叉树

合并二叉树/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {

2021-03-19 11:03:17 57

原创 二叉树的序列化与反序列化

二叉树的序列化与反序列化import java.util.*;public class Codec { String sequence = ""; public String serialize(TreeNode root) { if (root == null) { sequence += "null,"; return sequence; } sequence += root.val +

2021-03-19 10:21:30 60

原创 把二叉搜索树转换为累加树

把二叉搜索树转换为累加树/*中序遍历反向操作,遍历右子树、根节点、左子树*/class Solution { TreeNode pre = null; int sum = 0; public TreeNode convertBST(TreeNode root) { rInorder(root); return root; } public void rInorder(TreeNode root) { if (ro

2021-03-18 10:55:23 68

原创 翻转二叉树

翻转二叉树/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode invertTree(TreeNode root) { if

2021-03-18 10:29:06 65

原创 二叉树的最近公共祖先

二叉树的最近公共祖先/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { TreeNode ans = null; public TreeNode lowestC.

2021-03-17 11:01:45 59

原创 单词搜索

单词搜索class Solution { public boolean exist(char[][] board, String word) { for (int i = 0; i < board.length; i++) { for (int i1 = 0; i1 < board[0].length; i1++) { if (dfs(i, i1, word, board, 0)) return .

2021-03-15 11:33:03 96

原创 子集

子集class Solution { List<List<Integer>> res = new LinkedList<>(); public List<List<Integer>> subsets(int[] nums) { dfs(nums, 0, new LinkedList<>()); return res; } public void dfs(int[] num

2021-03-14 15:54:57 88

原创 颜色分类

颜色分类/*[0, j - 1]: 为0所在区间[j, i - 1] :为1所在区间[k + 1, n - 1]:为2所在区间*/class Solution { public void sortColors(int[] nums) { for (int i = 0, j = 0, k = nums.length - 1; i <= k;) { if (nums[i] == 0) swap(nums, j ++, i ++);

2021-03-14 10:49:04 551

原创 编辑距离

编辑距离class Solution { public int minDistance(String word1, String word2) { int len1 = word1.length(), len2 = word2.length(); word1 = " " + word1; word2 = " " + word2; int[][] f = new int[len1 + 1][len2 + 1]; for.

2021-03-14 10:20:18 60

原创 爬楼梯

爬楼梯class Solution { public int climbStairs(int n) { if (n == 1) return 1; int[] f = new int[n + 1]; f[1] = 1; f[2] = 2; for (int i = 3; i <= n; i ++) { f[i] = f[i - 1] + f[i - 2]; }

2021-03-11 11:35:24 63

原创 最小路径和

最小路径和class Solution { public int minPathSum(int[][] grid) { int[][] f = new int[grid.length][grid[0].length]; for (int i = 0; i < grid.length; i ++) { for (int j = 0; j < grid[0].length; j ++) { if (i

2021-03-11 11:30:31 61

原创 不同路径

不同路径/*动态规划版本*/class Solution { public int uniquePaths(int m, int n) { int[][] f = new int[m][n]; for (int i = 0; i < m; i ++) { for (int j = 0; j < n; j ++) { if (i == 0) f[i][j] = 1;

2021-03-09 16:53:53 81

原创 合并区间

合并区间class Solution { public int[][] merge(int[][] intervals) { if (intervals.length == 1) return intervals; Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]); LinkedList<LinkedList<Integer>> out = new LinkedList&

2021-03-09 16:06:22 64

原创 跳跃游戏

跳跃游戏/*j : 能够跳到最远位置i : 当前是否能够跳到的位置*/class Solution { public boolean canJump(int[] nums) { for (int i = 0, j = 0; i < nums.length; i ++) { if (j < i) return false; j = Math.max(j, nums[i] + i); }

2021-03-08 12:05:25 47

原创 旋转图像

旋转图像class Solution { public void rotate(int[][] matrix) { for (int i = 0; i < matrix.length; i ++) { for (int i1 = 0; i1 < matrix[0].length; i1 ++) { if (i < i1) { int tmp = matrix[i][i1

2021-03-08 11:46:53 43

原创 字母异位词分组

字母异位词分组/*hashMap.computeIfAbsent(s, key -> new LinkedList<>());hashmap中是否存在键s,不存在则创建键s,并添加值。*/class Solution { public List<List<String>> groupAnagrams(String[] strs) { List<List<String>> res = new LinkedLi

2021-03-07 16:29:31 53

原创 组合总和

组合总和class Solution { List<List<Integer>> res = new LinkedList<>(); public List<List<Integer>> combinationSum(int[] candidates, int target) { dfs(0, candidates, new LinkedList<>(), target); return

2021-03-06 11:00:41 82

原创 全排列

全排列/*u : 搜索第u个位置*/class Solution { List<List<Integer>> res = new LinkedList<>(); public List<List<Integer>> permute(int[] nums) { dfs(0, nums, new LinkedList<>(), new boolean[nums.length]); r

2021-03-06 10:59:05 62

原创 在排序数组中查找元素的第一个和最后一个位置

在排序数组中查找元素的第一个和最后一个位置class Solution { public int[] searchRange(int[] nums, int target) { if (nums.length == 0) return new int[] {-1, -1}; int left = -1, right = -1; int l = 0, r = nums.length - 1; while (l < r) {

2021-03-05 16:53:57 58

原创 搜索旋转排序数组

搜索旋转排序数组class Solution { public static int search(int[] nums, int target) { int l = 0, r = nums.length - 1; while (l < r) { int mid = l + r + 1 >> 1; if (nums[mid] >= nums[0]) { l = m

2021-03-05 15:45:44 75

原创 下一个排列

下一个排列/* 4 5 3 2 1 从后向前找第一个非降序元素. 从非降序后找大于非降序元素的最小值,两个交换,将非降序元素后的元素排序 */class Solution { public void nextPermutation(int[] nums) { int k = nums.length - 1; while (k > 0 && nums[k - 1] >= nums[k]) k --;

2021-03-04 11:42:38 104 1

原创 最长有效括号

最长有效括号 /* f[i] = ')' f[i - 1] = '(' f[i] = f[i - 2] + 2; f[i] = ')' f[i - 1] = ')' if (s[i - f[i - 1] - 1] == '(') f[i] = f[i - 1] + 2 + f[i - f[i - 1] - 2]; */class Solution { public int longestValidParentheses(String s) {

2021-03-04 11:13:38 102 1

空空如也

空空如也

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

TA关注的人

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