代码改变世界

leetcode - Reverse Words in a String

2014-03-08 19:18 by 张汉生, 158 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 void reverseWords(string &); 4 }; 5 void Solution::reverseWords(string &s){ 6 int len = s.length(); 7 int i =0 ; 8 stack st; 9 while (i0)18 st.push(tmpStr);19 }20 s = "";21 while (!st.empty()){22 if (s.length()>0)23 ... 阅读全文

leetcode - Max Points on a Line

2013-12-24 12:32 by 张汉生, 249 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int maxPoints(vector &points) { 4 unsigned n = points.size(); 5 if (n xLines; 9 unordered_map yLines;10 //consider situation of x and y;11 for (auto iter = points.begin(); iter != points.end(); iter++){12 if (xL... 阅读全文

leetcode - Word Ladder II

2013-12-23 21:32 by 张汉生, 219 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 typedef unordered_map MyHashMap; 4 typedef unordered_map> SVMap; 5 vector> findLadders(string start, string end, unordered_set &dict) { 6 // Note: The Solution object is instantiated only once and is reused by each test case. 7 unsigned rlt... 阅读全文

leetcode - Substring with Concatenation of All Words

2013-12-16 21:12 by 张汉生, 180 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 typedef unordered_map HashMap; 4 bool canMatch(const char* head, const char * end, HashMap & flags, int l){ 5 if (head == end+1) 6 return true; 7 string s=""; 8 for (int i=0; isecond findSubstring(string S, vector &L) {19 ... 阅读全文

leetcode - String to Integer (atoi)

2013-12-16 20:46 by 张汉生, 167 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 int atoi(const char *str) { 4 while (*str==' ') 5 str++; 6 int sign = 1; 7 if (*str=='+') 8 str++; 9 else if (*str=='-'){10 str++;11 sign = -1;12 }13 unsigned rlt=0;14 ... 阅读全文

leetcode - Regular Expression Matching

2013-12-16 18:56 by 张汉生, 174 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 //http://blog.csdn.net/pickless/article/details/9043389 4 bool isMatch(const char *s, const char *p) { 5 if (*p == '\0'){ 6 return *s == '\0'; 7 } 8 if (*(p + 1) == '*'){ 9 while (*s != '\0' && (*s == *p || *... 阅读全文

leetcode - Word Ladder

2013-12-16 18:19 by 张汉生, 207 阅读, 0 推荐, 收藏, 编辑
摘要:1 //bfs 2 class Solution { 3 public: 4 typedef unordered_map MyHashMap; 5 int ladderLength(string start, string end, unordered_set &dict) { 6 if (start == end) 7 return 1; 8 if (start.length() != end.length()) 9 return 0;10 queue qs;11 ... 阅读全文

leetcode - Clone Graph

2013-12-16 09:01 by 张汉生, 232 阅读, 0 推荐, 收藏, 编辑
摘要:1 /** 2 * Definition for undirected graph. 3 * struct UndirectedGraphNode { 4 * int label; 5 * vector neighbors; 6 * UndirectedGraphNode(int x) : label(x) {}; 7 * }; 8 */ 9 class Solution {10 public:11 UndirectedGraphNode * cloneGraph(UndirectedGraphNode *node, unordered_map ... 阅读全文

leetcode - Candy

2013-12-16 08:59 by 张汉生, 224 阅读, 0 推荐, 收藏, 编辑
摘要:1 class Solution { 2 public: 3 struct info{ 4 int rate; 5 int index; 6 info(int r, int i):rate(r), index(i){} 7 bool operator &ratings) {12 int n = ratings.size();13 if (n vi;16 for (int i = 0; i rlt(n, 0);21 for (int i = 0; i 0 ... 阅读全文

leetcode - Recover Binary Search Tree

2013-12-15 21:02 by 张汉生, 180 阅读, 0 推荐, 收藏, 编辑
摘要:1 //比较巧妙的解决方法,copied from: 2 //http://blog.csdn.net/jellyyin/article/details/9018803 3 class Solution { 4 public: 5 void recoverTree(TreeNode *root) { 6 // Start typing your C/C++ solution below 7 // DO NOT write int main() function 8 TreeNode *n1=NULL; 9 TreeNod... 阅读全文