自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

不慌不忙、不急不躁

https://github.com/JeraKrs

  • 博客(2258)
  • 收藏
  • 关注

原创 【索引】2015 ACM/ICPC Asia Regional Online

2015 ACM/ICPC Asia Regional OnlineProblem 1001: hdu 5484 Monitor the AlpacasProblem 1002: hdu 5485 The Relationship in ClubProblem 1003: hdu 5486 Difference of ClusteringProblem 1004

2015-10-02 21:33:21 2075

原创 【索引】 AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu)

AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu)Chapter 1. Algorithm DesignChapter 2. MathematicsChapter 3. Data StructuresChapter 4. GeometryChapter 5. Graph TheoryChapte

2013-12-08 22:30:50 2457

原创 【索引】AOAPC I: Beginning Algorithm Contests (Rujia Liu)

AOAPC I: Beginning Algorithm Contests (Rujia Liu)Volume 0. Getting StartedVolume 1. Elementary Problem SolvingVolume 2. Data StructuresVolume 3. Brute ForceVolume 4. Algorithm DesignVolume 5

2013-08-07 22:31:21 4450 6

原创 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 579

原创 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 468

原创 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 526

原创 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 353

原创 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 322

原创 Leetcode 146 LRU Cache(双向链表+STL)

题目连接:Leetcode 146 LRU Cache解题思路:用一个双向链表,维护一个最近访问次序,用map记录对应key的结点指针。对于get请求,需要将当前结点移动到链表的头位置;对于put操作,如果是更新,则同样将当前结点移动到头位置,如果不是更新,则在头位置插入一个新结点。如果链表长度超过缓存上限,则删除末尾结点并清空map中对应的记录。class LRUCache { private...

2018-06-25 20:45:54 520

原创 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 287

原创 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 227

原创 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 283

原创 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 231

原创 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 275

原创 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 368

原创 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 347

原创 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 222

原创 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 350

原创 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 229

原创 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 256

原创 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 569

原创 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 260

原创 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 284

原创 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 199

原创 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 282

原创 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 210

原创 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 147

原创 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 297

原创 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 695

原创 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 158

原创 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 786

原创 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 185

原创 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 136

原创 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 167

原创 Leetcode 120 Triangle(DP)

题目连接:Leetcode 120 Triangle解题思路:dp[i][j] += min(dp[i+1][j], dp[i+1][j+1])。class Solution { public: int minimumTotal(vector<vector<int>>& triangle) { int m = triangle.size(); fo...

2018-06-20 02:32:47 312 1

原创 Leetcode 119 Pascal's Triangle II(水)

题目连接:Leetcode 119 Pascal's Triangle II解题思路:空间复杂度可以优化到o(n),每次在数组末尾追加一个1,然后从右往左遍历,每个数加上它前一个数。这样的操作执行n次。class Solution { public: vector<int> getRow(int rowIndex) { vector<int> ans; a...

2018-06-20 02:31:33 150

原创 Leetcode 118 Pascal's Triangle(水)

题目连接:Leetcode 118 Pascal's Triangle解题思路:空间复杂度可以优化到o(n),每次在数组末尾追加一个1,然后从右往左遍历,每个数加上它前一个数。这样的操作执行n次。class Solution { public: vector<vector<int>> generate(int numRows) { vector<vecto...

2018-06-20 02:29:28 160

原创 Leetcode 117 Populating Next Right Pointers in Each Node II(二叉树)

题目连接:Leetcode 117 Populating Next Right Pointers in Each Node II解题思路:和Leetcode 116 一样的做法。/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *le...

2018-06-20 02:27:25 164

原创 Leetcode 116 Populating Next Right Pointers in Each Node(二叉树)

题目连接:Leetcode 116 Populating Next Right Pointers in Each Node解题思路:二叉树的层次遍历,每个结点指向它的后并且同一层的一个结点。/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNo...

2018-06-20 02:26:28 159

原创 Leetcode 115 Distinct Subsequences(DP)

题目连接:Leetcode 115 Distinct Subsequences解题思路:dp[i][j]表示s[0]~s[j]匹配前i个字符有多少种方式(s[j]一定匹配t[i]),dp[i][j] = sum(dp[i-1][k]) 其中k<j,如果s[j] != t[i] 的话,dp[i][j] = 0。最后可以省略一层dp状态。class Solution { public: i...

2018-06-20 02:24:45 188

空空如也

空空如也

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

TA关注的人

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