leetcode
学生症
coder
展开
-
[leetcode] Word Search
class Solution {public: bool exist(vector > &board, string word) { if (word.size()==0) { return true;//空串返回true } int row=board.size(); int co原创 2014-05-20 00:22:57 · 580 阅读 · 0 评论 -
[leetcode] Word Ladder
#include #include //mac os下引用unordered_set,散列容器#include #include using namespace std;class Solution {public: int ladderLength(string start, string end, unordered_set &dict) {原创 2014-05-12 01:26:17 · 856 阅读 · 0 评论 -
[leetcode] Reverse Words in a String
#include "iostream"using namespace std;class Solution {public: void reverseWords(string &s) { int len=(int)s.length()-1;//显示转换unsigned long to int string res=原创 2014-05-11 02:09:46 · 568 阅读 · 0 评论 -
[leetcode] Sort List
/**Definition for singly-linked list.*/#include using namespace std;struct ListNode{ int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution{public:原创 2014-05-06 14:29:58 · 527 阅读 · 0 评论 -
[leetcode] Merge Two Sorted Lists
Merge Two Sorted Lists原创 2014-05-05 00:09:46 · 542 阅读 · 0 评论 -
[leetcode] Count and Say
Count and Saysh//// main.cpp// CPPTest//// Created by lyd on 14-5-3.// Copyright (c) 2014年 ___LYD___. All rights reserved.//#include using namespace std;class Solution {public:原创 2014-05-04 00:52:25 · 769 阅读 · 0 评论 -
[leetcode] Container With Most Water
第一次超时:#include#includeusing namespace std;class Solution {public: int maxArea(vector &height) { int len=height.size(); int tempArea=0; int maxArea=0; for (int i = 0; i <原创 2014-04-29 15:57:14 · 571 阅读 · 0 评论 -
[leetcode] Reverse Integer
#includeusing namespace std;class Solution {public: int reverse(int x) { int result=0; while(x){ result=result*10+x%10; x=x/10; } return result; }};in原创 2014-04-29 01:19:54 · 551 阅读 · 0 评论 -
[leetcode] string to integer
#include using namespace std;class Solution {public: int atoi(const char *str) { if (*str==NULL){ return 0;//空串 } //假定合法的字符只有'0'~'9','+','-',' ' l原创 2014-04-26 18:41:51 · 508 阅读 · 0 评论 -
[leetcode] Remove Duplicates from Sorted List II
#include "iostream"using namespace std;/**Definition for singly-linked list.*/struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {原创 2014-05-07 10:53:15 · 618 阅读 · 0 评论 -
[leetcode] Remove Duplicates from Sorted List
#include "iostream"using namespace std;/**Definition for singly-linked list.*/struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {原创 2014-05-06 01:01:28 · 529 阅读 · 0 评论 -
[leetcode] Partition List
#include "iostream"using namespace std;/**Definition for singly-linked list.*/struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {p原创 2014-05-06 00:12:54 · 492 阅读 · 0 评论 -
[leetcode] Rotate List
/** Definition for singly-linked list. */#include using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public:原创 2014-05-05 01:12:57 · 553 阅读 · 0 评论 -
[leetcode] Insertion Sort List
#include "iostream"using namespace std;/**Definition for singly-linked list.*/struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {原创 2014-05-07 15:11:00 · 581 阅读 · 0 评论 -
[leetcode] Add Two Numbers
#include using namespace std;/** Definition for singly-linked list. */struct ListNode{ int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution{public:原创 2014-05-04 21:48:44 · 650 阅读 · 0 评论 -
[leetcode] Valid Parentheses
class Solution {public: bool isValid(string s) { if (s.size()%2==1) {//奇数个符号 return false; } //使用栈 stack sc;原创 2014-05-20 10:58:56 · 520 阅读 · 0 评论 -
[leetcode] Path Sum
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-07-17 20:47:16 · 682 阅读 · 1 评论 -
[leetcode] Path Sum II
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-07-17 21:11:47 · 551 阅读 · 0 评论 -
[leetcode] Binary Tree Postorder Traversal
Binary Tree Postorder Traversal/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NUL原创 2014-07-19 16:46:12 · 498 阅读 · 0 评论 -
[leetcode] First Missing Positive
#include "iostream"using namespace std;class Solution {public: int firstMissingPositive(int A[], int n) { if (A==NULL||n<1) {//数组为空 return 1; } int原创 2014-05-08 15:35:42 · 524 阅读 · 0 评论 -
[leetcode] Binary Tree Preorder 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-07-19 13:34:20 · 493 阅读 · 0 评论 -
[leetcode] Binary Tree Inorder Traversal
使用递归class Solution {private: vector res;public: vector inorderTraversal(TreeNode *root) { if (root!=NULL) { inorderTraversal(root->left); res.push_back(root原创 2014-07-20 02:36:13 · 490 阅读 · 0 评论 -
[leetcode] Binary Tree Zigzag Level Order Traversal
class Solution {private: vector> res;public: vector > zigzagLevelOrder(TreeNode *root) { if (root==NULL) { return res; } queue curQ,nextQ;原创 2014-07-21 00:36:02 · 528 阅读 · 0 评论 -
[leetcode] Binary Tree Level Order Traversal
使用两个队列/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas原创 2014-07-20 01:36:45 · 485 阅读 · 0 评论 -
[leetcode] Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder TraversalConstruct Binary Tree from Inorder and Postorder Traversal原创 2014-07-21 01:24:19 · 537 阅读 · 0 评论 -
[leetcode] Binary Tree Level Order Traversal II
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-07-20 02:05:22 · 462 阅读 · 0 评论 -
[leetcode] Convert Sorted List to Binary Search Tree
解法:把链表转换为vectorzclass Solution {public: TreeNode *sortedListToBSTbyIndex(vector &num,int begin,int end){ if (begin>end) { return nullptr; } int mid=be原创 2014-07-21 21:43:05 · 517 阅读 · 0 评论 -
[leetcode] Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Treeclass Solution {public: TreeNode *sortedArrayToBSTbyIndex(vector &num,int begin,int end){ if (begin>end) { return null原创 2014-07-21 21:35:26 · 542 阅读 · 0 评论 -
[leetcode] Implement strStr()
Implement strStr()原创 2014-06-02 21:31:37 · 726 阅读 · 0 评论 -
[leetcode] Merge Sorted Array
class Solution {public: void merge(int A[], int m, int B[], int n) { if (n<1) {//B为空,不需处理 return; } int i=m-1; int j=n-1; int原创 2014-06-12 01:09:07 · 528 阅读 · 0 评论 -
[leetcode] Maximum Subarray
Maximum Subarray原创 2014-06-01 00:47:36 · 682 阅读 · 0 评论 -
[leetcode] Permutations
思路:交换+dspclass Solution { vector > res; int length; public: vector > permute(vector &num) { length=num.size();//有length个数 res.clear(); perm(num,0); r原创 2014-06-03 00:07:07 · 608 阅读 · 0 评论 -
[leetcode] Climbing Stairs
Climbing Stairsclass Solution {public: int climbStairs(int n) { int *step=new int[n+1]; step[1]=1; step[2]=2; int i=3; while (i<=n) { step[原创 2014-06-17 17:16:00 · 502 阅读 · 0 评论 -
[leetcode] Longest Consecutive Sequence
class Solution {public: int longestConsecutive(vector &num) { unordered_map used; for(auto i:num){ used[i]=false; } int longest=0;原创 2014-08-01 10:46:27 · 603 阅读 · 0 评论 -
[leetcode] Single Number
Single Numberclass Solution {public: int singleNumber(int A[], int n) { int res=0; for(int i=0;i<n;++i){ res^=A[i]; } return res; }};原创 2014-08-01 10:48:53 · 367 阅读 · 0 评论 -
[leetcode] Single Number II
Single Number II原创 2014-08-01 21:29:36 · 558 阅读 · 0 评论 -
[leetcode] Maximum Depth of Binary Tree
class Solution {public: int maxDepth(TreeNode *root) { if(root==nullptr){ return 0; } return max(maxDepth(root->left),maxDepth(root->right))+1; }};原创 2014-08-01 23:45:07 · 403 阅读 · 0 评论 -
[leetcode] Search Insert Position
Search Insert Position原创 2014-08-02 00:38:56 · 438 阅读 · 0 评论 -
[leetcode] Two Sum
Two Sum使用hash原创 2014-08-02 15:02:28 · 423 阅读 · 0 评论 -
[leetcode] Minimum Depth of Binary Tree
Minimum Depth of Binary Tree原创 2014-08-02 00:03:23 · 379 阅读 · 0 评论