自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(210)
  • 收藏
  • 关注

原创 Leetcode Longest Substring with At Most K Distinct Characters

题意:找出最长的含k个不同字符的子串的长度。思路:windows slideclass Solution {public: int lengthOfLongestSubstringKDistinct(string s, int k) { if(k < 1) return 0; vector alpha(128, 0);

2017-05-04 01:46:07 345

原创 Leetcode Integer Replacement

题意:遇到偶数除以2,奇数 +1 或者 - 1。看多少次后能达到1。思路:BFS。class Solution {public: int integerReplacement(int n) { pair addNode; addNode.first = n, addNode.second = 0; node.push_back(ad

2017-04-08 08:59:14 350

原创 Leetcode Jump Game II

题意:给出从当前节点能跳的最大步数,求最少要跳多少步能到达终点。思路:枚举跳的步数,用BFS实现。class Solution {public: int jump(vector& nums) { if(nums.size() < 2) return 0; int step = 0, maxJump = 0, next = 0;

2017-04-03 01:28:33 353

原创 Leetcode Reconstruct Itinerary

题意:给出一个有向图,求出一条欧拉通路。思路:求欧拉通路。DFS 和后序遍历。class Solution {public: vector findItinerary(vector> tickets) { if(tickets.size() == 0) return re; for(int i = 0; i < tickets.s

2017-03-29 12:13:25 339

原创 Leetcode Count of Range Sum

题意:求连续和在区间内的和的个数。思路:分治,利用归并排序的思想,分成前后两部分。如果sumb [L] - suma[I] > upper sumb[K] - suma[I] > lower,那么L - K之间的和都满足要求。class Solution {public: int countRangeSum(vector& nums, int lower, int upper)

2017-03-28 12:29:14 245

原创 Leetcode Maximum XOR of Two Numbers in an Array

题意:给出一列数组,求出其中两个数字异或的最大值。思路:这题的思路很巧妙,枚举了每个位上的数字。又利用的异或的性质。class Solution {public: int findMaximumXOR(vector& nums) { int mask = 0; int re = 0; for(int i = 31; i >=0; -

2017-03-28 00:44:06 254

原创 Leetcode Find K Pairs with Smallest Sums

题意:给出两个排好序的数组,求出前k个和最小的数对。思路:归并排序的归并思路,每次取最小的数对。class Solution {public: vector> kSmallestPairs(vector& nums1, vector& nums2, int k) { vector> mp; for(int i = 0; i < nums1.size

2017-03-27 09:48:13 212

原创 Leetcode Maximum Product of Word Lengths

题意:给出一列单词,求出两个单词长度之积的最大值,要求这两个单词没有重复的字母。思路:关键在于如何快速判断两个单词没有相同的字母。这里用到了位运算,将单词看作是字母的集合,用位运算快速求出交并补的结果。class Solution {public: int maxProduct(vector& words) { vector w(words.size(), 0);

2017-03-27 04:47:09 254

原创 Leetcode Binary Watch

题意:手表用二进制显示时间,给出亮灯的个数,求可能的时间。思路:枚举子集的个数。这里数量较小,直接用二进制枚举。class Solution {public: vector readBinaryWatch(int num) { vector h(4, 1); for(int i = 1; i < h.size(); i ++ ) {

2017-03-12 02:42:40 207

原创 Leetcode Find Median from Data Stream

题意:找出一列数组中的中位数。思路:使用multiset,并维护一个指针,指向中位数,根据新加入的数的大小来向左向右移动指针。class MedianFinder {public: /** initialize your data structure here. */ MedianFinder() { mid = INT_MIN; cout

2017-03-11 09:35:33 183

原创 Leetcode Evaluate Division

题意:在已知一系列方程的条件下,求另一些方程的值。题目保证方程不会冲突。思路:将方程归类,有关联的归到一类。这部分用并查集实现。按一定顺序求解方程,这里用了变量名的字典序。class Solution { struct eq { string nums; string divs; double val; };public:

2017-03-11 08:19:03 255

原创 Leetcode Minimum Window Substring

题意:给出一个字符串,求出其子串中包含能组成给定字符串的最短子串。思路:先用hash map判断是否包含。枚举以每个字母结尾的子串。用一个指针指向该子串的启示位置。class Solution {public: string minWindow(string s, string t) { string temps = ""; map testa;

2017-03-07 15:11:58 211

原创 Leetcode Minimum Size Subarray Sum

题意:找出最短的连续数列,使其各项之和大于给定值。思路:先判断是否存在这样的数列。枚举以每一个元素结尾的满足条件的最短数列,枚举的时候保持一个指针,指向满足条件数列开始的位置。class Solution {public: int minSubArrayLen(int s, vector& nums) { vector p(nums.size(), -1);

2017-03-07 13:47:15 165

原创 Leetcode Surrounded Regions

题意:将被包围的O改成X。思路:按边深度搜索。class Solution {public: int h, w; void solve(vector>& board) { h = w = 0; h = board.size(); if(h) w = board[0].size(); for(int i = 0

2017-03-07 12:56:36 173

原创 Leetcode Best Time to Buy and Sell Stock with Cooldown

题意:给出每个时刻股票的价格,每天只能买入或卖出,买入或卖出后第二天不能交易。思路:DP,dp[i][j] 表示在第i天买入,第j天卖出的最大利润,转态方程为:dp[i][j] = max(dp[i][j - 1],  max(dp[i - 2][k])。class Solution {public: int maxProfit(vector& prices) {

2017-03-02 11:30:07 218

原创 Leetcode Minimum Absolute Difference in BST

题意: 找出一棵数中, 各结点之差绝对值的最小值。思路:dfs遍历,排序后,比较相邻元素的之差绝对值的大小。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(

2017-03-02 06:01:19 215

原创 Leetcode Subsets II

题意:找到一个集合所有不一样的子集。思路:暴力搜索。排序之后判断两个集合是否一样。class Solution {public: vector> subsetsWithDup(vector& nums) { int mark = pow(2.0, nums.size()); vector in; int iniin = 1;

2017-01-20 04:12:56 193

原创 Leetcode Count of Smaller Numbers After Self

题意:给出一组数字,求出在每个数字之后比这个数小的数字的个数。思路:这题用了分治的思想。将数组分成前后两部分来做,每一部分分别求出答案。在合并的时候,记录由后面的数组移动到前面的数组的数字的个数,这个数就是后面数组中比前面数组中比某个数小的数的个数。class Solution {public:struct mynode { int val; int pos;

2017-01-16 08:14:03 260

原创 Leetcode Queue Reconstruction by Height

题意:给出每个人的身高和前面高于或者等于自己身高的人的个数,重排列这个队伍。思路:先从身高较高的人入手,因为他们的限制是最小的,在他们中间插入身高较矮的人对结果没有影响。因此先将他们按身高降序排序,如果身高一样则按人数升序排序。最后一个一个插入,人数即是他们在队列中的新位置。顺序遍历插入:class Solution {public: vector> reconstructQ

2017-01-15 14:45:38 237

原创 Leetcode Move Zeroes

题意:将所有的)移动到数组最后,其他元素保持不变。思路:记录连续0开始的位置,将非零数不断与首个0交换,直到所有0都再尾部。class Solution {public: void moveZeroes(vector& nums) { for(int i = 0, j = -1; i < nums.size(); ++ i) { if(nu

2017-01-13 08:25:00 182

原创 Leetcode Decode String

题意:将字符串展开,数字表示重复的次数,方括号中表示重复的内容。思路:DFS,因为可能有嵌套,所以将方括号中的内容进行搜索。class Solution {public: string decodeString(string s) { return dfs(s); } string dfs(string s) { if(s

2017-01-13 08:06:29 298

原创 Leetcode Longest Absolute File Path

题意:给出先序遍历的所有文件路径,求出其中字符串长度最大的文件路径。思路:问题的关键是记录从根目录到文件的路径长度,这里使用了递推的方式,记录到叶子结点的路径上,每一个 结点的字符串长度。class Solution {public: int lengthLongestPath(string input) { vector level(200, 0);

2017-01-13 07:24:41 272

原创 Leetcode Merge Intervals

题意:给出一系列区间,合并有公共部分的区间。思路:先将其排序,从小到大遍历,判断有无重复部分。/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int

2017-01-11 00:54:29 238

原创 Leetcode Insert Interval

题意:给出一系列区间,再给出一个新的区间,求这些区间所组成的新的区间。思路:顺序遍历,先找出所有不重叠的区间,再处理重叠的区间。/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} *

2017-01-10 06:40:16 214

原创 Leetcode First Missing Positive

题意:找到第一个丢失的正数。思路:使用hash实现O(1)的查找。class Solution {public: int firstMissingPositive(vector& nums) { map myhash; for(int i = 0; i < nums.size(); ++ i) { if(nums[i] >

2017-01-10 04:12:34 188

原创 Leetcode Basic Calculator II

题意:设计一个计算器,实现四则运算。思路:顺序处理,先处理乘除运算,再处理加减运算。注意最后一个数字及运算的处理。class Solution {public: int calculate(string s) { stack addnumber; stack addop; string tempnum = ""; c

2017-01-09 13:19:07 180

原创 Leetcode Restore IP Addresses

题意:给出一串数字,求其所能组合成的有效的地址。思路:DFS,按位寻找有效的地址。class Solution {public: vector re; vector restoreIpAddresses(string s) { string tempre; dfs(s, 3, tempre); retur

2017-01-09 10:12:05 161

原创 Leetcode Sort List

题意:将链表排序。思路:使用归并排序。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {p

2017-01-06 14:48:11 163

原创 Leetcode Bitwise AND of Numbers Range

题意:求区间内所有数字按位与的结果。思路:对于区间中的数字,没有进位的情况下是不会出现0的,所以应当考虑需要进位的位数。高位的数字是保持的,只要考虑低位有多少进位。因此用区间的跨度计算进位的个数。class Solution {public: int rangeBitwiseAnd(int m, int n) { int re = m & n; i

2017-01-06 12:46:45 226

原创 Leetcode Binary Search Tree Iterator

题意:写一个二叉搜索树的迭代器。思路:中序遍历。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NUL

2017-01-06 12:17:29 158

原创 Leetcode Course Schedule II

题意:给定一些课程的先后顺序,输出一个可行的修课顺序,如果没有则输出空集。思路:简单拓扑排序,不断寻找入度为0的点。class Solution {public: vector findOrder(int numCourses, vector>& prerequisites) { vector indegree(numCourses, 0); ve

2017-01-06 10:30:58 217

原创 Leetcode Find Right Interval

题意:给出一系列区间,找出每个区间最近的右区间。思路:先将区间按左边界排序,然后用二分法快速查找所求区间。/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interv

2017-01-05 08:43:41 245

原创 Leetcode Sqrt(x)

题意:求x的平方根的整数部分。思路:二分查找。注意数据不要溢出。class Solution {public: int mySqrt(int x) { int low = 0; int high = x; while(low != high) { double mid = ceil(low + (high -

2017-01-05 03:24:13 162

原创 Leetcode Swap Nodes in Pairs

题意:链表两两之间交换位置。思路:简单模拟。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution

2017-01-05 02:53:29 148

原创 Leetcode Convert a Number to Hexadeci

题意:将一个数字转化成十六进制的形式。思路:简单模拟,注意负数符号位的处理。class Solution {public: string toHex(int num) { string re; if(num == 0) re += '0'; while(num) { int temp = num & 15;

2017-01-03 07:56:16 212

原创 Leetcode Number of Boomerangs

题意:求一个平面中,一个点到其他两个点的距离相等的点,这样的点一共有多少组。思路:求出所有两两点之间的距离,用hash统计距离相同的点。class Solution {public: int numberOfBoomerangs(vector>& points) { vector > distance; for(int i = 0

2017-01-03 05:43:37 280

原创 Leetcode Remove Nth Node From End of List

题意:删除链表中倒数第i个元素。思路:用堆栈完成计数工作。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class So

2017-01-02 01:57:05 187

原创 Leetcode Range Sum Query - Immutable

题意:给出一数组,求i到j之间的和。题意:记录开始位置到各位置的和,查询时,减去开头部分的和。class NumArray {public: NumArray(vector &nums) { totalSum.push_back(0); int sum = 0; for(int i = 0; i < nums.size(); ++

2017-01-02 01:26:36 174

原创 Leetcode Add Two Numbers II

题意:将两个以链表形式存储的数字向加,并以链表形式输出。思路:先按位向加,最后将链表颠倒。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} *

2017-01-01 08:43:26 162

原创 Leetcode Binary Tree Paths

题意:求出从根结点到叶结点的所有路径。思路:DFS。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL),

2017-01-01 07:32:27 170

空空如也

空空如也

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

TA关注的人

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