leetcode
文章平均质量分 64
vvickey11
一个热爱计算机,喜欢不断面对困难,迎接挑战的孩纸!↖(^ω^)↗,哈尔滨工业大学硕士在读
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
434. Number of Segments in a String
class Solution { public: int countSegments(string s) { int count = 0 ; int len = s.size(); for(int i = 0 ; i < len ; i++ ){ if(s[i] == ' '){ co原创 2017-07-10 14:46:59 · 274 阅读 · 0 评论 -
LeetCode 213:House Robber II
class Solution { public: int rob(vector& nums) { int length = nums.size(); //cout << "length = " << length << endl; int dp[10000] = {0}; int max1 = 0; int max2 = 0; if (length == 0){ r原创 2017-04-27 22:54:57 · 267 阅读 · 0 评论 -
66. Plus One
class Solution { public: vector plusOne(vector& digits) { int len = digits.size(); int plus = 0; for(int i = len - 1 ; i >=0 ;i--){ if(i == len - 1){原创 2017-05-10 10:47:47 · 206 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II
class Solution { public: int maxProfit(vector& prices) { //这道题我想的太复杂了,其实就是从头开始,只要后项大于前项就把数据加入到结果中, //多次交易的累和,只能向后计算 //贪心算法 int len = prices.size(); int res原创 2017-05-18 15:02:41 · 224 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
class Solution { public: int maxProfit(vector& prices) { int len = prices.size(); if (len == 0){ return 0; } int minpos = 0; int maxn = 0 ; int j = 0; for (int i = 0; i < len; i++){原创 2017-05-18 13:49:08 · 217 阅读 · 0 评论 -
38. Count and Say
class Solution { public: string countAndSay(int n) { if( n <= 0){ return NULL; } string dp[n]; dp[0] = "1"; string now; string newstr;原创 2017-05-09 16:12:57 · 200 阅读 · 0 评论 -
Valid Palindrome
class Solution { public: bool isPalindrome(string s) { int len = s.size(); if(len <= 1){ //记住回文数长度为1,是回文数 return true; } bool ti = false ,tj原创 2017-05-17 20:04:28 · 211 阅读 · 0 评论 -
136. Single Number
class Solution { public: int singleNumber(vector& nums) { int temp = 0; //用异域的思想,出现两次的数就是最后异域为0,只有全部异域之后剩下的就是出现次数为1的元素 for(int i = 0 ; i < nums.size() ;i++){ te原创 2017-05-17 16:42:48 · 204 阅读 · 0 评论 -
141. Linked List Cycle
点击打开链接 上面的参考的答案 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public原创 2017-05-17 16:03:42 · 215 阅读 · 0 评论 -
107. Binary Tree Level Order Traversal II
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ clas原创 2017-05-17 15:07:07 · 203 阅读 · 0 评论 -
LeetCode 69. Sqrt(x)
class Solution { public: int mySqrt(int x) { double cur = x,pre =0; while(abs(pre-cur)>0.000001){ pre = cur; cur = pre/2 + (x/(2*pre)); } r原创 2017-05-10 16:55:39 · 210 阅读 · 0 评论 -
123. Best Time to Buy and Sell Stock III
参考了: (1)点击打开链接 (2)点击打开链接 class Solution { public: int maxProfit(vector& prices) { if(prices.size() == 0){ return 0; } int g[3] = {0} , l[3] = {0}; //原创 2017-05-19 10:07:37 · 223 阅读 · 0 评论 -
188. Best Time to Buy and Sell Stock IV
参考链接: (1)点击打开链接 (2)点击打开链接 class Solution { public: int maxProfit(int k, vector& prices) { if(prices.size() == 0){ return 0; } if( k > prices.size()){原创 2017-05-19 10:56:40 · 237 阅读 · 0 评论 -
152. Maximum Product Subarray
分析法案:点击打开链接 最小的负数乘以负数是可以变成最大的乘积,因为是可以当个值成为一个字串的所以有和单个值的比较。有两个乘积记录子序列的乘积,从最大的正值和最小的负值去记录更新。 class Solution { public: int maxProduct(vector& nums) { int re; if(nums.empty()){原创 2017-06-14 15:59:19 · 269 阅读 · 0 评论 -
523. Continuous Subarray Sum
参考链接: 点击打开链接 class Solution { public: bool checkSubarraySum(vector& nums, int k) { if(nums.empty()){ return false; } //注意map用法,还有就是这个题目用的是余数 //有相同余原创 2017-07-05 22:30:47 · 286 阅读 · 0 评论 -
303. Range Sum Query - Immutable
参考答案链接: 点击打开链接 class NumArray { public: //构造函数 NumArray(vector nums) { if(nums.empty()){ return; } else{ //求出从开始的0位置到当前i位置的累加和 /原创 2017-07-05 16:17:32 · 279 阅读 · 0 评论 -
151. Reverse Words in a String
reverse讲解,size与capcity区别:点击打开链接 参考代码解释:点击打开链接 我的实现: class Solution { public: void reverseWords(string &s) { //利用reverse函数事半功倍 //string类型是可以用s[]这样来访问元素的 //storeIndex表示当前存原创 2017-06-14 10:41:16 · 320 阅读 · 0 评论 -
520. Detect Capital
class Solution { public: bool detectCapitalUse(string word) { int len = word.size(); if(len<=0){ return false; } if(len == 1){ return true;原创 2017-07-10 16:49:29 · 359 阅读 · 0 评论 -
459. Repeated Substring Pattern
class Solution { public: bool repeatedSubstringPattern(string s) { int len = s.size(); //注意这里从1开始,因为最小包含子串的字符串是2 //必须是包含等于二分之一的原因是奇数个字符的字符串 for(int i = 1; i <= len/原创 2017-07-10 16:11:33 · 333 阅读 · 0 评论 -
112. Path Sum
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ clas原创 2017-05-22 13:10:29 · 256 阅读 · 0 评论 -
119. Pascal's Triangle II
class Solution { public: vector getRow(int rowIndex) { vector res; if(rowIndex==0){ res.push_back(1); return res; } vector pre ; pre原创 2017-05-22 10:23:19 · 230 阅读 · 0 评论 -
118. Pascal's Triangle
class Solution { public: vector> generate(int numRows) { vector> res; if(numRows == 0) return res; if(numRows == 1) return {{1}}; for(int j = 0 ; j < numRows ; j+原创 2017-05-21 22:26:51 · 239 阅读 · 0 评论 -
35. Search Insert Position
class Solution { public: int searchInsert(vector& nums, int target) { int len = nums.size(); if(len == 0){ return 0; } if(target < nums[0] || target ==原创 2017-05-08 21:29:56 · 220 阅读 · 0 评论 -
28. Implement strStr()
class Solution { public: int strStr(string haystack, string needle) { int len1 = haystack.size(); int len2 = needle.size(); int i = 0 ; int index; //当两个序列的原创 2017-05-08 21:00:41 · 204 阅读 · 0 评论 -
139. Word Break
class Solution { public: bool wordBreak(string s, vector& wordDict) { unordered_set wordSet; for(auto &item : wordDict){ wordSet.insert(item); }原创 2017-05-08 16:28:22 · 315 阅读 · 0 评论 -
113. Path Sum II
参考链接: 点击打开链接 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL)原创 2017-05-23 22:30:46 · 254 阅读 · 0 评论 -
101. Symmetric Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ clas原创 2017-05-12 15:24:35 · 235 阅读 · 0 评论 -
53 Maximum Subarray
class Solution { public: int maxSubArray(vector& nums) { int length = nums.size(); int sum = 0 ; sum = nums[length-1]; int maxsum = sum ; //从后向前,因为原创 2017-05-04 14:16:53 · 211 阅读 · 0 评论 -
LeetCode 95. Unique Binary Search Trees II
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2017-05-04 11:08:52 · 205 阅读 · 0 评论 -
100. Same Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ clas原创 2017-05-12 13:59:05 · 197 阅读 · 0 评论 -
Merge Sorted Array
class Solution { public: void merge(vector& nums1, int m, vector& nums2, int n) { vector res; int i , j , k; if(nums2.size() == 0) return; for(i = 0 , j = 0 , k = 0原创 2017-05-12 10:41:49 · 194 阅读 · 0 评论 -
LeetCode twosum C++ map实现
class Solution { public: vector twoSum(vector& nums, int target) { map my_Map; vector result; for(int i = 0 ; i < nums.size() ; i++) { //保证每次插入的元素是在my_Map里是唯一的原创 2017-03-12 11:00:21 · 1020 阅读 · 0 评论 -
Reverse Integer Leetcode 每日一题 ↖(^ω^)↗
#include #include #include #define INT_MAX 0x7fffffff using namespace std; class Solution { public: int reverse(int x) { //主要的是考慮溢出,一開始想用堆棧做但是是不對的,得先把各个数位分开啊!这个是关键 //接着,得考虑变换后是否会超越边界原创 2016-10-14 14:32:28 · 756 阅读 · 0 评论 -
Leetcode Parlindrome Number 9
//解题思路,回文数就是1234321 //分别提取最高位和最低位的数进行比较 #include using namespace std; ////这个处理不了溢出 //class Solution{ //public: // bool isParlindrome(int x){ // int nTen = 1; // int high = 0; //原创 2016-10-25 14:43:04 · 442 阅读 · 0 评论 -
62. Unique Paths
class Solution { public: int uniquePaths(int m, int n) { //自底向上,从最后一个节点的方案数向回计算 int a[1000][1000]; if(n ==1 || m==1){ return 1; } a[m][n] = 1;原创 2017-05-04 16:26:46 · 182 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
递归解法: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2017-05-12 21:25:09 · 251 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
参考链接: 点击打开链接 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL)原创 2017-05-24 10:41:45 · 256 阅读 · 0 评论 -
221. Maximal Square
class Solution { public: int maximalSquare(vector>& matrix) { int rows = matrix.size(); if(rows == 0) return 0; int cols = matrix[0].size(); vector > dp原创 2017-05-07 22:45:58 · 213 阅读 · 0 评论 -
Distinct Subsequences
class Solution { public: int numDistinct(string s, string t) { int rows = s.size(); int cols = t.size(); int dp[rows + 1][cols + 1] = {0}; //当T为空,T的子串在S中出现的次数是1,因为空原创 2017-05-07 09:06:35 · 225 阅读 · 0 评论 -
LeetCode Triangle
class Solution { public: int minimumTotal(vector>& triangle) { int rows = triangle.size(); int res; if (rows<1){ return 0; } if (rows == 1){ return triangle[0][0]; } int dp[1000][原创 2017-05-06 22:50:37 · 188 阅读 · 0 评论
分享