leetcode
VigorQQ
这个作者很懒,什么都没留下…
展开
-
leetcode-29-divide two integer
class Solution {public: int divide(int dividend, int divisor) { bool flag = (dividend<0 && divisor>0) || (dividend>0 && divisor<0); long long tmp1 = a...原创 2018-08-24 15:53:05 · 211 阅读 · 0 评论 -
leetcode-171-excel sheet column number
class Solution {public: int titleToNumber(string s) { int len = s.size(); char single = 64; int p; int sum=0; for(int i=len-1;i>=0;i--){ p ...原创 2018-07-29 14:04:04 · 167 阅读 · 0 评论 -
leetcode-169-majority element
class Solution {public: int majorityElement(vector<int>& nums) { int len = nums.size(); map<int,int> result; for(int i=0;i<len;i++){ result...原创 2018-07-29 13:53:35 · 142 阅读 · 0 评论 -
leetcode-168-excel sheet column title
class Solution {public: string convertToTitle(int n) { string result = ""; while(n) { result = (char)((n-1)%26+'A') + result; n = (n-1)/26; ...原创 2018-07-29 11:19:39 · 217 阅读 · 0 评论 -
leetcode-167-two sum II- input array is sorted
class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int len = numbers.size(); vector<int> result={}; int i = 0, j = len-1...原创 2018-07-29 10:42:48 · 149 阅读 · 0 评论 -
leetcode-160-intersection of two linked lists
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *g...原创 2018-07-29 10:09:30 · 200 阅读 · 0 评论 -
leetcode-145-min stack
class MinStack {private: stack<int> stk; stack<int> min;public: /** initialize your data structure here. */ MinStack() { } void push(int x) { ...原创 2018-07-24 21:05:22 · 174 阅读 · 0 评论 -
leetcode-141-link list cycle
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCyc...原创 2018-07-24 20:45:54 · 284 阅读 · 0 评论 -
leetcode-136-single number
class Solution {public: int singleNumber(vector<int>& nums) { int result = 0; for(auto x:nums){ result ^= x; } return result; }...原创 2018-07-24 20:27:50 · 146 阅读 · 0 评论 -
leetcode-125-valid palindrome
class Solution {public: bool isPalindrome(string s) { if(s.empty() == true) return true; string result=""; for(auto x:s){ if(isa...原创 2018-07-24 20:11:17 · 140 阅读 · 0 评论 -
leetcode-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) {} * }; */clas...原创 2018-07-04 20:05:17 · 148 阅读 · 0 评论 -
leetcode-122-best time to buy and sell stock II
class Solution {public: int maxProfit(vector<int>& prices) { int max=0; int len = prices.size(); for(int i=1;i<len;i++){ if(prices[i]>prices[i-...原创 2018-07-09 21:49:01 · 243 阅读 · 0 评论 -
leetcode-121-best time to buy and sell stock
class Solution {public: int maxProfit(vector<int>& prices) { int len = prices.size(); int max = 0; for(int i=0;i<len-1;i++){ for(int j=i+1;j<le...原创 2018-07-09 21:38:03 · 148 阅读 · 0 评论 -
leetcode-172-Factorial Trailing Zeroes
class Solution {public: int trailingZeroes(int n) { int i = 1; int result = 0; while(pow(5,i)<=n){ result += (n / pow(5,i)); i++; } ...原创 2018-07-29 14:17:31 · 195 阅读 · 0 评论 -
leetcode-189-rotate array
class Solution {public: void rotate(vector<int>& nums, int k) { if( nums.empty() || (k %= nums.size()) == 0){ return; } int n = nums.size()...原创 2018-08-22 08:42:16 · 156 阅读 · 0 评论 -
leetcode-190-reverse bits
class Solution {public: uint32_t reverseBits(uint32_t n) { uint32_t result = 0; for(int i=0; i<32; i++){ if(n & 1 == 1){ result = (res...原创 2018-08-22 08:54:10 · 151 阅读 · 0 评论 -
leetcode-24-swap nodes in pairs
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* s...原创 2018-08-24 12:40:16 · 177 阅读 · 0 评论 -
leetcode-22-generate parantheses
class Solution {public: vector<string> generateParenthesis(int n) { vector<string> result; searchDfs(n, n, "", result); return result; } void sea...原创 2018-08-24 11:10:18 · 228 阅读 · 0 评论 -
leetcode-19-remove Nth node from end of list
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* r...原创 2018-08-24 10:08:19 · 152 阅读 · 0 评论 -
leetcode-18-4sum
class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { if(nums.size() < 4){ return {}; } vector<vect...原创 2018-08-23 17:01:10 · 152 阅读 · 0 评论 -
leetcode-17-combinations of a phone number
class Solution {public: vector<string> letterCombinations(string digits) { vector<string> ans; if(digits.size() == 0) return ans; int depth = di...转载 2018-08-23 11:16:29 · 169 阅读 · 0 评论 -
leetcode-16-3sum closest
class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { if(nums.size() < 3){ return {}; } vector<vector<int&g...原创 2018-08-22 19:51:55 · 160 阅读 · 0 评论 -
leetcode-15-3sum
class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { if(nums.size() < 3){ return {}; } vector<vector<int&g...原创 2018-08-22 17:20:07 · 135 阅读 · 0 评论 -
leetcode-12-integer to roman
class Solution {public: string intToRoman(int num) { string result = ""; vector<int> val{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; vector<string>...原创 2018-08-22 15:12:55 · 152 阅读 · 0 评论 -
leetcode-11-container with most water
class Solution {public: int maxArea(vector<int>& height) { int i = 0; int j = height.size()-1; int result = 0; while(i < j){ result = max...原创 2018-08-22 15:04:51 · 188 阅读 · 0 评论 -
leetcode-8-string to integer(atoi)
class Solution {public: int myAtoi(string str) { int len = str.size(); long long result = 0; int index; bool flag = false; for(int i=0; i<len;...原创 2018-08-22 11:08:05 · 223 阅读 · 0 评论 -
leetcode-6-zigzag conversion
class Solution {public: string convert(string s, int numRows) { if(s.size() == 1 || numRows == 1){ return s; } int len = s.size(); i...原创 2018-08-22 09:56:53 · 148 阅读 · 0 评论 -
leetcode-191-number of 1 bits
class Solution {public: int hammingWeight(uint32_t n) { int result = 0; for(int i=0; i<32; i++){ if(n == 0){ break; } ...原创 2018-08-22 09:05:53 · 134 阅读 · 0 评论 -
leetcode-119-pascal's triangle
class Solution {public: vector<int> getRow(int rowIndex) { vector<vector<int>> result; vector<int> ele={}; int ini=1; for(int i=0 ; i<ro...原创 2018-07-09 21:00:09 · 202 阅读 · 0 评论 -
leetcode-118-pascal's triangle
class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>> result; vector<int> ele={}; int ini=1; for(in...原创 2018-07-08 21:54:18 · 182 阅读 · 0 评论 -
leetcode-112-path sum I
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...原创 2018-07-08 21:19:23 · 175 阅读 · 0 评论 -
leetcode-35-search insert position
class Solution {public: int searchInsert(vector<int>& nums, int target){ for(int i =0; i<nums.size();i++){ if(nums[i]>=target) return i; ...原创 2018-06-15 16:19:39 · 136 阅读 · 0 评论 -
leetcode-28-implement strStr()
class Solution {public: int strStr(string haystack, string needle) { if(needle == "") return 0; int lenHay = haystack.size(); int lenNee = needle.size(); ...原创 2018-06-14 21:47:13 · 108 阅读 · 0 评论 -
leetcode-27-remove elements
class Solution {public: int removeElement(vector<int>& nums, int val) { vector<int>::iterator it=nums.begin(); while(it != nums.end()){ if(*it == val){...原创 2018-06-14 21:16:48 · 106 阅读 · 0 评论 -
leetcode-26-remove duplicates from sorted array
class Solution {public: int removeDuplicates(vector<int>& nums) { vector<int>::iterator it=nums.begin(); vector<int>::iterator it_1 = it; it_1++; ...原创 2018-06-14 21:11:43 · 113 阅读 · 0 评论 -
leetcode-21-merge two sorted lists
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(l1 == NULL) return l2; if(l2 == NULL) return l1; ListNode *lp=NULL...原创 2018-06-14 19:56:17 · 180 阅读 · 0 评论 -
leetcode-20-valid parentheses
class Solution {public: bool isValid(string s) { if(s.empty()) return true; stack<char> result; for(auto x:s){ if(x == '(' || x == '[' || x =...原创 2018-06-14 17:31:48 · 117 阅读 · 0 评论 -
leetcode-14-longest common prefix
class Solution {public: string longestCommonPrefix(vector<string>& strs) { string result=""; if(strs.empty()) return result; int len = strs.size(); ...原创 2018-06-14 16:44:13 · 120 阅读 · 0 评论 -
leetcode-13-roman to integer
class Solution {public: int romanToInt(string s) { int len = s.size(); int num=0; for(int i=0;i<len;i++){ if(s[i] == 'I' && (s[i+1] == 'V' || s[i+1]...原创 2018-06-14 16:15:05 · 117 阅读 · 0 评论 -
leetcode-09-palindrome number
class Solution {public: bool isPalindrome(int x) { if(x < 0) return false; vector<int> result; int k,j=0,l=0; while(x != 0){ k = x...原创 2018-06-14 15:55:52 · 105 阅读 · 0 评论