LeetCode560. Subarray Sum Equals K
摘要:我的方法:暴力。(结果:TLE。) 子数组的长度范围是1~n。这是最外层循环 i (0<i<length)。 对每一个长度,有一个开始,索引是 j (j+i<length)。 从 j 加到 j+i,又一个循环。 这是三重循环,我的。 class Solution { public: int suba
阅读全文
posted @
2018-06-12 20:59
苛性氢
阅读(134)
推荐(0)
leetcode 198 打家劫舍 (House Robber)
摘要:class Solution { public: int rob(vector<int>& nums) { int last,next2Last,ans; if(nums.empty()){ return 0; } if(nums.size()==1){ return nums[0]; } next
阅读全文
posted @
2018-05-21 20:55
苛性氢
阅读(348)
推荐(0)
leetcode 53 最大子序和 (Maximum Subarray)
摘要:class Solution { public: int maxSubArray(vector<int>& nums) { int pre=nums[0]; //int now; int maxSum=nums[0]; for(int i=1;i<nums.size();i++){ if(pre>0
阅读全文
posted @
2018-05-21 20:48
苛性氢
阅读(79)
推荐(0)
leetcode 121 买卖股票的最佳时机 (Best Time to Buy and Sell Stock)
摘要:class Solution { public: int maxProfit(vector<int>& prices) { if(prices.empty()){ return 0; } int minpri=prices[0]; int prof=0; for(int i=0;i<prices.s
阅读全文
posted @
2018-05-21 20:42
苛性氢
阅读(180)
推荐(0)
leetcode 70 爬楼梯 (Climbing Stairs)
摘要:最后一步走1、2个台阶。 dp[i+1]=dp[i]+dp[i-1] class Solution { public: int climbStairs(int n) { int *dp=new int[n+1]; dp[1]=1; dp[2]=2; for(int i=3;i<=n;i++){ dp
阅读全文
posted @
2018-05-21 20:40
苛性氢
阅读(156)
推荐(0)
leetcode 88 合并两个有序数组 (Merge Sorted Array)
摘要:归并。不过是从后向前。因为不能再开数组。 class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int i=n+m-1; n--; m--; for(;i>=0;i--)
阅读全文
posted @
2018-05-20 21:53
苛性氢
阅读(141)
推荐(0)
leetcode 278 第一个错误的版本 (First Bad Version)
摘要:很简单的二分查找。 弱智一开始直接复制mid=(1+n)/2 结果TLE,愣是没发现错误。 以为方法错了。 然后复制别人代码,int溢出。 // Forward declaration of isBadVersion API. bool isBadVersion(int version); clas
阅读全文
posted @
2018-05-13 22:16
苛性氢
阅读(231)
推荐(0)
leetcode 102 二叉树的层次遍历 (Binary Tree Level Order Traversal)
摘要:我的方法。每个队列保存一层的node: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v
阅读全文
posted @
2018-05-13 18:29
苛性氢
阅读(178)
推荐(0)
leetcode 108 将有序数组转换为二叉搜索树 (Convert Sorted Array to Binary Search Tree)
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), r
阅读全文
posted @
2018-05-13 16:52
苛性氢
阅读(88)
推荐(0)
leetcode 101 对称二叉树
摘要:一开始想中序遍历,然后左右判断。 但是是不对的。如果有空节点的话。 于是,空节点加数。 [1,2,3,3,null,2,null] 还是错。两个空加两个0.0,3,0,2,0,1,0,2,0,3,0 再改:只有一个空才加。(指针不能异或,转成longlong)。 [5,4,1,null,1,null
阅读全文
posted @
2018-05-11 20:50
苛性氢
阅读(114)
推荐(0)
leetcode 104 二叉树的最大深度 (Maximum Depth of Binary Tree)
摘要:别人的代码,不用vector,不过每次要比左右两个。 相当于数组中每个元素和前后比较一下。需要确定前后,i.e.,min,max。 不对。不过,递归到当前节点,不考虑下面时,确实是相当于前后。至于当前节点和下面节点的关系,递归到下面的时候会判断。 有道词典 相当于数组中每个 ... 详细X Equa
阅读全文
posted @
2018-05-08 22:03
苛性氢
阅读(129)
推荐(0)
leetcode 234 回文链表
摘要:public class Hello { public static void main(String[] args) { ListNode head=new ListNode(0); ListNode head1=new ListNode(1); ListNode head2=new ListNo
阅读全文
posted @
2018-05-07 16:11
苛性氢
阅读(143)
推荐(0)
leetcode 141 环形链表
摘要:1.我想到的用set。 2. 双指针,不使用额外空间。 一个速度为2,一个速度为1。 https://blog.csdn.net/nomasp/article/details/51598735 作者没给出证明。我的证明如下: node == n car = (2i+1) //%n man= i //
阅读全文
posted @
2018-05-04 22:56
苛性氢
阅读(108)
推荐(0)
LeetCode 21 合并两个有序链表
摘要:我的 最快的: 原来是注释里的,我改成了if。和我写的一样快。都是12ms。但是原提交却是8ms…… 我写得长,因为res->为NULL,没想到可以return preHead.next,学到了。
阅读全文
posted @
2018-04-29 16:54
苛性氢
阅读(116)
推荐(0)
leetcode 206 反转链表
摘要:不会。 递归。有点慢,18ms。 来源:https://blog.csdn.net/geekmanong/article/details/51097196 最快的。7ms?
阅读全文
posted @
2018-04-28 23:23
苛性氢
阅读(137)
推荐(0)
leetcode 8 字符串转整数
摘要:击败30%。 然后我今天又交了一次,没过。 " 0000000000012345678" 别人的代码: 来自:http://www.cnblogs.com/grandyang/p/4125537.html
阅读全文
posted @
2018-04-28 19:58
苛性氢
阅读(194)
推荐(0)
LeetCode14 最长公共前缀
摘要:我的代码:(4ms) 改正之后的代码:先找出一个最短的。 class Solution { public: string longestCommonPrefix(vector<string>& strs) { string res=""; if(strs.empty()){ return res;
阅读全文
posted @
2018-04-20 08:41
苛性氢
阅读(117)
推荐(0)
LeetCode38 报数
摘要:/* C++ string内部似乎还是char数组?末尾是'\0',str[length()]不会抛异常。 */ 我的代码: 最快代码:
阅读全文
posted @
2018-04-19 22:37
苛性氢
阅读(726)
推荐(0)