-------Leetcode题目
计算机的小粽子
2019年本科毕业于湖南大学,目前任职字节跳动
展开
-
1217. Minimum Cost to Move Chips to The Same Position(贪心)
链接:https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/思路:位置相差2不产生消耗,所以奇数或者偶数位置之间是等价的,实际只要得出哪个哪个数量少就行代码:func minCostToMoveChips(position []int) int { var ( even_number = 0 position_len = len(position) )原创 2021-04-11 16:47:54 · 229 阅读 · 0 评论 -
1019. Next Greater Node In Linked List(链表)
https://leetcode.com/problems/next-greater-node-in-linked-list/、题目:求每个节点后面比他大的节点思路:stack中维持递降的顺序,批量求解,时间复杂度: O(n), 空间复杂度O(n)代码class Solution {public: vector<int> nextLargerNodes(ListNod...原创 2020-02-09 13:07:19 · 394 阅读 · 0 评论 -
1290. Convert Binary Number in a Linked List to Integer(链表)
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/题目:将二进制转换为整数比较简单,直接放代码:func getDecimalValue(head *ListNode) int { return func(ret int) int { for head != ni...原创 2020-02-09 11:38:20 · 299 阅读 · 0 评论 -
921. Minimum Add to Make Parentheses Valid
链接:https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/题目:求最少括号匹配数目思路:出现 ’)’ 并且’(’数目大于0,则匹配,需要匹配的数目减1代码func minAddToMakeValid(S string) int { var ( left = 0 ret = 0...原创 2020-01-29 11:29:03 · 164 阅读 · 0 评论 -
1 Two Sum (数组)
https://leetcode.com/problems/two-sum/description/在一个数组中,找出2个数的和等于给定的数。直接用map映射即可。class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { map<int, int> m; vect原创 2017-08-13 19:48:02 · 192 阅读 · 0 评论 -
561. Array Partition I (数组)
https://leetcode.com/problems/array-partition-i/description/题意:给定一个长度为2n(偶数)的数组,分成n个小组,返回每组中较小值的和sum,使sum尽量大 思路:直接排序,然后将下标为偶数的元素累加即可(下标从0开始)。class Solution {public: int arrayPairSum(vector<int>& n原创 2017-08-20 19:33:34 · 226 阅读 · 0 评论 -
112. Path Sum (二叉树)
https://leetcode.com/problems/path-sum/description/题目大意:给定一个二叉树和一个值sum,判断是否存在一个从根节点到叶子节点的路径,使得路径上每个节点值之和等于sum?第一种解法:递归/** * Definition for a binary tree node. * struct TreeNode { * int val; *原创 2017-08-20 20:31:02 · 211 阅读 · 0 评论 -
141. Linked List Cycle (链表)
https://leetcode.com/problems/linked-list-cycle/description/题目:判断链表是否存在环。用2个指针,慢指针每次加一,快指针每次加二,如果有一个指针为NULL ,则没有环,如果两个指针相等,则存在环。/** * Definition for singly-linked list. * struct ListNode { * int原创 2017-08-21 17:10:41 · 173 阅读 · 0 评论 -
237. Delete Node in a Linked List(链表)
https://leetcode.com/problems/delete-node-in-a-linked-list/description/题目:将链表中的某个节点删除,但是给定的节点就是需要删除的节点 思路:采用替换原则。例如 1->2->3->4 需要删除2,先进行值替换得到: 1->3->3->4 , 然后删除 “需要删除的节点”的下一节点得到 1->3->4。/** * Definit原创 2017-08-21 17:41:15 · 203 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List (链表)
https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/题目: 去除链表中的重复元素。思路: 用2个指针遍历即可。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode原创 2017-08-21 17:57:03 · 161 阅读 · 0 评论 -
343. Integer Break
https://leetcode.com/problems/integer-break/description/题目:整数拆分。解法一 思路:拆成尽量多的3。 证明: >= 得:当把输入的n拆分成几个相等的数时它们的积最大。那么问题来了,拆分成几个呢?为了方便使用导数,我们先假设我们可以把n拆分成实数。那么设每一个数为x,则一共有n/x个数。设它们的积为f(x),则f(x)=x原创 2017-08-24 20:13:42 · 162 阅读 · 0 评论 -
203. Remove Linked List Elements(链表)
https://leetcode.com/problems/remove-linked-list-elements/description/题目:删除链表中的元素思路:直接用2个指针即可。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *原创 2017-08-22 10:27:56 · 202 阅读 · 0 评论 -
92. Reverse Linked List II(链表)
https://leetcode.com/problems/reverse-linked-list-ii/submissions/1题目:翻转一部分链表第一种,只交换链表元素的值。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * Li原创 2017-09-03 19:10:20 · 167 阅读 · 0 评论 -
338. Counting Bits(动态规划)
https://leetcode.com/problems/counting-bits/description/题目:求0~num中每个数1的个数。思路:动态规划,方程,v[n]=v[n>>1]+n%2。class Solution {public: vector<int> countBits(int num) { vector<int>v;v.push_back(0);原创 2017-09-04 16:40:26 · 355 阅读 · 0 评论 -
53. Maximum Subarray (dp)
https://leetcode.com/problems/maximum-subarray/description/题目:求最大字段和思路: 直接用动态规划即可。class Solution {public: int maxSubArray(vector<int>& nums) { int max,b=0,len=nums.size(); for(int原创 2017-08-23 11:59:05 · 267 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock (dp)
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/题目:求数组中两个数差值的最大值 思路:简单dp。class Solution {public: int maxProfit(vector<int>& prices) { if(prices.empty()) return 0原创 2017-08-23 12:32:44 · 155 阅读 · 0 评论 -
392. Is Subsequence (模拟)
https://leetcode.com/problems/is-subsequence/description/题目: 判断一个字符串是否是另一个字符串的前缀。思路: l 表示s的下标,r表示t的下标,s[l]==t[r],l++,r++;否则r++。刚开始加了特判,错了,后面发现有一个特殊输入 “ ”, “ ”。此时长度都为0.。。。。class Solution {public: b原创 2017-08-27 20:33:39 · 196 阅读 · 0 评论 -
70. Climbing Stairs (dp)
https://leetcode.com/problems/climbing-stairs/description/题目: 略思路:简单dpclass Solution {public: int climbStairs(int n) { int dp[1000005]; dp[0]=dp[1]=1; for(int x=2;x<=n;x++)原创 2017-08-28 11:45:13 · 189 阅读 · 0 评论 -
206. Reverse Linked List (链表)
https://leetcode.com/problems/reverse-linked-list/description/题目: 反转链表思路: 用一个栈,实现反转。(栈保存val就行,不能保存为链表节点)。。。。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNod原创 2017-08-29 18:00:10 · 189 阅读 · 0 评论 -
563. Binary Tree Tilt (二叉树)
https://leetcode.com/problems/binary-tree-tilt/description/题目:这题其实要你求每个节点的左右子树的差的绝对值,再对这些绝对值求和. 思路:简单递归搜索class Solution {public: int dfs(TreeNode *node) { if(node==NULL) return 0;原创 2017-09-23 21:36:45 · 267 阅读 · 0 评论 -
543. Diameter of Binary Tree (二叉树)
https://leetcode.com/problems/diameter-of-binary-tree/description/题目:这道题让我们求二叉树的直径。思路:求出每一个节点的左右子树的直径。更新最大值。然后当前节点的长度等于左右子树节点的最大值加1。class Solution {public: int dfs(TreeNode *root) { if原创 2017-09-23 22:20:20 · 150 阅读 · 0 评论 -
232. Implement Queue using Stacks (栈)
https://leetcode.com/problems/implement-queue-using-stacks/description/题目:要求用栈实现队列的基本操作。思路:用一个栈即可。 比如压入的元素是1 2 3 4 。那么栈顶到栈底的元素就是 1 2 3 4。实现上面的效果,改写Push函数即可(写成递归)。比如当前栈顶到栈底的元素为:1 2 ,需要压入 3暂存1 ,弹出1,暂存2,弹原创 2017-09-24 10:23:34 · 188 阅读 · 0 评论 -
404. Sum of Left Leaves (二叉树)
https://leetcode.com/problems/sum-of-left-leaves/description/题目:求所有左叶子的和。思路: 递归。。。class Solution {public: int sum=0; void dfs(TreeNode *root,int x) { if(root->left==NULL&&root->rig原创 2017-09-24 19:22:55 · 157 阅读 · 0 评论 -
55. Jump Game(dp, 贪心)
https://leetcode.com/problems/jump-game/description/题目:判断是否可以跳到最后一个节点?解法一:只要有一个可达的子节点能够到达最后一个节点,该节点即可到达最后一个子节点,否则无法到达。我们从后往前遍历。 例如: (0表示不可达,1表示可达) 3 2 1 0 4 0 0 0 0 1所以我们得原创 2017-09-16 15:35:32 · 376 阅读 · 0 评论 -
134. Gas Station (数学)
https://leetcode.com/problems/gas-station/description/题目:有N个加油站点构成一个环状,每个站点i加油量为gas[i],从站点i到站点i+1需要耗费有两为cost[i],现要求从哪个站点出发可以成功转一圈回到初始站点,返回该站点,若没有则返回-1;思路: 这个题要用反证法来理解。算法: 从i开始,j是当前station的指针,sum += g原创 2017-09-16 16:31:55 · 280 阅读 · 0 评论 -
148. Sort List (链表)
https://leetcode.com/problems/sort-list/description/题目: 链表排序思路: 我直接对值进行排序。。。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x)原创 2017-08-31 08:16:51 · 188 阅读 · 0 评论 -
349. Intersection of Two Arrays (set)
https://leetcode.com/problems/intersection-of-two-arrays/description/题目: 求集合的交集。第一种解法,直接用set集合。class Solution {public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { se原创 2017-09-01 14:50:06 · 140 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted (双指针)
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/题目:在数组中找出2个数的和等于目标值。思路:双指针,和大于目标值r–,小于目标值l++,否则 返回结果。class Solution {public: vector<int> twoSum(vector<int>& numbers, int原创 2017-09-01 15:04:46 · 154 阅读 · 0 评论 -
160. Intersection of Two Linked Lists(链表)
https://leetcode.com/problems/intersection-of-two-linked-lists/description/题目:找2个链表的公共节点。思路:遍历2遍,然后判断。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *nex原创 2017-08-21 18:25:04 · 143 阅读 · 0 评论 -
455. Assign Cookies (贪心)
https://leetcode.com/problems/assign-cookies/description/题目:将若干块蛋糕分配给若干个孩子。使得分配到蛋糕的孩子数量最大思路:贪心。将最小块蛋糕分配给需求最小的孩子,然后依次类推。class Solution {public: int findContentChildren(vector<int>& g, vector<int>&原创 2017-08-25 11:55:42 · 290 阅读 · 0 评论 -
547. Friend Circles(并查集)
https://leetcode.com/problems/friend-circles/description/题目:求朋友圈的个数。思路: 非常简单的并查集。。。class Solution {public: int fat[205]; void init() { for(int x=0;x<205;x++) fat[x]=x; } i原创 2017-08-29 18:47:23 · 463 阅读 · 0 评论 -
20. Valid Parentheses(栈)
https://leetcode.com/problems/valid-parentheses/description/题目:判断某个字符序列是否合法思路:如果是左括号,直接压栈,如果是右括号,如果栈顶元素不匹配当前元素或者栈为空,return 0,否则弹出元素。最后判断是否为空。class Solution {public: bool isValid(string s) {原创 2017-09-27 16:11:10 · 141 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree (二叉树)
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/题目:求二叉树高度。思路:直接遍历,找到一个叶子节点,更新最大值。class Solution {public: int sum=0; void dfs(TreeNode *root,int i) { if(!root原创 2017-09-25 19:39:50 · 82 阅读 · 0 评论 -
91. Decode Ways (dp)
https://leetcode.com/problems/decode-ways/description/题目:求编码数目思路:动态规划class Solution {public: int numDecodings(string s) { int len=s.length(); int *dp=new int[len+1]; dp[0]=原创 2017-09-19 17:15:21 · 340 阅读 · 0 评论 -
378. Kth Smallest Element in a Sorted Matrix (二分)
https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/description/题目:二维数组的第k小元素。第一种解法:直接排序。(n^2*log(n^2))class Solution {public: int kthSmallest(vector<vector<int>>& matrix, int k)原创 2017-09-02 18:03:12 · 221 阅读 · 0 评论 -
657. Judge Route Circle (字符串)
https://leetcode.com/problems/judge-route-circle/description/题目: 判断机器人是否可以回到原点。思路: 位置只与移动方向有关,与移动顺序无关。比如 LLR 与 RLL的效果是一样的。class Solution {public: bool judgeCircle(string moves) { int l=0,u原创 2017-09-02 19:40:18 · 165 阅读 · 0 评论 -
557. Reverse Words in a String III (字符串)
https://leetcode.com/problems/reverse-words-in-a-string-iii/description/题目 : 翻转字符串中的每个单词。思路: 使用双指针,每次找到一个单词,就直接进行字符串翻转就可以了。class Solution {public: string reverseWords(string s) { int len=s原创 2017-09-02 20:13:10 · 235 阅读 · 0 评论 -
344. Reverse String (字符串)
https://leetcode.com/problems/reverse-string/description/题目:翻转字符串。思路:直接首位交换即可 (O(n))。class Solution {public: string reverseString(string s) { int l=0,r=s.length()-1; while(l<r)原创 2017-09-03 14:02:07 · 228 阅读 · 0 评论 -
523. Continuous Subarray Sum (前缀和)
https://leetcode.com/problems/continuous-subarray-sum/description/题目:判断某一个字段和是否可以整除k思路1 :前缀和class Solution {public: bool checkSubarraySum(vector<int>& nums, int k) { int len =nums.size();原创 2017-09-20 20:54:50 · 253 阅读 · 0 评论 -
435. Non-overlapping Intervals(贪心)
https://leetcode.com/problems/non-overlapping-intervals/description/题目:求最少删除的区间数目,使得区间不重叠。思路:活动安排问题的变形,求出最多可以保留多少区间,然后用总的区间减去保留数目即可。贪心算法,将所有区间按右边界升序排序,如果当前区间的最左边的值大于等于已保留的最右边区间的值,则保留当前区间。if(intervals[x原创 2017-09-21 17:07:31 · 339 阅读 · 0 评论