![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
微躺青年
这个作者很懒,什么都没留下…
展开
-
leetcode300. 最长上升子序列
解法一:动态规划class Solution { public int lengthOfLIS(int[] nums) { int len = nums.length; if (len == 0) return 0; // memo[i]表示以nums[i]为结尾的最长上升子序列的长度 int[] memo = new int[len]; for (int i = 0; i < len;原创 2020-05-11 10:43:41 · 164 阅读 · 1 评论 -
面试题39. 数组中出现次数超过一半的数字
1、解法一:哈希表时间O(n),空间O(n/2);import java.util.HashMap;import java.util.Map;class Solution { public int majorityElement(int[] nums) { Map<Integer, Integer> map = new HashMap<>(); int res = 0; int len = nums.length;原创 2020-05-11 10:41:36 · 188 阅读 · 0 评论 -
面试题40. 最小的k个数
一、使用快排解决TopK问题:O(N)找前K大/前K小问题不需要对整个数组进行O(NlogN)的排序!例如本题,直接通过快排切分排好第K小的数(下标为K-1),那么它左边的数就是比它小的另外K-1个数!注意:在编写快排partition逻辑时,java需要额外编写一个swap函数,而不是像c++直接使用,并且swap函数中要传入数组的引用,否则最后把主元交换到数组某个位置是无法成功的!这也是java值传递的特点,这里花了一些时间,最后debug找出错误。import java.util.Arrays原创 2020-05-11 10:40:43 · 189 阅读 · 0 评论 -
leetcode215.数组中的第k个大的数
依旧使用快排中的 Partion解决问题(分治)需要注意的是,本题如果不对Partion进行随机优化,那么有些样例通过时间复杂度较高,所以在Partion中加入了随机优化。class Solution{ public int findKthLargest(int[] nums, int k) { if (k == 0 || nums.length == 0) return -1; return quickSearch(nums, 0, n原创 2020-05-11 10:40:05 · 145 阅读 · 0 评论 -
leetcode53.最大子序和
很经典的一道题,在数据结构课程中也涉及到过,使用贪心是最优解,达到O(n)的时间复杂度下面给出解释:首先对数组进行遍历,当前最大连续子序列和为 sum,结果为 ans;如果 sum > 0,则说明 sum 对结果有增益效果,则 sum 保留并加上当前遍历数字;如果 sum <= 0,则说明 sum 对结果无增益效果,需要舍弃,则 sum 直接更新为当前遍历数字;每次比较 sum 和 ans 的大小,将最大值置为 ans ,遍历结束返回结果;时间复杂度:O(n)O(n)class原创 2020-05-11 10:38:42 · 101 阅读 · 0 评论 -
leetcode面试题40.最小的k个数
import java.util.Arrays;class Solution{ public int[] getLeastNumbers(int[] arr, int k) { if (k == 0 || arr.length == 0) return new int[0]; // k-1表示我们要找的是下标为k-1的数 ...原创 2020-05-05 16:17:14 · 112 阅读 · 0 评论 -
leetcode岛屿问题
leetcode200岛屿数量分析:典型的岛屿之类的问题,代码框架应是① inArea() 方法,判断点是否在区域内部② dfs() 方法,递归函数,要对上下左右4个方向进行递归调用,同时对已访问的格子进行标记③ 已给出的主成员方法,通过双重for循环,调用dfs() 方法解答:/** * 算法:递归回溯、dfs、flood fill */class Solution { ...原创 2020-05-01 11:38:15 · 341 阅读 · 1 评论 -
leetcode 1046. 最后一块石头的重量
class Solution { public int lastStoneWeight(int[] stones) { Arrays.sort(stones); int len = stones.length - 1; for(int i = len; i > 0; i --) { stones[len] = s...原创 2020-04-18 14:23:56 · 110 阅读 · 0 评论 -
leetcode 860. 柠檬水找零
class Solution { public boolean lemonadeChange(int[] bills) { int five = 0; int ten = 0; for(int i = 0; i < bills.length; i ++) { if(bills[i] == 5) { ...原创 2020-04-18 13:58:50 · 168 阅读 · 0 评论 -
leetcode 392. 判断子序列
方法一:双指针class Solution { public boolean isSubsequence(String s, String t) { int si = 0; int ti = 0; while (si < s.length() && ti < t.length()) { ...原创 2020-04-17 17:44:27 · 100 阅读 · 0 评论 -
leetcode 455. 分发饼干
import java.util.Arrays;class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); // 胃口值大小排序 Arrays.sort(s); // 饼干大小排序 int gi = g.length - 1; ...原创 2020-04-17 16:46:11 · 106 阅读 · 0 评论 -
LeetCode70. 爬楼梯
动态规划import java.util.Scanner;public class Solution{ public int climbStairs(int n){ if (n == 1) return 1; int[] dp = new int[n + 1]; dp[1] = 1; dp[2]...原创 2020-03-06 14:04:13 · 95 阅读 · 0 评论 -
LeetCode343,整数拆分
/*暴力搜索class Solution { public int integerBreak(int n) { if(n == 2){ return 1; } int res = -1; for (int i = 1; i <= n - 1; i++) { res = M...原创 2020-01-18 21:00:57 · 112 阅读 · 0 评论 -
236,二叉树的最近公共祖先
分析:LCA问题,仍用递归解决class Solution{public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q){ if(root == nullptr) return nullptr; if(root == p ||...原创 2019-09-17 21:24:16 · 67 阅读 · 0 评论 -
235,二叉搜索树的最近公共祖先
分析:充分利用二叉搜索树的特性以及递归解决问题class Solution{public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q){ if(root == nullptr) return nullptr; if(root...原创 2019-09-17 21:26:14 · 95 阅读 · 0 评论 -
leetcode107,二叉树的层次遍历2
之所以记录下这题,是因为找一个/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {...原创 2019-10-06 12:40:21 · 128 阅读 · 0 评论 -
leetcode690
分析:bfs/*// Employee infoclass Employee {public: // It's the unique ID of each node. // unique id of this employee int id; // the importance value of this employee int importanc...原创 2019-10-07 12:02:52 · 116 阅读 · 0 评论 -
leetcode993
分析:dfs/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; ...原创 2019-10-07 12:34:20 · 192 阅读 · 0 评论 -
leetcode17
分析:递归回溯class Solution {private: const string letterMap[10] = { " ", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv",...原创 2019-10-07 15:06:47 · 189 阅读 · 0 评论 -
1008,从前序遍历与中序遍历序列构造二叉树
分析:该问题是经典问题,混乱点在于运用递归解题时如何去维护变量和边界,这也是编程的难点。1、边界无效的判定2、利用前序左边界初始化根结点3、利用循环找到中序遍历数组中根结点的索引4、计算中序遍历数组中是左子树的元素个数5、递归(注意维护边界条件)6、返回根结点class Solution{public: TreeNode* buildTree(vector<int&g...原创 2019-09-17 20:41:06 · 99 阅读 · 0 评论