代码改变世界

leetcode - ZigZag Conversion

2013-12-09 15:06 by 张汉生, 153 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 string convert(string s, int nRows) { 4 int len = s.length(); 5 if (nRows<=1 || len<=2) 6 return s; 7 string * strs = new string[nRows]; 8 for (int i=0; i<len; i++){ 9 int offset = i % (2*nRows-2);10 if (offset<nRows)11 ... 阅读全文

leetcode - Evaluate Reverse Polish Notation

2013-12-07 12:30 by 张汉生, 153 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 void parseNum(stack& _stack, const string & token){ 4 int number = atoi(token.c_str()); 5 _stack.push(number); 6 } 7 void parseOperation(stack & _stack, char token){ 8 int num1 = _stack.top(); 9 _stack.pop();10 int num2 = _stack.top();1... 阅读全文

leetcode - Permutation Sequence

2013-12-07 12:08 by 张汉生, 124 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 string getPermutation(int n, int k) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 string rlt=""; 7 unsigned *facts = new unsigned[n+1]; 8 unsigne... 阅读全文

leetcode - Scramble String

2013-12-06 11:13 by 张汉生, 121 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 bool isScramble(string &s1, string &s2, int start1, int end1, int start2, int end2){ 4 if (end1-start1 != end2-start2) 5 return false; 6 if (end1-start1 < 0) 7 return true; 8 int len = end1-start1+1; 9 if (len==1){10 if (s1.... 阅读全文

leetcode - Remove Duplicates from Sorted List II

2013-11-27 08:38 by 张汉生, 164 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 ListNode *deleteDuplicates(ListNode *head) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if (head==NULL) 7 return head; 8 ListNode * curHead ... 阅读全文

leetcode -Reverse Linked List II

2013-11-25 22:24 by 张汉生, 171 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 ListNode *reverseBetween(ListNode *head, 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 (n-mnext;14 i++;15 if (i==m){16 ltail = las... 阅读全文

leetcode - Rotate List

2013-11-25 22:07 by 张汉生, 159 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 ListNode *rotateRight(ListNode *head, int k) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 if (head == NULL || head->next ==NULL) 6 return head; 7 int size = 0; 8 ListNode * tmp = head; 9 whi... 阅读全文

leetcode - Reverse Nodes in k-Group

2013-11-25 21:30 by 张汉生, 177 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 ListNode *reverseKGroup(ListNode *head, int k) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if (knext;15 if (i==0)16 curHead = itr;17 if (i=... 阅读全文

leetcode - Restore IP Addresses

2013-11-25 13:13 by 张汉生, 163 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 bool isValid(string &s, int start, int end){ 4 int len = end-start+1; 5 if (len3) 6 return false; 7 if (len>1 && s.at(start)=='0') 8 return false; 9 int _val = atoi(s.substr(start,len).c_str());10 if (_val>=0 && _val restoreTwo... 阅读全文

leetcode - Gas Station

2013-11-23 23:30 by 张汉生, 156 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int canCompleteCircuit(vector &gas, vector &cost) { 4 // IMPORTANT: Please reset any member data you declared, as 5 // the same Solution instance will be reused for each test case. 6 if (gas.size()=cost[0])10 return 0;11 els... 阅读全文
上一页 1 2 3 4 5 6 7 ··· 16 下一页