代码改变世界

leetcode - Reverse Integer

2013-10-19 21:19 by 张汉生, 134 阅读, 0 推荐, 收藏, 编辑
摘要:class Solution {public: int reversePositive(int x){ int ans = 0; while(x!=0){ ans = ans*10 + (x%10); x = x / 10; } return ans; } int reverse(int x) { // Note: The Solution object is instantiated only once and is reused by each test ca... 阅读全文

leetcode - Same Tree

2013-10-19 21:10 by 张汉生, 120 阅读, 0 推荐, 收藏, 编辑
摘要:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { // Note: The Solution object is instantiate... 阅读全文

leetcode - Two Sum

2013-10-14 10:52 by 张汉生, 177 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 vector twoSum(vector &numbers, int target) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 int size = numbers.size(); 6 vector cp; 7 int i; 8 for (i=0; itarget)16 ... 阅读全文

leetcode - Single Number

2013-10-14 10:37 by 张汉生, 144 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 if (n<=0) 6 return 0; 7 int ans=0; 8 for (int i=0; i<n; i++) 9 ans ^= A[i];10 ... 阅读全文

leetcode - Single Number II

2013-10-14 10:34 by 张汉生, 152 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 if (n>j)&1) ) % 3;14 int ans=0;15 for (i=0; i<32; i++)16 ans = ans+(bits[i]<<i);17 re... 阅读全文

leetcode - Copy List with Random Pointer

2013-10-13 19:31 by 张汉生, 183 阅读, 0 推荐, 收藏, 编辑
摘要:/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(int x) : label(x), next(NULL), random(NULL) {} * }; *//*struct NodeInfo{ RandomListNode * node; int pos; NodeInfo(RandomListNod... 阅读全文

leetcode - Word Break II

2013-10-13 16:44 by 张汉生, 558 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处class Solution {public: vector> rlts; int * flag; bool wordBreak(string &s, int index, unordered_set &dict){ if (flag[index]==-1) return false; if (!rlts[index].empty()) return true; int len = s.length()-index; if (len==0){ ... 阅读全文

leetcode - Word Break

2013-10-13 10:59 by 张汉生, 139 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述:点击此处class Solution {public: int * flag; bool wordBreak(string &s, int index, unordered_set &dict){ if (flag[index]==-1) return false; int len = s.length()-index; if (len==0) return true; string header; bool ans = false; for (... 阅读全文

leetcode - Merge k Sorted Lists

2013-09-13 12:54 by 张汉生, 198 阅读, 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 ListNode * merge2Lists(ListNode* list1, ListNode* list2){10 ListNode * rlt = NULL;11 ListNode * curNode = NUL... 阅读全文

leetcode - Merge Intervals

2013-09-12 15:13 by 张汉生, 151 阅读, 0 推荐, 收藏, 编辑
摘要:题目描述,点击此处苍天啊,谁能告诉我为什么11行改成b?a:b;17 }18 vector merge(vector &intervals) {19 // Start typing your C/C++ solution below20 // DO NOT write int main() function21 if (intervals.size()<=0)22 return intervals;23 sort(intervals.begin(), intervals.end(), cmp);24 int size = interv... 阅读全文
上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 16 下一页