自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 Flask 网站 Web服务器部署-Ubuntu18.0.4

cd /home1. 安装anaconda下载wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2019.03-Linux-x86_64.sh或者wget https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh安装...

2019-07-18 20:25:09 1046

原创 Leetcode 275. H指数 II

class Solution {public: int hIndex(vector<int>& citations) { for(int i=(int)citations.size()-1;i>=0;--i) if(citations[i]<=citations.size()-1-i) return citation...

2018-09-01 10:05:31 520

原创 Leetcode 274. H指数

题目有问题 The definition of the index is that a scholar with an index of h has published h papers each of which has been cited in other papers at least h times.class Solution {public: int hIndex(v...

2018-09-01 10:03:18 449 1

原创 Leetcode 273. 整数转换英文表示

分层考虑,先1000以内的情况,然后再把(Billion)(Million)(Thousand)拼起来class Solution {public: const int Mod[3] = { 1000000000,1000000,1000 }; string H[3] = { "Billion","Million","Thousand" }, M[8] = {...

2018-09-01 09:08:36 503

原创 Leetcode 268. 缺失数字

class Solution {public: int missingNumber(vector<int>& nums) { int L=nums.size(); long long ans=L*(1+L)>>1; for(auto &x:nums) ans-=x; ...

2018-09-01 08:33:49 236

原创 Leetcode 264. 丑数 II

class Solution {public: int nthUglyNumber(int n) { vector<int> ans(1, 1); int multi[3] = { 0,0,0 }, s[3] = { 2,3,5 };//2,3,5 while (ans.size() < n) { ...

2018-08-09 21:58:19 322

原创 Leetcode 263. 丑数

class Solution {public: bool isUgly(int num) { if(!num) return false; int s[3]={2,3,5}; for(int i=0;i<3;++i) while(!(num%s[i])) num/=s[i]; return nu...

2018-08-09 20:32:31 361

原创 Leetcode 260. 只出现一次的数字 III

设只出现一次的两个数为b,c 先全部数异或依次,得到的是a=b^c,a二进制取1的位【k】代表b c在k位1个为1一个为0 把数组分2组,一组在k位是1一组是0,这样就把b和c分在了不同组,然后两组异或和分别了b和cclass Solution {public: vector<int> singleNumber(vector<int>& nums)...

2018-08-09 20:29:02 429

原创 Leetcode 258. 各位相加

每次第k位x【xek】计算的时候变为x,减少xek-x=x(1ek-1)【减少的是9的倍数】即答案和num之间差为9的倍数【num=ans+9x】 ans的取值为1~9故ans=(num-1)%9+1class Solution {public: int addDigits(int num) { return (num-1)%9+1; }};...

2018-08-09 17:48:30 361

原创 Leetcode 257. 二叉树的所有路径

dfs深搜/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; ...

2018-08-09 17:34:33 432

原创 Leetcode 242. 有效的字母异位词

class Solution {public: bool isAnagram(string s, string t) { int cnt[26]={}; for(auto& x:s) ++cnt[x-'a']; for(auto& x:t) --cnt[x-'a']; ...

2018-08-07 14:36:58 534

原创 Leetcode 241. 为运算表达式设计优先级

假设每一个符号为最后一步,若没有符号,只需返回字符串对应的数即可 【如2*3-4*5】 (2)*(3-4*5)、(2*3)-(4*5)、(2*3-4)*5class Solution {public: int calcu(char e,int a,int b){ switch(e){ case '+': return a+b; ...

2018-08-07 14:33:29 748

原创 Leetcode 240. 搜索二维矩阵 II

搜索过程中,慢慢缩小列的范围【matrix[i][j]右下方的值都大于它】class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int n=matrix.size(),m=n?matrix[0].size():0; ...

2018-08-07 09:52:02 341

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

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

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

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

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

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

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

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

原创 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 207 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 244

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

空空如也

空空如也

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

TA关注的人

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