leetcode
no_pain_no_gain
这个作者很懒,什么都没留下…
展开
-
leetcode--1. Two Sum
题目:https://leetcode.com/problems/two-sum/#/descriptionclass Solution {public: vector twoSum(vector& nums, int target) { map s; vector res; for(int i=0;i<nums.size();i++)原创 2017-04-18 15:41:39 · 216 阅读 · 0 评论 -
leetcode -- 12. Integer to Roman
题目:https://leetcode.com/problems/integer-to-roman/#/description代码:class Solution {public: string intToRoman(int num) { string temp; string str[]={"M", "CM", "D", "CD", "C", "X原创 2017-07-25 14:25:19 · 178 阅读 · 0 评论 -
leetcode--27. Remove Element
题目:https://leetcode.com/problems/remove-element/#/descriptionclass Solution {public: int removeElement(vector& nums, int val) { int len=nums.size(),num=0; for(int i=0;i<len;i++)原创 2017-04-24 11:12:19 · 208 阅读 · 0 评论 -
leetcode --24. Swap Nodes in Pairs
题目:https://leetcode.com/problems/swap-nodes-in-pairs/description/代码:class Solution {public: ListNode* swapPairs(ListNode* head) { if(head == NULL) return NULL; L原创 2017-08-16 19:47:51 · 251 阅读 · 0 评论 -
leetcode --22. Generate Parentheses
题目:https://leetcode.com/problems/generate-parentheses/description/代码:class Solution {public: vector generateParenthesis(int n) { vector res; string t; trans(t,res,0,0,原创 2017-08-16 20:07:02 · 258 阅读 · 0 评论 -
leetcode --8. String to Integer (atoi)
题目:https://leetcode.com/problems/string-to-integer-atoi/description/代码:class Solution {public: int myAtoi(string str) { if(str == "") return 0; int pos = 0,temp=1; doubl原创 2017-08-10 19:54:19 · 197 阅读 · 0 评论 -
leetcode--11. Container With Most Water
题目:https://leetcode.com/problems/container-with-most-water/description/代码:class Solution {public: int maxArea(vector& height) { int mx= 0,i=0,j=height.size()-1; while(i<j){原创 2017-08-10 20:46:32 · 201 阅读 · 0 评论 -
leetcode --3. Longest Substring Without Repeating Characters
题目:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/代码:class Solution {public: int lengthOfLongestSubstring(string s) { int maxlen =0,pos=-1;原创 2017-08-04 15:27:49 · 164 阅读 · 0 评论 -
leetcode --28. Implement strStr()
题目:https://leetcode.com/problems/implement-strstr/tabs/description/代码:class Solution {public: int strStr(string a, string b) { int i,j,len1=a.size(),len2=b.size(); if(!len2) retur原创 2017-07-27 20:05:38 · 190 阅读 · 0 评论 -
leetcode-- 35. Search Insert Position
传送门int searchInsert(int* nums, int numsSize, int target) { int i=0; while(target>nums[i]&&i<numsSize){ i++; } return i;}原创 2017-04-18 15:22:08 · 222 阅读 · 0 评论 -
leetcode --38. Count and Say
题目:https://leetcode.com/problems/count-and-say/tabs/description代码:class Solution {public: string countAndSay(int n) { string temp="1",res; int count; if(n==0) return "";原创 2017-07-28 09:29:30 · 239 阅读 · 0 评论 -
leetcode --5. Longest Palindromic Substring
题目:https://leetcode.com/problems/longest-palindromic-substring/description/代码:class Solution {public: string longestPalindrome(string s) { if(s.length()<=1) return s; bool dp[s.原创 2017-08-05 16:40:59 · 197 阅读 · 0 评论 -
leetcode --58. Length of Last Word
题目:https://leetcode.com/problems/length-of-last-word/tabs/description代码:class Solution {public: int lengthOfLastWord(string s) { if(s=="") return 0; int len=s.size(),i=len-1,cou原创 2017-07-28 16:43:46 · 231 阅读 · 0 评论 -
leetcode --70. Climbing Stairs
题目:https://leetcode.com/problems/climbing-stairs/tabs/description代码:class Solution {public: int climbStairs(int n) { int a[100]; a[0]=1,a[1]=2; for(int i=2;i<100;i++){原创 2017-07-29 10:05:02 · 238 阅读 · 0 评论 -
leetcode --19. Remove Nth Node From End of List
题目:https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/代码:class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode *pre=head,*q=pre;原创 2017-08-14 15:50:21 · 150 阅读 · 0 评论 -
leetcode --15. 3Sum
题目:https://leetcode.com/problems/3sum/description/代码:class Solution {public: vector> threeSum(vector& nums) { vector> res; sort(nums.begin(), nums.end()); for (int k = 0转载 2017-08-15 08:49:54 · 167 阅读 · 0 评论 -
leetcode --18. 4Sum
题目:https://leetcode.com/problems/4sum/description/代码:class Solution {public: vector> fourSum(vector& nums, int target) { vector > res; if(nums.size()==0) return res; sor原创 2017-08-15 19:17:09 · 181 阅读 · 0 评论 -
leetcode --16. 3Sum Closest
题目:https://leetcode.com/problems/3sum-closest/description/代码:class Solution {public: int threeSumClosest(vector& nums, int target) { if(nums.size() < 3) return 0; sort(nums.begi原创 2017-08-15 19:39:39 · 232 阅读 · 0 评论 -
leetcode --26. Remove Duplicates from Sorted Array
题目:https://leetcode.com/problems/remove-duplicates-from-sorted-array代码:class Solution {public: int removeDuplicates(vector& nums) { int i,count =0; if(!nums.size()) return 0;原创 2017-07-24 19:41:54 · 169 阅读 · 0 评论 -
leetcode--2. Add Two Numbers
题目:https://leetcode.com/problems/add-two-numbers/description/代码:class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode head(0), *res = &head; int原创 2017-08-02 10:02:37 · 214 阅读 · 0 评论 -
leetcode--119. Pascal's Triangle II
地址:https://leetcode.com/problems/pascals-triangle-ii/#/descriptionclass Solution {public: vector getRow(int rowIndex) { vector res(rowIndex+1); res[0]=1; for (int i = 0原创 2017-04-19 11:17:22 · 173 阅读 · 0 评论 -
leetcode--268. Missing Number
地址:https://leetcode.com/problems/missing-number/#/description暴力低效率:class Solution {public: int missingNumber(vector& nums) { int i; maps; for(i=0;i<nums.size();i++){原创 2017-04-19 11:32:57 · 189 阅读 · 0 评论 -
leetcode--485. Max Consecutive Ones
地址:https://leetcode.com/problems/max-consecutive-ones/#/descriptionclass Solution {public: int findMaxConsecutiveOnes(vector& nums) { int t=1,max=0,sum=0; for(int i=0;i<nums.siz原创 2017-04-19 11:44:57 · 192 阅读 · 0 评论 -
leetcode--121. Best Time to Buy and Sell Stock
地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/descriptionclass Solution {public: int maxProfit(vector& prices) { if (prices.size() == 0) { ret原创 2017-04-19 12:48:24 · 270 阅读 · 0 评论 -
leetcode--66. Plus One
题目:https://leetcode.com/problems/plus-one/#/descriptionclass Solution {public: vector plusOne(vector& digits) { int t = 1; for(int i = digits.size() - 1; i >= 0; -- i)原创 2017-04-21 20:30:32 · 220 阅读 · 0 评论 -
leetcode--53. Maximum Subarray
题目:https://leetcode.com/problems/maximum-subarray/#/descriptionclass Solution {public: int maxSubArray(vector& nums) { int sum=nums[0],max_sum=nums[0]; for(int i=1;i<nums.size()原创 2017-04-24 12:13:27 · 196 阅读 · 0 评论 -
leetcode --67. Add Binary
题目:https://leetcode.com/problems/add-binary/tabs/description代码:class Solution {public: string addBinary(string a, string b) { int i = a.size()-1,j = b.size()-1,temp=0; string c;原创 2017-07-29 15:14:18 · 226 阅读 · 0 评论 -
leetcode --69. Sqrt(x)
题目:https://leetcode.com/problems/sqrtx/tabs/description代码:class Solution {public: int mySqrt(int x) { double k=1.0; while(fabs(k*k-x)>0.01){ k=(k+x/k)/2; }原创 2017-07-29 15:28:52 · 258 阅读 · 0 评论 -
leetcode--7. Reverse Integer
题目:https://leetcode.com/problems/reverse-integer/#/descriptionclass Solution {public: int reverse(int x) { long long sum=0; while(x!=0) { sum=sum*10+x%10;原创 2017-04-25 16:27:35 · 169 阅读 · 0 评论 -
leetcode -- 9. Palindrome Number
题目:https://leetcode.com/problems/palindrome-number/#/description代码:bool isPalindrome(int x) { if(x < 0) return false; int t = x; double s = 0; while(x > 0){ s = s*1原创 2017-07-18 08:44:38 · 181 阅读 · 0 评论 -
leetcode--13. Roman to Integer
题目:https://leetcode.com/problems/roman-to-integer/#/description代码:class Solution {public: int romanToInt(string s) { map t = { { 'I' , 1 }, { 'V' , 5 },原创 2017-07-18 14:26:10 · 191 阅读 · 0 评论 -
leetcode -- 14. Longest Common Prefix
题目:https://leetcode.com/problems/longest-common-prefix/#/description代码:class Solution {public: string longestCommonPrefix(vector &strs) { if(strs.size() == 0)return ""; int len原创 2017-07-22 09:31:39 · 191 阅读 · 0 评论 -
leetcode--20. Valid Parentheses
题目:https://leetcode.com/problems/valid-parentheses/#/description代码:class Solution {public: bool isValid(string s) { stack t; for(int i=0;i<s.size();i++){ if(s[i]=='(原创 2017-07-22 10:05:10 · 173 阅读 · 0 评论 -
leetcode--83. Remove Duplicates from Sorted List
题目:https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/代码:class Solution {public: ListNode* deleteDuplicates(ListNode* head) { ListNode* temp = head; wh原创 2017-07-31 14:16:58 · 170 阅读 · 0 评论 -
leetcode --88. Merge Sorted Array
题目:https://leetcode.com/problems/merge-sorted-array/description/代码:class Solution {public: void merge(vector& nums1, int m, vector& nums2, int n) { int i=m-1,j=n-1,k=m+n-1; wh原创 2017-07-31 14:47:15 · 236 阅读 · 0 评论 -
leetcode --100. Same Tree
题目:https://leetcode.com/problems/same-tree/description/代码:class Solution {public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p==NULL&&q==NULL) return true; else if(p==NULL&&原创 2017-08-01 14:16:23 · 212 阅读 · 0 评论 -
leetcode --21. Merge Two Sorted Lists
题目:https://leetcode.com/problems/merge-two-sorted-lists/#/description代码:class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode *dummy = new ListNode(-1),原创 2017-07-24 14:46:13 · 192 阅读 · 0 评论 -
leetcode --17. Letter Combinations of a Phone Number
题目:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/代码:class Solution {public: vector letterCombinations(string digits) { vector res; if(digits.si原创 2017-08-15 20:06:05 · 285 阅读 · 0 评论