自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 Leetcode 239. 滑动窗口最大值

考察单调队列【On复杂度求固定大小窗口的最大值最小值】class Solution {public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { deque<int> que; vector<int> ans; ...

2018-07-30 15:02:41 580

原创 Leetcode 238. 除自身以外数组的乘积

出于对空间复杂度分析的目的,输出数组不被视为额外空间。 这个算一个提示class Solution {public: vector<int> productExceptSelf(vector<int>& nums) { vector<int> ans; int t=1; for(int i=...

2018-07-30 11:05:43 713

原创 Leetcode 237. 删除链表中的节点

很巧妙,是用node->next的值赋值给node,然后删除node->next/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * ...

2018-07-30 10:58:38 275

原创 Leetcode 236. 二叉树的最近公共祖先

1.p q不互为子孙,一个在fa的左,一个在fa的右 2.p q互为子孙,p是q的孙或q是p的孙/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) ...

2018-07-30 10:49:37 616

原创 Leetcode 235. 二叉搜索树的最近公共祖先

最近祖先节点特点,大于小的那个,小于大的那个/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right...

2018-07-29 13:10:11 335

原创 Leetcode 234. 回文链表

反转前半部分链表,然后依次比较/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:...

2018-07-29 13:05:49 309

原创 Leetcode 233. 数字1的个数

统计每一位上1的个数class Solution {public: int countDigitOne(int n) { if(n<=0) return 0; int ans=0,k=1; string s="0"+to_string(n); for(int i=2;i<s.size();++i) k*=10...

2018-07-29 12:40:28 702

原创 Leetcode 232. 用栈实现队列

0号栈是队列的正序,1号栈是队列的倒序【只有其中一个在使用,用到另一个时需要转换】class MyQueue {public: /** Initialize your data structure here. */ stack<int> sta[2]; int f; MyQueue() { f=0; } /** P...

2018-07-29 12:09:21 377

原创 Leetcode 231. 2的幂

n二进制是0.010.0的形式【排除100..】class Solution {public: bool isPowerOfTwo(int n) { return n>0 && n==(n&-n); }};

2018-07-29 12:04:06 220 2

原创 Leetcode 230. 二叉搜索树中第K小的元素

记录每个节点为跟的树的节点个数,然后进行查找,若删除,添加只需要Oh的复杂度来更改节点个数/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : va...

2018-07-29 12:01:03 325

原创 Leetcode 229. 求众数 II

摩尔投票法class Solution {public: vector<int> majorityElement(vector<int>& nums) { int a=0,b=0,ca=0,cb=0; vector<int> ans; for(auto& x:nums){ ...

2018-07-29 11:06:22 455

原创 Leetcode 228. 汇总区间

class Solution {public: vector<string> summaryRanges(vector<int>& nums) { vector<string> ans; if(nums.empty()) return ans; int val=nums[0]; f...

2018-07-29 10:11:49 453

原创 Leetcode 227. 基本计算器 II

遇到+-,要把前面的运算都完成 遇到乘除,若之前的运算符是乘除的话,则运算乘除,否则不运算 【若乘除连着几个,后一个乘除都会先计算前一个乘除】class Solution {public: int get_new_num(int a,int op,int b){ switch(op){ case 1: return a+b; ...

2018-07-29 09:42:18 418

原创 Leetcode 226. 翻转二叉树

先左、右子树翻转下,然后交换位置,递归/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NU...

2018-07-28 08:59:44 192

原创 Leetcode 225. 用队列实现栈

通过判断队列是否为空来找到栈顶class MyStack {public: /** Initialize your data structure here. */ queue<int> que[2]; int f; MyStack() { f = 0; } /** Push element x onto stack...

2018-07-27 17:49:23 287

原创 Leetcode 224. 基本计算器

一个全局符号flag,遇到左括号,若括号前面为+flag不变,-flag取反 pre代表上一个符号,若遇到括号,则进行重置【初始为1即为+】重置后也是+ 遇到一个数,答案+flag*pre*val,注意有空格的情况,直接跳过即可class Solution {public: int calculate(string s) { int val = 0, ans = ...

2018-07-27 14:33:31 600

原创 Leetcode 223. 矩形面积

求重叠部分的面积【若没有则为0】 答案为两面积相加-重叠部分面积class Solution {public: int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int a=max(A,E),b=max(B,F),c=min(C,G),d=min(D,H); ...

2018-07-27 13:28:41 353

原创 Leetcode 222. 完全二叉树的节点个数

对答案二分法进行求解/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} *...

2018-07-27 13:12:57 259

原创 Leetcode 221. 最大正方形

动态规划,dp[i][j]=min(dp[i-1][j-1],L[i-1][j],up[i][j-1])+1;【如果不存在则dp,L,up为0】 dp:此位置最大正方形边长,L此位置向左延申1的最长长度,up此位置向上延申1的最长长度class Solution {public: int maximalSquare(vector<vector<char>>&...

2018-07-27 12:34:35 262

原创 Leetcode 220. 存在重复元素 III

坑点1:考虑t+1和x-mm可能超了int的最大范围,顾都用long long类型 坑点2:t可能是负数class Solution {public: bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) { map<long long,long long&g...

2018-07-25 21:44:13 827

原创 Leetcode 219. 存在重复元素 II

class Solution {public: set<int> A; bool containsNearbyDuplicate(vector<int>& nums, int k) { int i=0; k+=1; k=min(k,(int)nums.size()); for(;i&l...

2018-07-25 17:55:18 254

原创 Leetcode 218. 天际线问题

把图分成一个个区间【端点为输入中出现过的端点】,求这些区间的高度【成为一个个矩形】,如果右区间高度不等于左区间,那么右区间的左端点即关键点class Solution {public:vector<pair<int, int>> ans,all;set<int> vis;vector<pair<int, int>> getSk...

2018-07-24 16:26:30 492

原创 Leetcode 217. 存在重复元素

class Solution {public: set<int> A; bool containsDuplicate(vector<int>& nums) { for(auto &x:nums) if(!A.count(x)) A.insert(x); else return ...

2018-07-24 11:41:21 197

原创 Leetcode 216. 组合总和 III

暴力枚举配合剪枝class Solution {public: vector<vector<int>> ans; vector<int> tmp; void dfs(int k,int n){ int i= tmp.empty()? 0:tmp.back(); ++i; if(i+k-...

2018-07-24 11:40:03 432

原创 Leetcode 215. 数组中的第K个最大元素

快速排序的思想 ON平均时间复杂度class Solution {public: int quicksort(vector<int>& nums, int k,int l,int r){ int ll=l,rr=r,val=nums[l]; while(ll<rr){ while(ll<rr &a...

2018-07-24 11:08:58 365

原创 Leetcode 214. 最短回文串

用了Manacher算法,On复杂度class Solution {public: string shortestPalindrome(string s) { reverse(s.begin(),s.end()); string s2="$#"; for(auto& x:s) s2 += x ,s2+='...

2018-07-23 09:04:58 360

原创 Leetcode 213. 打家劫舍 II

class Solution {public: int rob(vector<int>& nums) { if(nums.empty()) return 0; int tou=0,butou=0,t,ans1,ans2; for(int i=1;i<nums.size();++i)//1号不偷的情况,2号至最后一号...

2018-07-22 14:06:56 384

原创 Leetcode 211. 添加与搜索单词 - 数据结构设计

借用Trie树,addword就是insert,搜索的时候因为有’.’的存在,用递归dfs的思路#define cti(x) (x-'a')#define itc(x) (x+'a')const int maxn = 70005;const int sigma_size = 26;class WordDictionary {public: /** Initialize you...

2018-07-22 13:56:59 494

原创 Leetcode 212. 单词搜索 II

用Trie树方式实现#define cti(x) (x-'a')#define itc(x) (x+'a')const int maxn = 70005;const int sigma_size = 26;class Solution {public: int ch[maxn][sigma_size], val[maxn] = {}, sz = 0; void _in...

2018-07-22 13:44:45 284

原创 Leetcode 210. 课程表 II

和Leetcode 207. 课程表一样,拓扑排序,上一个记录拓扑排序过程中的数量,这一个记录顺序class Solution {public: vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) { vector&

2018-07-22 09:43:18 395

原创 Leetcode 209. 长度最小的子数组

On的解法,当队列大于等于s的时候,剔除队首元素,直到小于s,这样以队尾元素为最后一个元素的子数组的大于等于s的最大长度为队列size+1 O nlgn解法就是用二分法去搜寻答案,每次验证是否满足需要On的时间,故为O nlgnclass Solution {public: int minSubArrayLen(int s, vector<int>& nums)...

2018-07-22 09:38:34 584

原创 Leetcode 208. 实现 Trie (前缀树)

#define cti(x) (x-'a')#define itc(x) (x+'a')const int maxn = 100005;const int sigma_size = 26;class Trie {public: /** Initialize your data structure here. */ int ch[maxn][sigma_size], val...

2018-07-22 09:35:45 261

原创 Leetcode 207. 课程表

拓扑排序class Solution {public: bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) { vector<vector<int>> A(numCourses,vector<int>()); ...

2018-07-22 09:34:24 258

原创 Leetcode 206. 反转链表

迭代版本/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNo...

2018-07-22 09:31:26 135

原创 Leetcode 205. 同构字符串

对字符重新进行编号,再进行比较是否相等class Solution {public: bool isIsomorphic(string s, string t) { map<char,int> A; string ss,tt; for(auto x:s) if(A.count(x)) ss+=(cha...

2018-07-22 09:30:20 193

原创 Leetcode 204. 计数质数

筛法求素数class Solution {public: int countPrimes(int n) { int ans=0; vector<int> A(n,1); for(int i=2;i<n;++i) if(A[i]){ ++ans; ...

2018-07-22 09:28:19 184

原创 Leetcode 203. 删除链表中的节点

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* re...

2018-07-21 15:49:45 200

原创 Leetcode 202. 快乐数

class Solution {public: set<int> A; bool isHappy(int n) { while(n!=1){ A.insert(n); int k=0; while(n) k+=(n%10)*(n%10),n/=10;...

2018-07-21 15:44:07 185

原创 Leetcode 201. 数字范围按位与

class Solution {public: int rangeBitwiseAnd(int m, int n) { int t=m&n,ans=0; n-=m; while(t){ int k=t&-t; if(k>=n) ans+=k; t-...

2018-07-21 15:38:55 578

原创 Leetcode 200. 岛屿的个数

求连通量的个数class Solution {public: int n,m; int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; bool _place(int x,int y){ return x>=0 && x<n && y>=0 &&y<m; ...

2018-07-21 15:30:58 309

空空如也

空空如也

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

TA关注的人

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