leetcode
eclipse_c
游戏程序员
展开
-
【leetcode】01背包类型问题
https://www.cnblogs.com/kkbill/p/12081172.html图解请参考上述文章#include "bag01.h"#include <algorithm>#include <vector>using namespace std;// 重点理解dp[i][w]表示什么含义int maxValue(int w[], int v[], const int count, const int bagSize){ // dp[i][w]原创 2020-06-30 17:07:47 · 297 阅读 · 0 评论 -
【leetcode】392. 判断子序列(贪心,easy)
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。示例 1:s = “abc”, t = “ahbgdc”返回 true.示例 2:s = “axc”, t = “ahbgdc原创 2020-06-30 11:23:21 · 230 阅读 · 0 评论 -
【leetcode】76. 最小覆盖子串(滑动窗口)
滑动窗口问题的通用解题思路:https://labuladong.gitbook.io/algo/di-ling-zhang-bi-du-xi-lie/hua-dong-chuang-kou-ji-qiao-jin-jie给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字符的最小子串。示例:输入: S = “ADOBECODEBANC”, T = “ABC”输出: “BANC”说明:如果 S 中不存这样的子串,则返回空字符串 ""。如果 S 中存在这样的子串,我们原创 2020-06-28 17:08:54 · 368 阅读 · 0 评论 -
【leetcode】34. 在排序数组中查找元素的第一个和最后一个位置(二分查找)
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。你的算法时间复杂度必须是 O(log n) 级别。如果数组中不存在目标值,返回 [-1, -1]。示例 1:输入: nums = [5,7,7,8,8,10], target = 8输出: [3,4]示例 2:输入: nums = [5,7,7,8,8,10], target = 6输出: [-1,-1]链接:https://leetcode-cn.com/problems/f原创 2020-06-24 20:41:49 · 149 阅读 · 0 评论 -
【leetcode】704. 二分查找(这不能一遍过?)
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。示例 1:输入: nums = [-1,0,3,5,9,12], target = 9输出: 4解释: 9 出现在 nums 中并且下标为 4示例 2:输入: nums = [-1,0,3,5,9,12], target = 2输出: -1解释: 2 不存在 nums 中因此返回 -1提示:你可以假设 nums 中的所原创 2020-06-24 20:24:27 · 94 阅读 · 0 评论 -
【leetcode】111. 二叉树的最小深度(BFS宽搜+递归)
给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回它的最小深度 2.链接:https://leetcode-cn.com/problems/minimum-depth-of-binary-treeeasy题目,看了提交记录,6年前提交了一次。今天又做了一次,然而使用的原创 2020-06-24 20:06:55 · 219 阅读 · 0 评论 -
【leetcode】16. 最接近的三数之和(回溯超时,两个指针优化)
给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。示例:输入:nums = [-1,2,1,-4], target = 1输出:2解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。提示:3 <= nums.length <= 10^3-10^3 <= nums[i] <= 10^3-10^4 <= t原创 2020-06-24 18:13:20 · 209 阅读 · 0 评论 -
【leetcode】105. 重建二叉树(剑指offer)中等难度
输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如,给出前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7]返回如下的二叉树: 3 / \ 9 20 / \ 15 7链接:https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof如何找到左子树的根的位置,和右子树根的位置原创 2020-06-22 21:04:17 · 145 阅读 · 0 评论 -
【leetcode】46. 全排列(回溯法,回溯类问题如何解决)
给定一个 没有重复 数字的序列,返回其所有可能的全排列。示例:输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]链接:https://leetcode-cn.com/problems/permutations解题思路:https://labuladong.gitbook.io/algo/di-ling-zhang-bi-du-xi-lie/hui-su-suan-fa-xiang原创 2020-06-19 16:19:52 · 190 阅读 · 0 评论 -
【leetcode】96. 不同的二叉搜索树(动态规划)
给定一个整数 n,求以 1 … n 为节点组成的二叉搜索树有多少种?示例:输入: 3输出: 5解释:给定 n = 3, 一共有 5 种不同结构的二叉搜索树:分析:可以转换成数学公式来解。12345…理解为一个数组,其中的每个节点都可以拎起来,当做根节点。故,可以划分为左子树有i-1个节点,右子树有n-1个节点。(其中一个位根节点)递归写法,必定超时。用hash表存一下也不行。// 还是超时int numTreesImpl(int n, unordered_map<int,原创 2020-06-18 17:10:46 · 528 阅读 · 0 评论 -
【leetcode】42. 连续子数组的最大和(动态规划)
输入一个整型数组,数组里有正数也有负数。数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)。示例1:输入: nums = [-2,1,-3,4,-1,2,1,-5,4]输出: 6解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。提示:1 <= arr.length <= 10^5-100 <= arr[i] <= 100注意:本题与主站 53 题相同:https://leetcode-cn.com/problem原创 2020-06-18 15:21:49 · 248 阅读 · 0 评论 -
【leetcode】322. 零钱兑换(动态规划)
给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。示例 1:输入: coins = [1, 2, 5], amount = 11输出: 3解释: 11 = 5 + 5 + 1示例 2:输入: coins = [2], amount = 3输出: -1说明:你可以认为每种硬币的数量是无限的。解题分析:https://labuladong.gitbook.io/algo/di原创 2020-06-18 14:50:52 · 256 阅读 · 0 评论 -
【leetcode】509. 斐波那契数(动态规划)
斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:F(0) = 0, F(1) = 1F(N) = F(N - 1) + F(N - 2), 其中 N > 1.给定 N,计算 F(N)。// https://leetcode-cn.com/problems/fibonacci-number/// 509. 斐波那契数// 递归,太慢了int fib(int N) { if (N <= 1)原创 2020-06-18 10:57:46 · 191 阅读 · 0 评论 -
【leetcode】239. 滑动窗口最大值(单调队列)
https://leetcode-cn.com/problems/sliding-window-maximum/解题思路:https://labuladong.gitbook.io/algo/shu-ju-jie-gou-xi-lie/dan-tiao-dui-lieclass MonotonicQueue{private: deque<int> data; // 单调队列中元素的个数不一定非要恒等于kpublic: // 比它小的都pop出去,保持队列的头部为最大 voi原创 2020-06-16 11:47:42 · 165 阅读 · 0 评论 -
【leetcode】496. 下一个更大元素 I (单调栈)
https://leetcode-cn.com/problems/next-greater-element-i/#include <stack>#include <unordered_map>using std::stack;using std::unordered_map;// https://leetcode-cn.com/problems/next-greater-element-i/// 单调栈 https://blog.csdn.net/lucky52529/原创 2020-06-16 10:28:30 · 154 阅读 · 0 评论 -
【leetcode】222. 完全二叉树的节点个数(附加 求完全二叉树叶子节点个数)
https://labuladong.gitbook.io/algo/shu-ju-jie-gou-xi-lie/wan-quan-er-cha-shu-jie-dian-shuhttps://leetcode-cn.com/problems/count-complete-tree-nodes/// 获取完全二叉树所有节点的个数,当然可以遍历一遍效率比较低int TreeSearch::countNodes(TreeNode* root){ if (root == NULL) return 0原创 2020-06-15 18:35:34 · 231 阅读 · 0 评论 -
【leetcode】146. LRU缓存机制
https://leetcode-cn.com/problems/lru-cache/// 头文件class LRUCache{public: LRUCache(int capacity); int get(int key); void put(int key, int value);private: std::unordered_map<int, std::list<std::pair<int, int>>::iterator> table; st原创 2020-06-15 16:02:31 · 119 阅读 · 0 评论 -
【leetcode】226. Invert Binary Tree(easy)
Invert a binary tree.Example:Input: 4/ 2 7/ \ / 1 3 6 9Output: 4/ 7 2/ \ / 9 6 3 1Trivia:This problem was inspired by this original tweet by Max Howell:Google...原创 2019-03-16 16:49:54 · 140 阅读 · 0 评论 -
【leetcode】206. Reverse Linked List(easy)
反转链表// 206. Reverse Linked ListListNode* Solution::reverseList(ListNode* head){ if (head == NULL || head-&gt;next == NULL) return head; ListNode * p = head; // p指向当前处理的原链表的位置 ListNode * res ...原创 2019-03-16 15:14:29 · 126 阅读 · 0 评论 -
【leetcode】53. Maximum Subarray(easy)
给一个数组,然后算这个数组中最大子串的和。O(n) 时间复杂度,也就是一次遍历不要被这个子串迷惑了,class Solution {public: int maxSubArray(vector&lt;int&gt;&amp; nums) { int res = INT_MIN; int cur = 0; for (auto it = nums.cbegin(...原创 2019-03-12 11:16:36 · 119 阅读 · 0 评论 -
【leetcode】198. House Robber(easy)
dp问题找一下规律,使用数学归纳法数组有一个元素那么就返回第一个,数组有两个元素,返回二者最大的,数组有3个的话,返回第二个和低三个与第一个和的最大值。用一个数组存一下dpdp[0] = n[0];dp[1] = n[1];dp[2] = max(dp[0] + n[2], dp[1])…dp[n] = max(dp[n-2] + n[n], dp[n-1])那么就很容易写出代...原创 2019-03-14 21:09:33 · 181 阅读 · 0 评论 -
【leetcode】3. Longest Substring Without Repeating Characters (medium)
Given a string, find the length of the longest substring without repeating characters.Example 1:Input: “abcabcbb”Output: 3Explanation: The answer is “abc”, with the length of 3.Example 2:Input: ...原创 2019-03-02 16:39:51 · 109 阅读 · 0 评论 -
【leetcode】728. Self Dividing Numbers -- easy
https://leetcode.com/problems/self-dividing-numbers/description/A self-dividing number is a number that is divisible by every digit it contains.For example, 128 is a self-dividing number because 128...原创 2018-10-16 17:22:26 · 148 阅读 · 0 评论 -
【leetcode】27. Remove Element(easy)
将指定值的元素移动到最后,并返回修改后数组的size。元素可以无序不知道为啥时间复杂度堪忧。代码确实复杂了点,也可能是题目的要求没看懂//27. Remove Elementint removeElement(vector& nums, int val) { int lastPos = 0; // 最后一个可以交换位置 距离最后一个的距离,初始为0 int len = 0;原创 2018-01-10 20:19:59 · 195 阅读 · 0 评论 -
【leetcode】38. Count and Say(easy)
还是字符串题目,1 对应“1”,第二个根据上一个的“1”,为1个“1”,即“11”,第三个分析“11”,两个1,即“21”,依次分析下去,递归即可。还是需要注意边界,以及The count-and-say sequence is the sequence of integers with the first five terms as following:1.原创 2018-01-10 17:48:11 · 177 阅读 · 0 评论 -
【leetcode】121. Best Time to Buy and Sell Stock(easy)
与 53. Maximum Subarray类似。https://blog.csdn.net/eclipse_c/article/details/88417065Kadane算法算法的变形// 121. Best Time to Buy and Sell Stock// 对于原数组,如果我们将arr[i]-arr[i-1],也就是股票某日与前一日价格差,依次排列成一个数组,那么所求问题的...原创 2019-03-12 16:15:55 · 133 阅读 · 0 评论 -
【leetcode】155. Min Stack(easy)
在栈的结构上多加一个getMin方法,返回栈中的最小值。使用两个栈,一个来保存原有的值,另外一个初始push进去第一个值,当push第二个的时候,检查是不是比第一个小,如果比第二个栈的栈顶小,那么就push进去。// 155 Min Stackclass MinStack{ /** initialize your data structure here. */public: MinSt...原创 2019-03-12 17:20:20 · 127 阅读 · 0 评论 -
【leetcode】Move Zeroes[easy]
好久没玩这玩意了,就先挑了个最简单的开始练练手,一次ac,但是这运行时间有点捉急。题目很简单,给你一串Int型数组,将0元素都放到最后,上代码。for (auto it = t.begin(); it != t.end(); it++) { if (*it == 0) { for (auto it2 = it+1; it2 != t.end(); i原创 2015-11-07 23:54:08 · 316 阅读 · 0 评论 -
【leetcode】538. Convert BST to Greater Tree(easy)
给定一颗二叉树,从右侧中序遍历,然后不断的加和就可以了。一次acvoid converBSTImpl(TreeNode* root, int& value){ if (root) { if (root->right) { converBSTImpl(root->right, value); } value = value + root->v...原创 2019-03-29 11:01:57 · 175 阅读 · 0 评论 -
【leetcode】448. Find All Numbers Disappeared in an Array(easy,数组下标和值的技巧)
// 448. Find All Numbers Disappeared in an Arrayvector<int> findDisappearedNumbers(vector<int>& nums){ // 这种数组要求on遍历的,而且时间复杂度O(n) 空间O(1)的,就需要动动脑筋,想下数组的特性 // 尤其是这题目的限制条件,就可以把数组的inde...原创 2019-03-28 19:19:22 · 139 阅读 · 0 评论 -
【leetcode】 438. Find All Anagrams in a String (easy 窗口滑动)
卡了好几天,这个问题,实际上很简单,说到底还是stl里的容器掌握的不熟练类似窗口滑动,每次去比较,这里有个技巧,假定字符串里都是26个英文字母。// 438. Find All Anagrams in a String (窗口滑动)vector<int> Solution::findAnagrams(string s, string p){ vector<int>...原创 2019-03-28 17:11:43 · 150 阅读 · 0 评论 -
【leetcode】437. Path Sum III(easy,DFS深搜)
看代码,深搜要记得把每个节点当做根节点遍历一次int pathSumImpl(TreeNode * root, int sum){ if (root == NULL) return 0; return (root->val == sum ? 1 : 0) + pathSumImpl(root->left, sum - root->val) + pathSumImpl(r...原创 2019-03-26 18:54:26 · 210 阅读 · 0 评论 -
【leetcode】234. Palindrome Linked List(easy)
判断一个链表是不是回文链表。这里用到了前两天做的题目,反转链表。思路:一个快指针一个满指针,找到中间点,然后将中间点之后的链表反转,然后在遍历一次,总共是遍历了两次,时间O(n) ,空间O(1)/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *nex...原创 2019-03-18 11:06:58 · 155 阅读 · 0 评论 -
【leetcode】169. Majority Element(easy 一次ac)
虽然效率较低,但是一次ac了。找数组中出现次数大于一半数组大小的数字。// 169. Majority Elementint Solution::majorityElement(vector<int>& nums){ unordered_map<int, int> m; for (int i = 0; i < nums.size(); ++i) ...原创 2019-03-12 19:30:47 · 121 阅读 · 0 评论 -
【leetcode】160. Intersection of Two Linked Lists(easy)
查找两个链表,有重合的那个节点。注意不要比链表中节点的值,而要比较指针将两个链表的长度统一,然后一边遍历即可。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NU...原创 2019-03-12 18:49:08 · 160 阅读 · 0 评论 -
【leetcode】28. Implement strStr()(easy) KMP算法
匹配子串。手写一个效率非常低的。倒数的算法。(应该去用kmp算法求解)// 28. Implement strStr()int strStr(string haystack, string needle) { if (haystack.size() < needle.size()) return -1; if (needle.size() == 0) return 0原创 2018-01-10 10:27:59 · 203 阅读 · 0 评论 -
【leetcode】14. Longest Common Prefix(easy)
本题的意思是求所有字符串的最长公共前缀,即数组的所有字符串都包含这个前缀。abcdefgabcccc返回 abc这种题目还是抠细节的,比如for循环中当result为“”空字符串时,只在第一次初始化,后面如果result还是空串,那么就不初始化。需要加入一个标志位,如果不匹配了,那么就跳出循环// 2018.1.8//14. Longest Common Prefix原创 2018-01-09 11:14:38 · 186 阅读 · 1 评论 -
Evaluate Reverse Polish Notation【leetcode 2】
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1",原创 2014-09-15 19:02:46 · 606 阅读 · 0 评论 -
Insertion Sort List 【leetcode】
今天面试第一个笔试通过的公司(去哪儿网),今天早上就ko原创 2014-09-17 17:06:13 · 558 阅读 · 0 评论 -
Single Number 【leetcode】1分钟解题系类
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using ext原创 2014-09-21 22:39:26 · 565 阅读 · 0 评论