
leetcode
奈何辰星无可奈
github: hackerchenzhuo
展开
-
leetcode 2. 两数相加
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode*...原创 2019-04-23 15:26:08 · 140 阅读 · 0 评论 -
leetcode 11.盛水最多的容器 c++
方法1:暴力法。从前往后,每次检索 最长的区域*二者间最矮的高度。时间复杂度O(n^2)class Solution {public: int maxArea(vector<int>& height) { if(height.size()<=1) return 0; else{ int siz...原创 2019-05-01 10:39:45 · 287 阅读 · 0 评论 -
leetcode 15三数之和
最开始的想法很简单,双指针遍历,查询,时间复杂度O(n^3),过不了最后几个案例。class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> kong; v...原创 2019-05-01 14:26:23 · 170 阅读 · 0 评论 -
leetcode 121. 买卖股票的最佳时机
暴力法,时间O(n^2):执行用时:1220 msclass Solution {public: int maxProfit(vector<int>& prices) { int sz=prices.size(); int max=0;int t; for(int i=0;i<sz;i++) ...原创 2019-05-14 13:35:52 · 139 阅读 · 0 评论 -
leetcode 283. 移动零
Runtime:12 ms, faster than99.76%ofC++online submissions forMove Zeroes.Memory Usage:9.5 MB, less than59.39%ofC++online submissions forMove Zeroes.相当于一次遍历,O(n)时间复杂度。没有用额外空间。 /...原创 2019-05-19 21:02:25 · 147 阅读 · 0 评论 -
leetcode 53. 最大子序和
不来花里胡哨的分治,做出来才是王道解法一:(best)class Solution {public: int maxSubArray(vector<int>& nums) { int sum = nums[0]; int maxSum = sum; for(int i=1; i<nums.size(); ...原创 2019-05-07 13:36:01 · 92 阅读 · 0 评论 -
leetcode 58. 最后一个单词的长度
class Solution {public: int lengthOfLastWord(string s) { stringstream ss; ss<<s; string t=""; ss>>t; if(t==""||t==" ") return 0; strin...原创 2019-05-07 13:41:41 · 123 阅读 · 0 评论 -
leetcode 437. 路径总和 III
借用的他人的代码:不要想得过于复杂。把中间步骤分解,easy的。两种情况:1.包括当前结点2.不包括当前结点然后dfs一下。执行用时 :28 ms, 在Path Sum III的C++提交中击败了96.77%的用户内存消耗 :14.6 MB, 在Path Sum III的C++提交中击败了80.08%的用户class Solution {pu...原创 2019-05-19 21:59:04 · 166 阅读 · 0 评论 -
leetcode 70.爬楼梯
不要写专门的递归函数,没必要。我最开始的代码:class Solution {public: map<int,int> m; int climbStairs(int n) { return pa(n); } int pa(int x){ if(x<0) return 0; else if(...原创 2019-05-10 09:49:39 · 274 阅读 · 0 评论 -
二进制转换【转】
对于二进制一直不是很有感觉,看到了一篇比较好的博文,将二进制输出的算法总结的差不多了,一起学习一下,原文网址https://blog.youkuaiyun.com/sodacoco/article/details/81624915看《编程之美》第二节的时候,它是定义的一个整型,然后取位。但是他的那个或运算符号好像写错了,写成了异或符号“^”,应该是“|”。我就突然对二进制的输出感兴趣了。想知道怎样输出...转载 2019-05-24 13:19:49 · 345 阅读 · 0 评论 -
leetcode 190. 颠倒二进制位【二进制处理】
方法一:位运算执行用时 :0 ms, 在Reverse Bits的C++提交中击败了100.00%的用户内存消耗 :8.1 MB, 在Reverse Bits的C++提交中击败了29.04%的用户class Solution {public: uint32_t reverseBits(uint32_t n) { int i=32; uint32_t ...原创 2019-05-24 13:28:02 · 182 阅读 · 0 评论 -
leetcode 191. 位1的个数
解法一:bitset执行用时 :8 ms, 在Number of 1 Bits的C++提交中击败了93.71%的用户内存消耗 :8.2 MB, 在Number of 1 Bits的C++提交中击败了14.97%的用户class Solution {public: int hammingWeight(uint32_t n) { bitset<...原创 2019-05-24 13:32:41 · 168 阅读 · 0 评论 -
leetcode 202. 快乐数【循环相关!快慢指针】
一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。输入: 19输出: true解释: 12 + 92 = 8282 + 22 = 6862 + 82 = 10012 + 02 + 02 = 1题解:解法一:先验!不...原创 2019-05-24 13:55:31 · 294 阅读 · 0 评论 -
全排列【转】
在leetcode上刷题的时候,偶然看到一位仁兄总结的关于寻找数组的子集(78,90)、全排列(46,47)、在数组中找出等于固定值的元素的集合(39,40)、找出字符串回文子串的集合(131),感觉很惊喜,所以搬运到这里分享给大家,下边是原文链接,里面也有很多讨论。https://discuss.leetcode.com/topic/46161/a-general-approach-to-bac...转载 2019-05-20 22:37:22 · 143 阅读 · 0 评论 -
leetcode 438. 找到字符串中所有字母异位词【傻子才用全排列(比如我)】
remember:时间使用增加原因:1.函数传入的参数比较复杂,每一次的拷贝很耗时间2.能用下标表示的用下标。尽量抽象参考leetcode 242. 有效的字母异位词【哈希表】最开始的想法: //用map //在全排列中检索。 //因为有红黑树,所以时间复杂度O(2lgn+n),还好 //visit数组记录是否访问过,level定义当前到哪了...原创 2019-05-21 00:35:49 · 204 阅读 · 0 评论 -
leetcode 141. 环形链表【快慢指针】
刚开始都没看清题目里面的pos啥意思。。。执行用时 :8 ms, 在Linked List Cycle的C++提交中击败了100.00%的用户内存消耗 :9.9 MB, 在Linked List Cycle的C++提交中击败了12.12%的用户/** * Definition for singly-linked list. * struct ListNode { * ...原创 2019-05-15 22:52:32 · 128 阅读 · 0 评论 -
leetcode 155. 最小栈
class MinStack {public: /** initialize your data structure here. */ //双栈。顺序天然相同 stack<int> s; stack<int>min; MinStack() { } void push(int x)...原创 2019-05-15 23:24:51 · 109 阅读 · 0 评论 -
leetcode 448. 找到所有数组中消失的数字【要求空间O(1)】
方法一:空间换时间(easy)执行用时 :132 ms, 在Find All Numbers Disappeared in an Array的C++提交中击败了96.39%的用户内存消耗 :16.1 MB, 在Find All Numbers Disappeared in an Array的C++提交中击败了6.77%的用户class Solution {public:...原创 2019-05-21 13:07:51 · 229 阅读 · 0 评论 -
leetcode 88. 合并两个有序数组
时间复杂度O(2n)。分别是比较:n,一一赋值:nclass Solution { //19.51-public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { vector<int>tmp(m+n); ...原创 2019-05-11 20:11:57 · 417 阅读 · 0 评论 -
leetcode 101. 对称二叉树【递归】
class Solution {public: bool isSymmetric(TreeNode* root) { if(!root) return true; return same(root->left,root->right); } bool same(TreeNode*l,TreeNode*r) {...原创 2019-05-11 20:58:50 · 206 阅读 · 0 评论 -
leetcode 160. 相交链表
easy,知识迁移即可。双指针。找规律。若有相交后半截必定重合。执行用时 :68 ms, 在Intersection of Two Linked Lists的C++提交中击败了95.19%的用户内存消耗 :16.6 MB, 在Intersection of Two Linked Lists的C++提交中击败了64.61%的用户/** * Definition for ...原创 2019-05-16 13:22:23 · 155 阅读 · 0 评论 -
leetcode 169. 求众数【四种方法】【摩尔投票法】
前三种方法://两次遍历,用map存:O(2n)//先排序,再一次遍历O(nlgn + n)//三次遍历,用空间换时间的数组存,O(3n)方法一:先排序执行用时 :40 ms, 在Majority Element的C++提交中击败了55.89%的用户内存消耗 :11.1 MB, 在Majority Element的C++提交中击败了76.48%的用户c...原创 2019-05-16 13:50:54 · 623 阅读 · 0 评论 -
leetcode 198. 打家劫舍
一开始肯定是想用递归做:56 / 69个通过测试用例超时不可避免。class Solution {public: int rob(vector<int>& nums) { int sz=nums.size(); if(sz==0)return 0; return get(nums,sz-1)...原创 2019-05-16 14:18:50 · 122 阅读 · 0 评论 -
leetcode 107. 二叉树的层次遍历 II【队列】
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla...原创 2019-05-11 21:29:02 · 135 阅读 · 0 评论 -
leetcode 108. 将有序数组转换为二叉搜索树
在递归函数中建立新树。一次建立一棵树,而不是两棵。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), ...原创 2019-05-12 13:41:22 · 187 阅读 · 0 评论 -
leetcode 110. 平衡二叉树
无脑写法:双重递归——一个找深度,一个判平衡:没有重复利用深度,深度进行了重复测量,浪费了时间:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x...原创 2019-05-12 13:57:02 · 120 阅读 · 0 评论 -
leetcode 111. 二叉树的最小深度
记住:最小深度和最大深度方法不同。最小深度要小心非叶子结点的干扰。执行用时 :28 ms, 在Minimum Depth of Binary Tree的C++提交中击败了95.85%的用户内存消耗 :20.1 MB, 在Minimum Depth of Binary Tree的C++提交中击败了7.68%的用户/** * Definition for a binary t...原创 2019-05-12 14:20:51 · 145 阅读 · 0 评论 -
leetcode 461. 汉明距离【二进制计算】
执行用时 :0 ms, 在Hamming Distance的C++提交中击败了100.00%的用户内存消耗 :8.3 MB, 在Hamming Distance的C++提交中击败了73.26%的用户class Solution {public: int hammingDistance(int x, int y) { int t=x^y; ...原创 2019-05-22 14:50:39 · 218 阅读 · 0 评论 -
leetcode 538. 把二叉搜索树转换为累加树【中序遍历的逆】
执行用时 :44 ms, 在Convert BST to Greater Tree的C++提交中击败了97.88%的用户内存消耗 :23.5 MB, 在Convert BST to Greater Tree的C++提交中击败了85.57%的用户原创 2019-05-22 15:33:37 · 247 阅读 · 0 评论 -
leetcode 543. 二叉树的直径
方法一:(无脑DFS)n次递归(n是深度)注意。不一定最大宽度每次都经过根节点执行用时 :108 ms, 在Diameter of Binary Tree的C++提交中击败了5.26%的用户内存消耗 :35.8 MB, 在Diameter of Binary Tree的C++提交中击败了5.39%的用户/** * Definition for a binary tree...原创 2019-05-23 11:13:55 · 207 阅读 · 0 评论 -
leetcode 206. 反转链表
执行用时 :16 ms, 在Reverse Linked List的C++提交中击败了96.25%的用户内存消耗 :9.1 MB, 在Reverse Linked List的C++提交中击败了53.22%的用户前,中,后指针,一次遍历反转列表。/** * Definition for singly-linked list. * struct ListNode ...原创 2019-05-18 11:18:39 · 150 阅读 · 0 评论 -
leetcode 226. 翻转二叉树
递归求解:执行用时 :4 ms, 在Invert Binary Tree的C++提交中击败了96.52%的用户内存消耗 :9.4 MB, 在Invert Binary Tree的C++提交中击败了5.20%的用户我的方法:/** * Definition for a binary tree node. * struct TreeNode { * int va...原创 2019-05-18 11:34:52 · 131 阅读 · 0 评论 -
leetcode 234. 回文链表【最佳解】【c++】
回文链表最佳解:【结合了反转链表的最佳方式】 //快慢指针找中点 //反转后半列表 //依次往前遍历执行用时 :28 ms, 在Palindrome Linked List的C++提交中击败了95.77%的用户内存消耗 :12.5 MB, 在Palindrome Linked List的C++提交中击败了77.91%的用户时间复杂度O(n)——————(很重...原创 2019-05-18 12:07:19 · 247 阅读 · 0 评论 -
leetcode 581. 最短无序连续子数组【一次遍历最佳方法】
执行用时 :36 ms, 在Shortest Unsorted Continuous Subarray的C++提交中击败了97.79%的用户内存消耗 :10.4 MB, 在Shortest Unsorted Continuous Subarray的C++提交中击败了91.37%的用户原创 2019-05-23 12:35:37 · 337 阅读 · 0 评论 -
leetcode 617.合并二叉树【递归连接二叉树】
执行用时 :40 ms, 在Merge Two Binary Trees的C++提交中击败了99.62%的用户内存消耗 :13.8 MB, 在Merge Two Binary Trees的C++提交中击败了84.32%的用户递归使用二叉树,进行连接。千万不要使用new!!!!千万不要单独或者多余地判断!!!/** * Definition for a binary ...原创 2019-05-23 12:57:54 · 154 阅读 · 0 评论 -
leetcode 118. 杨辉三角【让每一次循环变得有意义】
执行用时 :4 ms, 在Pascal's Triangle的C++提交中击败了98.60%的用户内存消耗 :8.9 MB, 在Pascal's Triangle的C++提交中击败了8.35%的用户class Solution {public: vector<vector<int>> generate(int numRows) { ...原创 2019-05-23 13:12:19 · 369 阅读 · 0 评论 -
leetcode 171. Excel表列序号
执行用时 :4 ms, 在Excel Sheet Column Number的C++提交中击败了99.20%的用户内存消耗 :8.3 MB, 在Excel Sheet Column Number的C++提交中击败了20.26%的用户class Solution {public: int titleToNumber(string s) { int sz=s....原创 2019-05-23 21:48:06 · 128 阅读 · 0 评论 -
leetcode 125. 验证回文串
执行用时 :8 ms, 在Valid Palindrome的C++提交中击败了99.47%的用户内存消耗 :9.6 MB, 在Valid Palindrome的C++提交中击败了8.01%的用户两遍遍历:class Solution {public: bool isPalindrome(string s) { int cha='a'-'A'; ...原创 2019-05-23 22:00:46 · 132 阅读 · 0 评论 -
leetcode 172. 阶乘后的零[【认真你就输了】
执行用时 :4 ms, 在Factorial Trailing Zeroes的C++提交中击败了98.02%的用户内存消耗 :8.2 MB, 在Factorial Trailing Zeroes的C++提交中击败了76.37%的用户解题:5的数量少于2,4,6, 8。所以5是基准。千万不要乘,不要急,多想想。第一印象往往是不准的class Solution ...原创 2019-05-23 22:43:21 · 230 阅读 · 0 评论 -
leetcode 189. 旋转数组【经典问题,两次逆序】
执行用时 :24 ms, 在Rotate Array的C++提交中击败了97.74%的用户内存消耗 :9.5 MB, 在Rotate Array的C++提交中击败了52.70%的用户经典问题class Solution {public: void rotate(vector<int>& nums, int k) { //如此经典的...原创 2019-05-23 23:17:26 · 166 阅读 · 0 评论