![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
gfgdsg
这个作者很懒,什么都没留下…
展开
-
【LeetCode】Generate Parentheses
class Solution {public: //Generate Parentheses void generate(int n, string s, int l, int r, vector &result){ if (l == n) { result.push_back(s.append(n - r, ')')); return; } generate(n, s原创 2014-12-01 19:56:02 · 275 阅读 · 0 评论 -
【LeetCode】Decode Ways
if (s.empty() || s[0] == '0') return 0; int prev = 0; int curr = 1; for (int i = 1; i <= s.size(); i++) { if (s[i - 1] == '0') curr = 0; if (i < 2 || !(s[i - 2] == '1' || s[i - 2] == '2'原创 2014-12-04 10:56:56 · 280 阅读 · 0 评论 -
【LeetCode】Longest Palindromic Substring
//一直超时啊。。。跪了class Solution {public: string longestPalindrome(string s) { int i, j,max_len,temp; int n = s.size(); max_len = 1; temp=0; bool f[n][n];// vector> f(n, vector(n, false));原创 2014-12-04 15:33:31 · 278 阅读 · 0 评论 -
【LeetCode】Palindrome Partitioning II
class Solution {public: int minCut(string s) { const int n = s.size(); vector f(n+1); //bool p[n][n]; vector> p(n, vector(n,false)); for (int i = 0; i <= n; i++) { f[i] = n - 1 - i原创 2014-12-05 11:57:21 · 291 阅读 · 0 评论 -
【LeetCode】Palindrome Partitioning
class Solution {public: void dfs(string s, vector &path, vector> &result){ if (s.size() < 1) { result.push_back(path); return; } for (int i = 0; i < s.size(); i++) { int begin = 0; int原创 2014-12-05 11:07:17 · 256 阅读 · 0 评论 -
【LeetCode】Min Stack
class MinStack{public: void push(int x) { nums.push(x); if (mins.empty() || x <= mins.top()) { mins.push(x); } } void pop() { if (nums.empty())return; if (nums.top() == mins.top原创 2014-12-25 16:23:18 · 249 阅读 · 0 评论 -
【LeetCode】Interleaving String
class Solution {public: bool isInterleave(string s1, string s2, string s3) { if (s3.length() != s1.length() + s2.length()) return false; vector>f(s1.length() + 1, vector(s2.length() +原创 2014-12-25 17:12:45 · 278 阅读 · 0 评论 -
【LeetCode】Combinations
void dfs(int n,int k,int start,int cur,vector&path,vector>&result){ if (cur == k) { result.push_back(path); } for (int i = start; i <= n; i++) { path.push_back(i); dfs(n, k, i + 1, cur + 1原创 2014-12-25 15:57:44 · 258 阅读 · 0 评论 -
【LeetCode】Count and Say
string convert(string str){ stringstream ss; int i,count=0; char last=str[0]; for(i=0;i<=str.size();i++) { if(str[i]==last) { count++; } else { ss<<count<<last; c原创 2014-12-10 16:22:20 · 244 阅读 · 0 评论 -
【LeetCode】Longest Substring Without Repeating Characters
class Solution {public: int lengthOfLongestSubstring(string s) { const int MAX=256; int last[MAX]; fill(last,last+MAX,-1); int start=0; int max_len=0; for(int i=0;i<s.size();i++) {原创 2014-12-10 17:58:09 · 329 阅读 · 0 评论 -
【LeetCode】Reverse Integer
public class Solution { public static int reverse(int x){ long newNum=0,old=x;//*Notice* long,so that it can compare with MAX_Value x=(x>0)?x:(0-x); while(x!=0){ newNum*=10;原创 2014-12-10 11:50:23 · 291 阅读 · 0 评论 -
【LeetCode】Search in Rotated Sorted Array
class Solution {public: int search(int A[], int n, int target) { int start = 0, end = n; while (start != end) { int mid = (start + end) / 2; if (A[mid] == target) return mid;原创 2015-01-08 11:39:31 · 297 阅读 · 0 评论 -
【LeetCode】Plus One
class Solution {public: void add(vector &digits, int num){ int c; c = num; for (auto i = digits.rbegin(); i != digits.rend();i++) { *i += c; c = *i / 10; *i = *i % 10; } if (c > 0)原创 2014-12-03 15:06:32 · 240 阅读 · 0 评论 -
【LeetCode】Triangle
class Solution {public: int minimumTotal(vector > &triangle) { if (triangle.size() == 0) return 0; vector f(triangle[triangle.size() - 1].size()); f[0] = triangle[0][0]; for (int i =原创 2014-12-01 20:43:42 · 325 阅读 · 0 评论 -
【LeetCode】Merge Sorted Array
class Solution {public: void merge(int A[], int m, int B[], int n) { int ia=m-1,ib=n-1,result=m+n-1; while(ia>=0&&ib>=0) { A[result--]=A[ia]>=B[ib]?A[ia--]:B[i原创 2015-01-07 17:23:23 · 260 阅读 · 0 评论 -
【LeetCode】Multiply Strings
class Solution {public: string multiply(string num1, string num2) { if(num1=="0"||num2=="0") return "0"; int l1=num1.length(),l2=num2.length(); int *n1=new int[l1]; int *n2=new i原创 2014-12-17 20:29:33 · 277 阅读 · 0 评论 -
【LeetCode】Pascal’s Triangle
class Solution {public: vector > generate(int numRows) { vector> result; vectorret; int i, j; for (i = 0; i < numRows; i++) { if (i == 0) { ret.push_back(1); result.push原创 2014-12-02 11:08:45 · 328 阅读 · 0 评论 -
【LeetCode】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 *re原创 2015-01-07 20:03:25 · 280 阅读 · 0 评论 -
【LeetCode】Remove Element
class Solution {public: int removeElement(int A[], int n, int elem) { int result=0; for(int i=0;i<n;i++) { A[result]=A[i]; if(A[i]!=elem)原创 2015-01-07 20:08:57 · 277 阅读 · 0 评论 -
【LeetCode】Search for a Range
class Solution {public:int Left(int A[], int n, int target){ if (n == 0)return -1; int left = 0; int right = n - 1; while (left < right) { int mid = left + (right - left) / 2; if (A[mid] >原创 2014-12-03 14:31:48 · 283 阅读 · 0 评论 -
【LeetCode】Pascal's Triangle II
class Solution {public: vector getRow(int rowIndex) { vector result(rowIndex + 1); result[0] = 1; for (int i = 1; i <= rowIndex; i++) { for (int j = i; j >= 0; j--) { if (j ==原创 2014-12-02 11:20:24 · 254 阅读 · 0 评论 -
【LeetCode】Search in Rotated Sorted Array II
class Solution {public: bool search(int A[], int n, int target) { int start = 0, end = n; while (start != end) { int mid = (start + end) / 2; if (A[mid] == target) return原创 2015-01-08 11:42:10 · 400 阅读 · 0 评论 -
【LeetCode】Climbing Stairs
class Solution {public: int climbStairs(int n) { int prev, curr; prev = 0; curr = 1; for (int i = 0; i < n; i++) { int temp = curr; curr += prev; prev = temp; } return curr; }原创 2014-12-03 14:47:20 · 295 阅读 · 0 评论 -
【LeetCode】Palindrome Number
class Solution {public: bool isPalindrome(int x) { if(x<0) return false; if(x==0) return true; int base=1; while(x/base>=10) { base*=10; } int leftnum,rightnum; while(x) {原创 2014-12-10 12:26:52 · 318 阅读 · 0 评论 -
【LeetCode】Integer to Roman
class Solution {public: string intToRoman(int num) { const int radix[]={1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; const string symbol[]={"M", "CM", "D", "CD", "C", "XC原创 2014-12-10 12:39:37 · 272 阅读 · 0 评论 -
【LeetCode】Anagrams
class Solution {public: vector anagrams(vector &strs) { vector result; map> store; int i; for(i=0;i<strs.size();i++) { string temp; temp=strs[i]; sort(temp.begin(),temp.end());原创 2014-12-10 15:55:56 · 244 阅读 · 0 评论 -
【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-12-30 16:57:09 · 291 阅读 · 0 评论 -
【LeetCode】Search a 2D Matrix
class Solution {public: bool searchMatrix(vector > &matrix, int target) { int first, mid,end, row, col; row = matrix.size(); col = matrix[0].size(); first = 0; end = row*col; while (f原创 2014-12-14 15:23:31 · 305 阅读 · 0 评论 -
【LeetCode】Pow(x, n)
class Solution {public: double power(double x, int n){ if (n == 0) return 1; double v = power(x, n / 2); if (n % 2 == 0) return v*v; else return v*v*x;}double pow(double x, int n){原创 2014-12-14 16:16:45 · 254 阅读 · 0 评论 -
【LeetCode】Sqrt(x)
class Solution {public: int sqrt(int x) { long long left = 0; long long right = x; while (left <= right) { long long mid = left + (right - left) / 2; long long square = mid*mid;原创 2014-12-14 16:15:39 · 274 阅读 · 0 评论 -
【LeetCode】Search Insert Position
class Solution {public: int searchInsert(int A[], int n, int target) { int first, end,mid,value,result; first = 0; end = n ; while (first < end) { mid = first + (end - first) / 2;原创 2014-12-14 15:42:19 · 231 阅读 · 0 评论 -
【LeetCode】Add Binary
class Solution {public: string addBinary(string a, string b) { int aindex = a.size() - 1; int bindex = b.size() - 1; string c; int flag = 0,num; while (aindex>=0 && bindex>=0) { nu原创 2015-03-04 09:46:26 · 507 阅读 · 0 评论 -
【LeetCode】Partition List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *pa原创 2015-03-04 10:34:01 · 381 阅读 · 0 评论 -
【LeetCode】Flatten Binary Tree to Linked List
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2015-03-05 11:49:30 · 341 阅读 · 0 评论 -
【LeetCode】Balanced Binary Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2015-03-05 11:06:39 · 307 阅读 · 0 评论 -
【LeetCode】Linked List Cycle II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *de原创 2015-03-05 10:12:48 · 383 阅读 · 0 评论 -
【LeetCode】Symmetric Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-12-30 16:28:12 · 296 阅读 · 0 评论 -
【LeetCode】Same Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-12-29 15:53:23 · 259 阅读 · 0 评论 -
【LeetCode】Scramble String
class Solution {public: bool isScramble(string s1, string s2) { const int N = s1.size(); if (N != s2.size()) return false; bool f[N + 1][N][N]; fill_n(&f[0][0][0], (N + 1)*N*N, false原创 2014-12-27 15:36:22 · 441 阅读 · 0 评论 -
【LeetCode】Set Matrix Zeroes
class Solution {public: void setZeroes(vector > &matrix) { bool x[1000] = { false }; bool y[1000] = {false}; int i, j; for (i = 0; i < matrix.size(); i++) { for (j = 0; j < matrix[0原创 2014-12-27 15:55:30 · 356 阅读 · 0 评论