摘要:
Brute Force: 1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 if (!haystack) return -1; 5 if (!needle) ... 阅读全文
摘要:
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;... 阅读全文
摘要:
Nothing fancy, just use recursive. 1 class Solution { 2 public: 3 void getP(vector &result, string current, int left, int right) { 4 if (l... 阅读全文
摘要:
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, ... 阅读全文
摘要:
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... 阅读全文
摘要:
Logic is simple, but careful with the details. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left;... 阅读全文
摘要:
Two methods:1. Use extra memory: 1 class Solution { 2 public: 3 int firstMissingPositive(int A[], int n) { 4 unordered_set sets; 5 ... 阅读全文
摘要:
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... 阅读全文
摘要:
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 ... 阅读全文
摘要:
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 ... 阅读全文
摘要:
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 ... 阅读全文
摘要:
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... 阅读全文
摘要:
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... 阅读全文
摘要:
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... 阅读全文
摘要:
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... 阅读全文
摘要:
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 ... 阅读全文
摘要:
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... 阅读全文
摘要:
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... 阅读全文
摘要:
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... 阅读全文
摘要:
Pretty straight forward. count numbers and put char. 1 class Solution { 2 public: 3 string getNext(string s) { 4 ostringstream oss; 5 ... 阅读全文