自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 leetcode739. 每日温度

// 暴力法class Solution { public int[] dailyTemperatures(int[] T) { int len = T.length; int[] result = new int[len]; for (int i = 0; i < len; i++) { for (int j = i + 1; j < len; j++) { if (T[i] &

2020-07-15 17:20:54 155

原创 leetcode647. 回文子串

/** * 使用动态规划, dp[i][j] 代表str[i] - str[j]是否是回文子串 * 考虑单字符和双字符的特殊情况 * 状态转移方程:dp[i][j] = dp[i+1][j-1] && str[i]==str[j] */class Solution { public int countSubstrings(String s) { int result = 0; boolean dp[][] = new boolean[s.le

2020-07-15 16:59:02 157

原创 leetcode621. 任务调度器

import java.util.Arrays;/** * 解题思路: * 1、将任务按类型分组,正好A-Z用一个int[26]保存任务类型个数 * 2、对数组进行排序,优先排列个数(count)最大的任务, * 如题得到的时间至少为 * retCount =(count-1)* (n+1) + 1 ==> A->X->X->A->X->X->A(X为其他任务或者待命) * 3、再排序下一个任务,如果下一个任务B个数和最大任务数

2020-07-15 15:20:44 154

原创 leetcode494. 目标和

// 01背包/** * 我们可以将原问题转化为: 找到nums一个正子集和一个负子集,使得总和等于target,统计这种可能性的总数。 * 我们假设P是正子集,N是负子集。让我们看看如何将其转换为子集求和问题: * sum(P) - sum(N) = target * (两边同时加上sum(P)+sum(N)) * sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + s

2020-07-15 11:37:15 224

原创 leetcode438. 找到字符串中所有字母异位词

import java.util.ArrayList;import java.util.HashMap;import java.util.List;class Solution { public List<Integer> findAnagrams(String s, String p) { // 用于返回字母异位词的起始索引 List<Integer> result = new ArrayList<>()

2020-07-14 15:01:47 159

原创 leetcode309. 最佳买卖股票时机含冷冻期

class Solution { public int maxProfit(int[] prices) { int n = prices.length; if (n < 1) return 0; int[][] dp = new int[n][3]; // 本来就不持有,啥也没干 dp[0][0] = 0; // 第0天只买入 dp[0][1] = -1 * prices[0];

2020-07-14 13:35:22 101

原创 leetcode416. 分割等和子集

public class Solution { public boolean canPartition(int[] nums) { int len = nums.length; if (len == 0) { return false; } int sum = 0; for (int num : nums) { sum += num; }

2020-07-14 11:18:54 203

原创 leetcode406. 根据身高重建队列

import java.lang.reflect.Array;import java.util.Arrays;import java.util.LinkedList;/** * 解题思路:先排序再插入 * 1.排序规则:按照先H高度降序,K个数升序排序 * 2.遍历排序后的数组,根据K插入到K的位置上 * * 核心思想:高个子先站好位,矮个子插入到K位置上,前面肯定有K个高个子, * 矮个子再插到前面也满足K的要求 * */class Solution { public

2020-07-14 10:05:07 134

原创 leetcode399. 除法求值

import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;// DFSclass Solution { Map<String, HashMap<String, Double>> g = new HashMap<>(); public double[] calcEquati

2020-07-14 09:46:36 77

原创 leetcode347. 前 K 个高频元素

import java.util.Comparator;import java.util.HashMap;import java.util.PriorityQueue;// 最小堆/** * 具体操作为: * 借助 哈希表 来建立数字和其出现次数的映射,遍历一遍数组统计元素的频率 * 维护一个元素数目为 k 的最小堆 * 每次都将新的元素与堆顶元素(堆中频率最小的元素)进行比较 * 如果新的元素的频率比堆顶端的元素大,则弹出堆顶端的元素,将新的元素添加进堆中 * 最终,堆中的 k 个元

2020-07-13 15:12:47 79

原创 leetcode337. 打家劫舍 III

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */// 动态规划// 4 个孙子偷的钱 + 爷爷的钱 VS 两个儿子偷的钱 哪个组合钱多,// 就当做当前节点能偷的最大钱数。这就是动态规划里面的最优子结构

2020-07-13 10:52:48 68

原创 leetcode322. 零钱兑换

// 动态规划-自上而下class Solution { public int coinChange(int[] coins, int amount) { if (amount < 1) return 0; return helper(coins, amount, new int[amount]); } private int helper(int[] coins, int rem, int[] count) { if (r

2020-07-10 11:51:59 75

原创 leetcode300. 最长上升子序列

import java.util.Arrays;class Solution { public int lengthOfLIS(int[] nums) { int len = nums.length; if (len < 2) return len; int[] dp = new int[len]; // 初始化 Arrays.fill(dp, 1); // 动态规划更新dp数组

2020-07-10 11:27:59 130

原创 leetcode287. 寻找重复数

// 用二分查找法/** * 以[2,4,5,2,3,1,6,7]为例,一共8个数,n+1=8,n=7,根据题目意思,每个数都在1和7之间。 * 例如:区间[1,7]的中位数是4,遍历整个数组,统计小于等于4的整数的个数, * 如果不存在重复元素,最多为4个。等于4的时候区间[1,4]内也可能有重复元素。 * 但是,如果整个数组里小于等于4的整数的个数严格大于4的时候,就可以说明重复的数存在于区间[1,4]。 */class Solution { public int findDupl

2020-07-10 10:11:38 109

原创 leetcode279. 完全平方数

class Solution { public int numSquares(int n) { // 默认初始化值都为0 int[] dp = new int[n + 1]; for (int i = 1; i <= n; i++) { // 最坏的情况就是每次+1 dp[i] = i; for (int j = 1; i - j * j >= 0; j++) {

2020-07-10 09:51:10 66

原创 leetcode238. 除自身以外数组的乘积

// 遍历两次,一次乘左边的数,一次乘右边的数class Solution { public int[] productExceptSelf(int[] nums) { int[] result = new int[nums.length]; int k = 1; for (int i = 0; i < result.length; i++) { result[i] = k; // 此时数组存储的

2020-07-08 09:49:27 182

原创 leetcode208.实现 Trie (前缀树)

class Trie { /** Initialize your data structure here. */ class TrieNode { private boolean isEnd; TrieNode[] next; public TrieNode() { isEnd = false; next = new TrieNode[26]; } }

2020-07-07 10:27:21 74

原创 leetcode207. 课程表

import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Queue;// 入度表(广度优先遍历)class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { // 入度表indegrees int[] indegrees =

2020-07-07 09:52:58 72

原创 leetcode141.环形链表

/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public boolean hasCycle(ListNode head) {

2020-06-09 17:15:07 77

原创 leetcode139.单词拆分

import java.util.HashSet;import java.util.List;import java.util.Set;class Solution { public boolean wordBreak(String s, List<String> wordDict) { int len = s.length(); // 状态定义:以 s[i] 结尾的子字符串是否符合题意 boolean[] dp = new boo

2020-06-09 08:45:02 91

原创 leetcode136.只出现一次的数字

//异或运算满足交换律,a^b^a=a^a^b=b,因此ans相当于nums[0]^nums[1]^nums[2]^nums[3]^nums[4]..... 然后再根据交换律把相等的合并到一块儿进行异或(结果为0),然后再与只出现过一次的元素进行异或,这样最后的结果就是,只出现过一次的元素(0^任意值=任意值)class Solution { public int singleNumber(int[] nums) { int result = nums[0]; fo

2020-06-08 18:38:20 70

原创 leetcode560.和为K的子数组

// 暴力法class Solution { public int subarraySum(int[] nums, int k) { if (nums == null) return 0; int length = nums.length; int count = 0; for (int left = 0; left < length; left++) { int sum = 0;

2020-06-08 18:24:46 77

原创 leetcode128. 最长连续序列

import java.util.HashSet;import java.util.Set;//用哈希表//遍历哈希表中的元素,然后查看是否连续,然后更新最大长度class Solution { public int longestConsecutive(int[] nums) { Set<Integer> map = new HashSet<...

2020-05-06 16:49:24 121

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

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

2020-05-06 16:16:41 98

原创 leetcode85.最大矩形

import java.util.Stack;//通过一行行添加转换成为84题,用栈解决class Solution { public int maximalRectangle(char[][] matrix) { if (matrix.length == 0) return 0; //辅助矩阵存放添加的行的信息 int[] heigh...

2020-05-01 16:20:50 117

原创 leetcode84. 柱状图中最大的矩形

import java.util.ArrayDeque;import java.util.Deque;import java.util.Stack;//通过一个单调递增的栈找到最左和最右//核心思想是遍历元素,栈里面保证有一个最小元素作为最低值//用指针i遍历,不停计算i左边元素向左能延申到的最大值class Solution { public int largestRect...

2020-04-18 18:12:25 78

原创 leetcode32. 最长有效括号

import java.util.Stack;//用栈,妙就妙在做差是与垫底的那个元素做差,而不是对应的括号的下标//对于遇到的每个( ,我们将它的下标放入栈中。//对于遇到的每个) ,我们弹出栈顶的元素并将当前元素的下标与弹出元素下标作差,//得出当前有效括号字符串的长度。通过这种方法,我们继续计算有效子字符串的长度,//并最终返回最长有效子字符串的长度。public class...

2020-04-17 21:01:55 67

原创 leetcode72. 编辑距离

//通过一个辅助矩阵来理解,其实我理解为通过两个字符串挨个字符去进行比较匹配//如果碰到相同的字符,那么操作数就相当于在前面增加或者删除的字符数//如果替换,则(有点难理解)//如果是增加或者删除(不太好形容)class Solution { public int minDistance(String word1, String word2) { int n1 = ...

2020-04-17 17:09:40 62

原创 leetcode42. 接雨水

//按列计算,按行计算超出时间限制了//每一列能装的水,取决于左右两边的最高列的高度,如果有一边最高列比当前列矮,则不能装水,如果两边都有更高的,则装水的量等于比较矮的那一列的高度减去当前列的高度class Solution { public int trap(int[] height) { int sum = 0; //保存着左边和右边最高列的坐标 ...

2020-04-13 15:26:33 57

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

class Solution { public int[] searchRange(int[] nums, int target) { if (nums == null || nums.length == 0) return new int[]{-1, -1}; //先找开始位置 int firstPosition = findFirstP...

2020-04-13 12:00:03 55

原创 leetcode31. 下一个排列

class Solution { public void nextPermutation(int[] nums) { if (nums == null || nums.length == 0) return; //做一个标记,如果flag变为1,则这个数组变动过,不需要整个翻转 int flag = 0; //从后往前遍历 ...

2020-04-06 23:04:45 82

原创 leetcode23. 合并K个排序链表

class Solution { public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode mergeKLists(ListNode[] lists) { if...

2020-04-05 17:14:26 56

原创 leetcode20. 有效的括号

import java.util.HashMap;import java.util.Map;import java.util.Stack;class Solution { public boolean isValid(String s) { if (s == null) return false; //创建一个map进行匹配 Map<...

2020-04-03 23:07:42 55

原创 leetcode5. 最长回文子串

//中心扩散法class Solution { public String longestPalindrome(String s) { if (s.length() < 2) return s; //需要初始化 String result = s.substring(0, 1); //这里要-1,因为最后一个字符不需要...

2020-04-01 15:41:31 53

原创 十大排序算法java实现

因为面试美团的时候,面试官让我写个快排我20分钟都没写出来,后面经高人指点,把十大经典排序算法抄十遍,因此有了这篇博客的出现。1. 冒泡排序// 冒泡排序算法平均n2,最好n,最坏n2,空间复杂度1,稳定class Solution { public int[] buubleSort(int[] nums) { if (nums == null || nums.len...

2020-03-29 16:48:58 68

原创 leetcode89. 格雷编码

class Solution { public List<Integer> grayCode(int n) { List<Integer> result = new ArrayList<>(); result.add(0); int head = 1; for (int i = 0; i &...

2020-03-24 16:26:40 77

原创 leetcode567. 字符串的排列

class Solution { public boolean checkInclusion(String s1, String s2) { if (s1.length() > s2.length()) return false; int[] s1map = new int[26]; int[] s2map = new int[26];...

2020-03-22 16:35:28 63

原创 leetcode14. 最长公共前缀

class Solution { public String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; String prefix = strs[0]; for (int i = 0; i < strs.length - 1; i++) {...

2020-03-22 16:01:14 84

原创 leetcode3. 无重复字符的最长子串

class Solution { //滑动窗口法 public int lengthOfLongestSubstring(String s) { if (s.length() == 0) return 0; HashMap<Character, Integer> map = new HashMap<Character, Intege...

2020-03-22 15:28:15 65

原创 leetcode88. 合并两个有序数组

class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int len1 = m - 1; int len2 = n - 1; int len = m + n - 1; while (len1 >= 0 &&...

2020-03-21 22:14:14 63

空空如也

空空如也

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

TA关注的人

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