
Leetcode
JeraKrs
本人目前就职于百度商业研发部,有需要内推的朋友简历可发我邮箱 jerakrs@qq.com
展开
-
Leetcode 121 Best Time to Buy and Sell Stock(水)
题目连接:Leetcode 121 Best Time to Buy and Sell Stock解题思路:遍历过程中,维护当前碰到的最小值,并用当前值减去目前的最小值,维护最大收益。class Solution { public: int maxProfit(vector<int>& prices) { if (prices.size() == 0) return...原创 2018-06-22 02:53:53 · 210 阅读 · 0 评论 -
Leetcode 122 Best Time to Buy and Sell Stock II(水)
题目连接:Leetcode 122 Best Time to Buy and Sell Stock II解题思路:从左向右遍历,每次用当天的值减去前一天的值,如果亏损,则不进行交易。class Solution { public: int maxProfit(vector<int>& prices) { if (prices.size() == 0) return ...原创 2018-06-22 02:53:59 · 174 阅读 · 0 评论 -
Leetcode 123 Best Time to Buy and Sell Stock III(递推)
题目连接:Leetcode 123 Best Time to Buy and Sell Stock III解题思路:先从左到右遍历一遍,用Leetcode 121 的方法,求出在第i天以前可以获得的最大收益dp[i]。然后从右向左遍历,计算从第i天到左后一天的最大收益,维护两段收益的最大和。class Solution { public: int maxProfit(vector<in...原创 2018-06-22 02:54:05 · 237 阅读 · 0 评论 -
Leetcode 124 Binary Tree Maximum Path Sum(树形dp)
题目连接:Leetcode 124 Binary Tree Maximum Path Sum解题思路:每次递归遍历,返回结点单向的最大值,遍历过程中维护最大值。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *r...原创 2018-06-22 02:54:12 · 828 阅读 · 0 评论 -
Leetcode 125 Valid Palindrome(水)
题目连接:Leetcode 125 Valid Palindrome解题思路:将除了字母和数字的字符去除掉,判断是否为回文串。class Solution { public: bool isPalindrome(string s) { int n = 0; for (int i = 0; i < s.size(); i++) { if ((s[i] >= 'a...原创 2018-06-22 02:54:35 · 235 阅读 · 0 评论 -
Leetcode 126 Word Ladder II(BFS)
题目连接:Leetcode 126 Word Ladder II解题思路:将每个字符串看做结点,结点间存在边的条件为两个字符串差异字符数等于1。建图后,从起始结点开始进行BFS,因为要求最短路径,所以用一个数组记录每个结点是否遍历过。class Solution { public: bool trans(string a, string b) { bool diff = false;...原创 2018-06-22 02:54:40 · 798 阅读 · 0 评论 -
Leetcode 127 Word Ladder(BFS)
题目连接:Leetcode 127 Word Ladder解题思路:参考 Leetcode 126,将路径保存改为记录路径长度。class Solution { public: bool trans(string a, string b) { bool diff = false; for (int i = 0; i < a.size(); i++) { if (a[...原创 2018-06-22 02:54:45 · 356 阅读 · 0 评论 -
Leetcode 128 Longest Consecutive Sequence(水)
题目连接:Leetcode 128 Longest Consecutive Sequence解题思路:将数组排序,然后遍历维护最大连续长度。class Solution { public: int longestConsecutive(vector<int>& nums) { int ans = 1, n = nums.size(); if (n == 0) ...原创 2018-06-22 02:54:50 · 190 阅读 · 0 评论 -
Leetcode 129 Sum Root to Leaf Numbers(递归)
题目连接:Leetcode 129 Sum Root to Leaf Numbers解题思路:递归,到叶子结点是加入总和。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNo...原创 2018-06-22 02:54:55 · 249 阅读 · 0 评论 -
Leetcode 130 Surrounded Regions(BFS)
题目连接:Leetcode 130 Surrounded Regions解题思路:所有与边界连接的点,是不会被包围的,所有以边界点为起始点,进行BFS,所有没有遍历过的点赋值‘X’。class Solution { public: void solve(vector<vector<char>>& board) { if (board.size() == ...原创 2018-06-22 02:55:01 · 323 阅读 · 0 评论 -
Leetcode 131 Palindrome Partitioning(DFS)
题目连接:Leetcode 131 Palindrome Partitioning解题思路:每次枚举往后取若干字符组成的字符串是否为回文串,如果是回文串则递归继续分割后面剩余的字符串。class Solution { public: bool isPalindrome(string s) { int n = s.size(); for (int i = 0; i + i <...原创 2018-06-24 04:59:10 · 242 阅读 · 0 评论 -
Leetcode 132 Palindrome Partitioning II(BFS)
题目连接:Leetcode 132 Palindrome Partitioning II解题思路:每次向后枚举若干字符,判断这些字符组成的字符串是否为回文串。因为要求最小切割数,所以用BFS,记录当前切割到的位置。class Solution { public: bool isPalindrome(string s) { int n = s.size(); for (int i ...原创 2018-06-24 04:59:23 · 344 阅读 · 0 评论 -
Leetcode 133 Clone Graph(STL)
题目连接:Leetcode 133 Clone Graph解题思路:对每个结点用new方法新建一个一样的结点,并且将对应的label和新节点指针的对应关系保存在Map中,每次遍历一个结点是,对应在节点的neighbors中放入对应新结点的指针。/** * Definition for undirected graph. * struct UndirectedGraphNode { * ...原创 2018-06-24 04:59:28 · 347 阅读 · 0 评论 -
Leetcode 134 Gas Station(环形最大连续和)
题目连接:Leetcode 134 Gas Station解题思路:目标可以转换为从哪个位置开始,可以获得连续最长的净Gas收入。class Solution { public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { int n = gas.size(); ...原创 2018-06-24 04:59:34 · 626 阅读 · 0 评论 -
Leetcode 135 Candy(BFS)
题目连接:Leetcode 135 Candy解题思路:一个位置,如果不高于它左右两边,则一定为1,然后以这些点作为起点,进行BFS,每次更新邻居中比自己高的手中的糖果值为自己的+1,如果邻居的被更新了,则放入队列。最后累加所有人手中的糖果数。class Solution { public: int candy(vector<int>& ratings) { int...原创 2018-06-24 04:59:47 · 305 阅读 · 0 评论 -
Leetcode 136 Single Number(水)
题目连接:Leetcode 136 Single Number解题思路:求所有数的亦或和。class Solution { public: int singleNumber(vector<int>& nums) { sort(nums.begin(), nums.end()); int ans = 0; for (int i = 0; i < nu...原创 2018-06-24 04:59:52 · 279 阅读 · 0 评论 -
Leetcode 137 Single Number II(水)
题目连接:Leetcode 137 Single Number II解题思路:将数组排序,和前后都不相同的数即为signle number。class Solution { public: int singleNumber(vector<int>& nums) { int n = nums.size(); sort(nums.begin(), nums.end...原创 2018-06-24 04:59:58 · 405 阅读 · 0 评论 -
Leetcode 138 Copy List with Random Pointer(STL)
题目连接:Leetcode 138 Copy List with Random Pointer解题思路:首先遍历一遍链表,为每个结点新建一个拷贝,并将对应label和指针关系保存在map中。然后再遍历一遍数组,更新每个新结点的random和next指针。/** * Definition for singly-linked list with a random pointer. * struct...原创 2018-06-24 05:00:03 · 295 阅读 · 0 评论 -
Leetcode 139 Word Break(DP)
题目连接:Leetcode 139 Word Break解题思路:dp[i]表示到第i个字符,可否被表示,每次在dp[i]=true的位置,向后枚举。class Solution { public: bool wordBreak(string s, vector<string>& wordDict) { int n = s.size(); bool* dp =...原创 2018-06-24 05:00:09 · 429 阅读 · 0 评论 -
Leetcode 140 Word Break II(DFS+dp)
题目连接:Leetcode 140 Word Break II解题思路:递归遍历,每次枚举一个词,并且在剩下的位置可被切割的情况递归,作为剪枝。class Solution { public: bool dfs(int d, string s, string t, vector<string>& wordDict, vector<string>& an...原创 2018-06-24 05:00:15 · 435 阅读 · 0 评论 -
Leetcode 141 Linked List Cycle(链表)
题目连接:Leetcode 141 Linked List Cycle解题思路:创建两个指针,从起始位置开始,一个走一步,一个走两步,如果存在环,两个指针会有相等的时候。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo...原创 2018-06-25 20:45:28 · 335 阅读 · 0 评论 -
Leetcode 142 Linked List Cycle II(STL)
题目连接:Leetcode 142 Linked List Cycle II解题思路:用set记录遍历过的结点,如果有结点在遍历过程中两次经过,则为环的起点。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(in...原创 2018-06-25 20:45:34 · 279 阅读 · 0 评论 -
Leetcode 143 Reorder List(链表操作)
题目连接:Leetcode 143 Reorder List解题思路:首先遍历一遍链表,统计一下链表的元素个数,然后再对链表进行对半切割。然后从每次从两个链表中取一个加入新链表的末尾,注意奇数个元素的情况。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *n...原创 2018-06-25 20:45:39 · 328 阅读 · 0 评论 -
Leetcode 144 Binary Tree Preorder Traversal(STL)
题目连接:Leetcode 144 Binary Tree Preorder Traversal解题思路:用栈模拟递归,每次向栈中放入当前结点的值,并将指针赋值为当前结点的左指针。如果当前指正为NULL,则取出栈顶元素,将指针赋值为它的右指针。/** * Definition for a binary tree node. * struct TreeNode { * int val;...原创 2018-06-25 20:45:44 · 283 阅读 · 0 评论 -
Leetcode 145 Binary Tree Postorder Traversal(STL)
题目连接:Leetcode 145 Binary Tree Postorder Traversal解题思路:与Leetcode 144 类似,不过在碰到当前指针为NULL时,先判断栈顶元素是否为NULL,为空,则表示上一个结点的右子树也遍历过,不为NULL,则向令当前指针指向栈顶结点的右指针,并向栈中push一个空指针。/** * Definition for a binary tree nod...原创 2018-06-25 20:45:49 · 352 阅读 · 0 评论 -
Leetcode 146 LRU Cache(双向链表+STL)
题目连接:Leetcode 146 LRU Cache解题思路:用一个双向链表,维护一个最近访问次序,用map记录对应key的结点指针。对于get请求,需要将当前结点移动到链表的头位置;对于put操作,如果是更新,则同样将当前结点移动到头位置,如果不是更新,则在头位置插入一个新结点。如果链表长度超过缓存上限,则删除末尾结点并清空map中对应的记录。class LRUCache { private...原创 2018-06-25 20:45:54 · 579 阅读 · 0 评论 -
Leetcode 147 Insertion Sort List(插入排序)
题目连接:Leetcode 147 Insertion Sort List解题思路:模拟插入排序,注意指针的变换即可。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(N...原创 2018-06-25 20:45:59 · 380 阅读 · 0 评论 -
Leetcode 148 Sort List(链表+归并)
题目连接:Leetcode 148 Sort List解题思路:每次将链表对半分成两个链表,分别排序,然后进行合并。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NU...原创 2018-06-25 20:46:04 · 399 阅读 · 0 评论 -
Leetcode 149 Max Points on a Line(暴力+gcd)
题目连接:Leetcode 149 Max Points on a Line解题思路:枚举每个点作为基准点,和它后面所有点作直线,用dy,dx表示斜率(注意保证斜率表示方式一致,gcd和符号)。用map记录有多少斜率相同的直线,注意相同点。/** * Definition for a point. * struct Point { * int x; * int y; * ...原创 2018-06-25 20:46:09 · 627 阅读 · 0 评论 -
Leetcode 150 Evaluate Reverse Polish Notation(栈)
题目连接:Leetcode 150 Evaluate Reverse Polish Notation解题思路:碰到数字入栈,碰到运算符,推出栈顶两个元素进行相应运算,将结果放回栈中。class Solution { public: int evalRPN(vector<string>& tokens) { stack<int> nums; for ...原创 2018-06-25 20:46:14 · 526 阅读 · 0 评论 -
Leetcode 344 Reverse String(水)
题目连接:Leetcode 344 Reverse String解题思路:遍历一半字符串,收尾互换。class Solution { public: string reverseString(string s) { int n = s.size(); string ret = s; for (int i = 0; i < n; i++) ret[i] = ...原创 2018-06-27 17:28:29 · 650 阅读 · 0 评论 -
Leetcode 001 Two Sum (Towpoint)
题目连接:Leetcode 001 Two Sum (Towpoint)解题思路:将数组排序,然后左指针从数组起始位置开始,右指针从数组终止位置开始。如果两个指针指向的值的和小于目标值,移动左指针,如果和大于目标值,移动右指针,直到和等于目标值。class Solution { public: vector<int> twoSum(vector<int>& n...原创 2018-06-06 20:09:05 · 241 阅读 · 0 评论 -
Leetcode 002 Add Two Numbers (大数求和 & 链表操作)
题目连接:Leetcode 002 Add Two Numbers 解题思路:将两个链表当作数组来对待,终止条件为指针指向 NULL,然后就是简单的模拟两个大数求和。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode...原创 2018-06-06 20:18:38 · 233 阅读 · 0 评论 -
Leetcode 003 Longest Substring Without Repeating Characters (STL)
题目连接:Leetcode 003 Longest Substring Without Repeating Characters解题思路:用一个map来记录每个字符出现的位置,一个变量记录不重复子串的起始位置。然后遍历字字符串,如果当前字符没有出现过,就插入一条记录在map中;如果字符出现过,就要将当前不重复子串的起始位置 到 重复字符出现的位置(map中记录)之间的字符在map中删除,并且更新不...原创 2018-06-06 20:31:40 · 134 阅读 · 0 评论 -
Leetcode 004 Median of Two Sorted Arrays(三分)
题目连接:Leetcode 004 Median of Two Sorted Arrays解题思路:对两个数组进行三分,直到有一个数组为空或者两个数组各剩一个数。 array1: a1_l, n1_l, a1_m, n1_r, a1_r array2: a2_l, n2_l, a2_m, n2_r, a2_r其中,n1_...原创 2018-06-06 20:48:53 · 211 阅读 · 0 评论 -
Leetcode 005 Longest Palindromic Substring (Manachar)
题目连接:Leetcode 005 Longest Palindromic Substring 解题思路:裸的Manachar 算法,找到最大值后反推子串位置。class Solution { public: string longestPalindrome(string s) { const int maxn = 2 * 1e3 + 5; int pos, rad[maxn];...原创 2018-06-06 20:52:17 · 180 阅读 · 0 评论 -
Leetcode 006 ZigZag Conversion (模拟)
题目连接:Leetcode 006 ZigZag Conversion解题思路:假设一个周期需要n个数,那么第一行两个数的下标关系为0,n;第二行为1,n-1;以此类推。需要注意的是第一行和最后一行,一个周期里面只有一个数。class Solution { public: string convert(string s, int numRows) { if (numRows == 1)...原创 2018-06-06 21:00:24 · 170 阅读 · 0 评论 -
Leetcode 007 Reverse Integer (水)
题目连接:Leetcode 007 Reverse Integer class Solution { public: int reverse(int x) { int flag = 1, ans = 0; if (x < 0) { flag = -1; x = -x; } while (x) { int temp = ans * 10 ...原创 2018-06-06 21:05:25 · 166 阅读 · 0 评论 -
Leetcode 008 String to Integer (模拟)
题目连接:Leetcode 008 String to Integer解题思路:遍历字符串,对当前字符和当前状态进行判断。class Solution { public: int myAtoi(string str) { long ans = 0; int flag = 1; for (int i = 0; i < str.length(); i++) { i...原创 2018-06-06 21:08:27 · 202 阅读 · 0 评论 -
Leetcode 009 Palindrome Number(水)
题目连接:Leetcode 009 Palindrome Number解题思路:负数肯定不是对称数,而对于正数,用 Leetcode007中的方法将数翻转,如果等于原先的数,那么它就是对称数。class Solution { public: bool isPalindrome(int x) { if (x < 0) return false; int ans = 0, t ...原创 2018-06-06 21:11:19 · 159 阅读 · 0 评论