leetcode
zhenkaiwang
这个作者很懒,什么都没留下…
展开
-
leetcode:Add Binary
class Solution {public: string addBinary(string a, string b) { // Start typing your C/C++ solution below // DO NOT write int main() function int m=a.size();原创 2013-08-29 20:45:57 · 445 阅读 · 0 评论 -
leetcode-084:Largest Rectangle in Histogram
int largestRectArea(vector &h) { stack p; int i = 0, m = 0; h.push_back(0); while(i < h.size()) { if(p.empty() || h[p.top()] <= h[i]) p.push(i++); el转载 2013-09-02 20:59:54 · 483 阅读 · 0 评论 -
leetcode-012:Integer to Roman
class Solution {public: string intToRoman(int num) { // Start typing your C/C++ solution below // DO NOT write int main() function int nums[] = {1000, 900, 500, 4转载 2013-09-03 11:15:29 · 497 阅读 · 0 评论 -
leetcode-004:Median of Two Sorted Arrays
int findKth(int a[],int m,int b[],int n,int k){ if(m>n) return findKth(b,n,a,m,k); if(m==0) return b[k-1]; if(k==1) return min(a[0],b[0]); int pa=min(k/2,m),pb=k-pa; if(a[pa-1]==b[pb-1]原创 2013-08-30 21:11:25 · 477 阅读 · 0 评论 -
Letter Combinations of a Phone Number
class Solution {/*Straightforward, and easy**/private: vector rep; vector result; void build(const string & digits, int digitIndex, string ss) { int num = digits[digitI转载 2013-09-03 16:32:40 · 436 阅读 · 0 评论 -
4 Sum
class Solution {public: vector > fourSum(vector &num, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function int n = num.size();转载 2013-09-03 16:43:39 · 438 阅读 · 0 评论 -
Generate Parentheses
class Solution {public: vector generateParenthesis(int n) { vector ans; if (n>0) generator(ans, "", 0, 0, n); return ans; } void gener转载 2013-09-03 17:26:11 · 431 阅读 · 0 评论 -
leetcode-13:Roman to Integer
class Solution {public: int romanToInt(string s) { if (s.length()==0) return 0; map m; m['I'] = 1; m['V'] = 5; m['X'] = 10; m['L'] =转载 2013-09-03 12:20:59 · 455 阅读 · 0 评论 -
3Sum Closest
class Solution {public: int threeSumClosest(vector &num, int target) { sort(num.begin(),num.end()); int n = num.size(); int cl = num[0]+num[1]+num.back(); in原创 2013-09-03 16:13:32 · 521 阅读 · 0 评论 -
leetcode-036:Valid Sudoku
class Solution {public: bool isValidSudoku(vector > &board) { // Start typing your C/C++ solution below // DO NOT write int main() function vector > rows(9, vector(9,转载 2013-09-04 11:16:08 · 899 阅读 · 0 评论 -
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原创 2013-09-04 22:02:36 · 456 阅读 · 0 评论 -
Valid Number
bool isNumber(const char *s) { enum InputType { INVALID, // 0 SPACE, // 1 SIGN, // 2 DIGIT, // 3 DOT, // 4 EXPONENT转载 2013-09-09 12:19:21 · 420 阅读 · 0 评论 -
leetcode-011:Container With Most Water
class Solution {public: int maxArea(vector &height) { // Start typing your C/C++ solution below // DO NOT write int main() function int result=0; if(height.s原创 2013-09-02 22:15:43 · 480 阅读 · 0 评论 -
leetcode-042:Trapping Rain Water
class Solution {public: int trap(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int result=0; if(n==0) retur原创 2013-09-02 21:47:43 · 559 阅读 · 0 评论 -
leetcode:Length of Last Word
int lengthOfLastWord(const char *s) {//Length of Last Word // Start typing your C/C++ solution below // DO NOT write int main() function if(!s) return 0; int lastle原创 2013-08-29 20:33:19 · 424 阅读 · 0 评论 -
Search Insert Position
int searchInsert(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n<=0) return 0; int left=0原创 2013-08-29 21:13:16 · 405 阅读 · 0 评论 -
leetcode others...
int maxProfit(vector &prices) {//Best Time to Buy and Sell Stock if (prices.size() == 0) return 0; int low = prices[0]; int profit = 0; for (int i = 1; i < pri原创 2013-08-29 21:15:10 · 442 阅读 · 0 评论 -
leetcode:Sort Colors
void sortColors(int A[], int n) { int r=0,w=0,b=n-1; for(w=0;w<=b;) { if(A[w]==0) swap(A[r++],A[w++]); // why also w++? because r's next position can only be white else if(A[w]==2)转载 2013-08-30 12:29:11 · 401 阅读 · 0 评论 -
leetcode:Decode Ways
class Solution {public: int numDecodings(string s) { if(s.size()==0) return 0; vector ways(s.size()+1,1); for(int i=s.size()-1;i>=0;i--) {转载 2013-08-30 11:08:03 · 382 阅读 · 0 评论 -
leetcode-006:ZigZag Conversion
string convert(string s, int nRows) { int len = s.length(); if (len < 3 || nRows == 1) return s; bool down = true; vector > tab(nRows); for(int i = 0, row = 0; i <转载 2013-09-01 15:58:39 · 425 阅读 · 0 评论 -
leetcode-008:String to Integer (atoi)
class Solution {public: int atoi(const char *str) { // Start typing your C/C++ solution below // DO NOT write int main() function while(*str==' ') str++; int原创 2013-09-01 20:21:25 · 461 阅读 · 0 评论 -
leetcode-3:Longest Substring Without Repeating Characters
class Solution {public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int n=s.size();原创 2013-08-30 21:44:56 · 609 阅读 · 0 评论 -
leetcode-010:Regular Expression Matching
class Solution {public: bool isMatch(const char *s, const char *p) { // Start typing your C/C++ solution below // DO NOT write int main() function if(*p=='\0') re原创 2013-09-01 21:20:15 · 428 阅读 · 0 评论 -
leetcode-005:Longest Palindromic Substring
// Transform S into T.// For example, S = "abba", T = "^#a#b#b#a#$".// ^ and $ signs are sentinels appended to each end to avoid bounds checkingstring preProcess(string s) { int n = s.lengt转载 2013-09-01 14:06:18 · 502 阅读 · 0 评论 -
strStr算法(无KMP) 略微优化的暴力解法
class Solution {public: char* strStr(const char *str, const char *target) { if (!*target) return (char*)str; char *p1 = (char*)str, *p2 = (char*)target; char *p1Adv = (char*)s原创 2013-09-16 00:43:38 · 844 阅读 · 0 评论