代码改变世界

leetcode - Remove Duplicates from Sorted Array II

2013-11-02 21:20 by 张汉生, 174 阅读, 0 推荐, 收藏,
摘要:class Solution {public: int removeDuplicates(int A[], int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if (n<=1) return n; int last = 1; for (int i=1; i<n; i++){ ... 阅读全文

leetcode - Path Sum

2013-11-02 21:14 by 张汉生, 163 阅读, 0 推荐, 收藏,
摘要:1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */10 class Solution {11 public:12 bool hasPathSum(TreeNode *root, int sum) {13 // IM... 阅读全文

leetcode - Populating Next Right Pointers in Each Node II

2013-11-02 21:04 by 张汉生, 144 阅读, 0 推荐, 收藏,
摘要:1 /** 2 * Definition for binary tree with next pointer. 3 * struct TreeLinkNode { 4 * int val; 5 * TreeLinkNode *left, *right, *next; 6 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:11 void connect(TreeLinkNode *root) {1... 阅读全文

leetcode - Sort Colors

2013-11-02 20:28 by 张汉生, 155 阅读, 0 推荐, 收藏,
摘要:1 class Solution { 2 public: 3 void sortColors(int A[], int n) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 int i =0 ,j = n-1; 7 while (i=0 && A[j]==2)10 j--;11 ... 阅读全文

leetcode - Remove Nth Node From End of List

2013-11-02 20:01 by 张汉生, 168 阅读, 0 推荐, 收藏,
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:11 ListNode *removeNthFromEnd(ListNode *head, int n) {12 // IMPORTANT: Please reset ... 阅读全文

leetcode - Set Matrix Zeroes

2013-11-02 19:49 by 张汉生, 161 阅读, 0 推荐, 收藏,
摘要:1 class Solution { 2 public: 3 void setZeroes(vector > &matrix) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if (matrix.size()=0; k--)15 matrix[k][j] = 0;16 ... 阅读全文

leetcode - Linked List Cycle II

2013-11-02 14:37 by 张汉生, 178 阅读, 0 推荐, 收藏,
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution {10 public:11 ListNode *detectCycle(ListNode *head) {12 // IMPORTANT: Please reset any member d... 阅读全文

leetcode - Plus One

2013-11-02 13:52 by 张汉生, 157 阅读, 0 推荐, 收藏,
摘要:1 class Solution { 2 public: 3 // not binary digits !!!!! shit! 4 vector plusOne(vector &digits) { 5 // Note: The Solution object is instantiated only once and is reused by each test case. 6 vector rlt(digits.size()+1,0); 7 int n = digits.size(); 8 rlt[n]=1; ... 阅读全文

leetcode - Rotate Image

2013-11-02 13:02 by 张汉生, 178 阅读, 0 推荐, 收藏,
摘要:1 class Solution { 2 public: 3 void rotate(vector > &matrix) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 int n = matrix.size(); 6 if (n<=1) 7 return; 8 for (int i=0; i<n/2; i++){ 9 for (int... 阅读全文

leetcode - Unique Paths

2013-10-31 16:57 by 张汉生, 117 阅读, 0 推荐, 收藏,
摘要:1 class Solution { 2 public: 3 int uniquePaths(int m, int n) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if (m<=0 || n<=0) 7 return 0; 8 int * f = new int[n]; 9 ... 阅读全文
上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 16 下一页