摘要:
Brute Force: 1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 if (!haystack) return -1; 5 if (!needle) ... 阅读全文
posted @ 2015-03-19 23:35
keepshuatishuati
阅读(141)
评论(0)
推荐(0)
摘要:
i ^ (i >> 1), that's the general format 1 class Solution { 2 public: 3 vector grayCode(int n) { 4 vector result; 5 for (int i = 0;... 阅读全文
posted @ 2015-03-19 23:20
keepshuatishuati
阅读(109)
评论(0)
推荐(0)
摘要:
Nothing fancy, just use recursive. 1 class Solution { 2 public: 3 void getP(vector &result, string current, int left, int right) { 4 if (l... 阅读全文
posted @ 2015-03-19 23:18
keepshuatishuati
阅读(153)
评论(0)
推荐(0)
摘要:
Not quite hard. Just remember initialize index to 0. Because if you initialize it as -1 and all the gas satisfy the cost, it will return -1.Actually, ... 阅读全文
posted @ 2015-03-19 23:15
keepshuatishuati
阅读(128)
评论(0)
推荐(0)
摘要:
Notes:1. When numerator is 0, return "0". Check this corner case, because 0 / -5 will return -0.2. Use long long int for divd and divs, mainly for div... 阅读全文
posted @ 2015-03-19 23:09
keepshuatishuati
阅读(131)
评论(0)
推荐(0)
摘要:
Logic is simple, but careful with the details. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left;... 阅读全文
posted @ 2015-03-19 10:09
keepshuatishuati
阅读(121)
评论(0)
推荐(0)
摘要:
Two methods:1. Use extra memory: 1 class Solution { 2 public: 3 int firstMissingPositive(int A[], int n) { 4 unordered_set sets; 5 ... 阅读全文
posted @ 2015-03-19 10:03
keepshuatishuati
阅读(157)
评论(0)
推荐(0)
摘要:
The ticky part for this question is :mid == 0 num[mid] num[mid] > num[mid+1] end = mid - 1;mid = l num[mid] 0 && num[mid] > num[mid - 1] end = mid... 阅读全文
posted @ 2015-03-19 09:55
keepshuatishuati
阅读(111)
评论(0)
推荐(0)
摘要:
lazy solutions..... Just skip the duplicates. Then worse case of time is O(n). 1 class Solution { 2 public: 3 int findMin(vector &num) { 4 ... 阅读全文
posted @ 2015-03-19 09:36
keepshuatishuati
阅读(96)
评论(0)
推荐(0)
摘要:
Be carefully with one element and two element sitution.1. Since mid = (start + end) / 2 is alway shifting to the left. So when we do comparision, not ... 阅读全文
posted @ 2015-03-19 09:30
keepshuatishuati
阅读(125)
评论(0)
推荐(0)
摘要:
Think about that.. n! = n * (n-1) * (n-2) ..... *1Zeros happened when each of them can be divided by 10. And 10 = 2 * 5. 2 is much more than 5 in the ... 阅读全文
posted @ 2015-03-19 09:16
keepshuatishuati
阅读(129)
评论(0)
推荐(0)
摘要:
Only trick is the 'Z'. Because when the number % 26 == 0, means there is a 'Z'. But when you n/=26, there is an extra 1 added to the n. EX:27 % 26 = 1... 阅读全文
posted @ 2015-03-19 09:11
keepshuatishuati
阅读(131)
评论(0)
推荐(0)
摘要:
No tricks. 1 class Solution { 2 public: 3 int titleToNumber(string s) { 4 int result = 0, len = s.size(); 5 for (int i = 0; i < le... 阅读全文
posted @ 2015-03-19 09:03
keepshuatishuati
阅读(126)
评论(0)
推荐(0)
摘要:
Just use a stack to record the numbers. And evey time you encounter a operator, pop two numbers, calucate it and push it back.Do not disorder the numb... 阅读全文
posted @ 2015-03-19 08:58
keepshuatishuati
阅读(165)
评论(0)
推荐(0)
摘要:
Still a DP problem.The transition function is not hard to get:1. When s[i-1] == t[j-1], which means we dont have to do any thing to handle the current... 阅读全文
posted @ 2015-03-19 08:49
keepshuatishuati
阅读(143)
评论(0)
推荐(0)
摘要:
Since it need the min value of initial health, this dp tracks from the back to the start.The trick is either this knight has 1 health or base on next ... 阅读全文
posted @ 2015-03-19 08:38
keepshuatishuati
阅读(146)
评论(0)
推荐(0)
摘要:
There couple of edge cases need to remember:1. The result, absolute value of dividend and divisor. Otherwise, when the record goes out of boundary, th... 阅读全文
posted @ 2015-03-19 08:00
keepshuatishuati
阅读(110)
评论(0)
推荐(0)
摘要:
This DP is a little bit tricky. You need to clear that:when S[i-1] == T[j-1], it has two part :1. dp[i-1][j-1], this means from 0 - j-1, it already ma... 阅读全文
posted @ 2015-03-19 07:44
keepshuatishuati
阅读(118)
评论(0)
推荐(0)
摘要:
Simple DP.Scanning from the end. If current is 0, then no decode way as "0", but it can be treated as "10", "20". So do another check whether it can h... 阅读全文
posted @ 2015-03-19 07:27
keepshuatishuati
阅读(131)
评论(0)
推荐(0)
摘要:
Pretty straight forward. count numbers and put char. 1 class Solution { 2 public: 3 string getNext(string s) { 4 ostringstream oss; 5 ... 阅读全文
posted @ 2015-03-19 07:20
keepshuatishuati
阅读(172)
评论(0)
推荐(0)
摘要:
Two methods for doing this problem:1. With extra memory, using hashtable:I made a mistake for mapping[runner->random] = mapping[runner]->random. Then ... 阅读全文
posted @ 2015-03-19 07:16
keepshuatishuati
阅读(165)
评论(0)
推荐(0)
摘要:
Similar to the sorted array. But we have to remember, the position of node not depends on the index. So we need do two more things:1. Ensure the point... 阅读全文
posted @ 2015-03-19 05:48
keepshuatishuati
阅读(138)
评论(0)
推荐(0)
摘要:
It is kind of binary search. Since this is a sorted array, we can treat it as inorder traversal. And we can define the root node for the Tree.So find ... 阅读全文
posted @ 2015-03-19 05:38
keepshuatishuati
阅读(117)
评论(0)
推荐(0)
摘要:
The minimum height controls the volumns. So let two runner at two ends start to scan the array. 1 class Solution { 2 public: 3 int maxArea(vector ... 阅读全文
posted @ 2015-03-19 05:34
keepshuatishuati
阅读(129)
评论(0)
推荐(0)
摘要:
Only different with preorder and postorder is that the root start from the beginning for preorder. 1 /** 2 * Definition for binary tree 3 * struct T... 阅读全文
posted @ 2015-03-19 05:30
keepshuatishuati
阅读(170)
评论(0)
推荐(0)
摘要:
For this problem just need to know the structure of two traversal.1. It is hard to find the root node in the inorder traversal but it is easy in posto... 阅读全文
posted @ 2015-03-19 05:27
keepshuatishuati
阅读(135)
评论(0)
推荐(0)
摘要:
Two notes:1. I dont know whether C++ has a good split function for STL as the JAVA. Need to figure it out.2. At the beginning, I tried to set tmp1 = t... 阅读全文
posted @ 2015-03-19 05:11
keepshuatishuati
阅读(129)
评论(0)
推荐(0)
摘要:
Same algorithm with combination. Input as the (1..n), constraint is that the length of each combine should be k. 1 class Solution { 2 public: 3 vo... 阅读全文
posted @ 2015-03-19 04:56
keepshuatishuati
阅读(111)
评论(0)
推荐(0)
摘要:
The different between I and II is that:1. For II, each number only can be chosen ONCE.2. The a number, in the array, encountered more than twice will ... 阅读全文
posted @ 2015-03-19 04:52
keepshuatishuati
阅读(143)
评论(0)
推荐(0)
摘要:
This is a classical combination question. 1 class Solution { 2 public: 3 void getComb(vector > &result, const vector &num, vector current, int sum... 阅读全文
posted @ 2015-03-19 04:39
keepshuatishuati
阅读(124)
评论(0)
推荐(0)
摘要:
1. Use BFS to search the graph.2. Create a hashtable to record the one to one mapping. 1 /** 2 * Definition for undirected graph. 3 * struct Undirec... 阅读全文
posted @ 2015-03-19 04:32
keepshuatishuati
阅读(136)
评论(0)
推荐(0)
浙公网安备 33010602011771号