题型总结(按难易程度排列)
1.对整数直接进行操作(如反转,比较)
2.算数运算题(如乘除,开方)三个主要运用的办法:二分法、牛顿法、位移法
3.几何解析题
对整数直接进行操作
263 Ugly Number
感觉很简单啊,小心0,还有这道题好像并不需要怎么想负数。剩下的看code,肯定能看懂了。
class Solution {
public:
bool isUgly(int num) {
if(num<=0) return false;
while(num%2==0){
num/=2;
}
while(num%3==0){
num/=3;
}
while(num%5==0){
num/=5;
}
if(num!=1) return false;
return true;
}
};
264 Ugly Number II
看似跟前面一道题很像,但是相同之处只有运用了相同的ugly number的概念,这道题用DP解。
算法:ugly numbers 可以分为三个子集:2的倍数,3的倍数,5的倍数
for example:
看完这个之后可以停下来想想怎么解~ (那怎么样才可以把它们合并到一起呢?)
最终数列第一个element永远都是1,下一个element在三个子集中选。每当最终数列的一个新element出现后三个子集个出现一个新数,最终数列的下一个element在每个子集的最小数中选。个人觉得看code来的更直接:
int m = min(min(res[t1]*2,res[t2]*3),res[t3]*5);
其中res是最终数列,m是最终数列的下一个element,t1,t2,t3是三个子集的pointer。
注意事项:1)选定下一个element之后,不要忘记移动pointer 2)如果新的element跟之前加入最终数列的数相同,忽略这次新的element
class Solution {
public:
int nthUglyNumber(int n) {
if(n<=0) return -1;
int t1=0,t2=0,t3=0;
vector<int> res;
res.push_back(1);
while(res.size()<n){
int m = min(min(res[t1]*2,res[t2]*3),res[t3]*5);
if(m==res[t1]*2) t1++;
else if(m==res[t2]*3) t2++;
else t3++;
if(res[res.size()-1]!=m)
res.push_back(m);
}
return res[n-1];
}
};
258 Add Digits
算法:1)利用stack将各个位数上的数字存起来,来完成题目中提到的运算
2)找规律:0是0,9的倍数是9,其余都是mod 9
class Solution {
public:
int addDigits(int num) {
if(num==0) return 0;
if(num%9==0) return 9;
else return num%9;
}
};
将规律综合起来
class Solution {
public:
int addDigits(int num) {
return 1+(num-1)%9;
}
};
67 Add Binary
算法:感觉并没有什么特殊,按照平常计算二进制的方法来就行了
注意事项:1)while loop的条件为string a pointer & string b pointer >= 0 还有 c==1 (不可以assume string a & b 等长)
2)easiest ever to convert a char to a num:char - '0'; convert a num to a string: to_string(int)
题外话:这道题要是变成 string addBinary(string a, string b, string c) 可以用recursion来做
class Solution {
public:
string addBinary(string a, string b) {
int ap=a.size()-1,bp=b.size()-1,c=0;
string res="";
while(ap>=0 || bp>=0 || c==1){
//see if the number exists
int aa = ap>=0 ? a[ap--]-'0' : 0;
int bb = bp>=0 ? b[bp--]-'0' : 0;
int sum = aa+bb+c;
res = to_string(sum%2) + res;
c = sum>1 ? 1:0;
}
return res;
}
};
Reference:
http://www.cnblogs.com/grandyang/p/4084971.html
172 Factorial Trailing Zeros
题目解释:n的factorial里有多少个0
算法: 1)有多少个0跟factorial里有多少个2和5有关
2)5的数量永远比2的数量多,所以题目就变成了factorial里面有几个5
a. 用一个for loop, 每5个jump一次,算有几个5 O(n/5)
b.不断的除以5,将得到的数相加。一次除以5是factorial里至少有1个5的数的个数和,再除一次5是factorial里至少 有2个5的数的个数和,以此类推我们可以得到一共5在factorial里的个数。 O(log_5(n))
class Solution {
public:
int trailingZeroes(int n) {
int countF = 0;
while(n/5!=0){
countF += n/5;
n /= 5;
}
return countF;
}
};
9 Palindrome Number
题目special:no extra space (所以把int convert to string行不通)
算法:不断用mod和/得到第一位还有最后一位进行比较
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) return false;
if(x<10) return true;
int largeD = 1;
while(largeD<=x/10){
largeD *= 10;
}
int smallM = 10;
while(largeD>1){
int left = x/largeD;
int right = x%smallM;
if(left!=right){
return false;
}
else{
x-=left*largeD;
x/=10;
largeD /= 100;
}
}
return true;
}
};
8 String to Integer (atoi)
一些需要特殊处理的地方: 1)移除开头的空格 2)正负号 3)overflow (use long to store data first)
useful funciton: find_first_not_of()
class Solution {
public:
int myAtoi(string str) {
long res = 0;
int s = 1;
int i = str.find_first_not_of(' ');
if(str[i] == '-' || str[i] == '+'){
s = str[i++] == '+' ? 1:-1;
}
while(isdigit(str[i])){
res = res*10 + (str[i++]-'0');
if(res>INT_MAX) return s==1 ? INT_MAX : INT_MIN;
}
return s*res;
}
};
疑问:为什么不需要keeping checking i<str.size()? isdigit()包括了check bound
204 Count Primes
算法:1)bruce force 利用prime的概念:对于每个小于n的数,检验是否能被比他小的数整除,若不能就是prime
2)建立一个array,index表示数,value at the index表示是否为prime;从2开始到N-1,将之后自己的倍数设为非prime
优化a. 4的倍数也还是2的倍数,已经被设置为非prime的数不需要重新设置它的的倍数也为非prime
优化b. 只需要loop到sqrt(n), 因为n=pq=qp
其实我们这里用了一个叫sieve of eratosthenes的算法
class Solution {
public:
int countPrimes(int n) {
if(n<=2) return 0;
int res = 0;
vector<int> prime(n,1);
int i;
for(i=0;i<sqrt(prime.size());i++){
if(i==0||i==1) prime[i] = 0;
if(prime[i]==1){
res++;
int count = 2;
int index = count*i;
while(index<prime.size()){
prime[index] = 0;
count++;
index = count*i;
}
}
}
for(;i<prime.size();i++){
if(prime[i]==1) res++;
}
return res;
}
};
7 Reverse Integer
算法:可以用纯数学手段来解决->%10得到末位数,/10消去一位
重新组合:(本题用到的)从高到低 res = res*10 + x*10
(拓展)从低到高 res = x*count + res (需要不断increment count)
class Solution {
public:
int reverse(int x) {
long res = 0;
while(x!=0){
res = res*10 + x%10;
if(res>INT_MAX||res<INT_MIN) return 0;
x /= 10;
}
return int(res);
}
};
算数运算题
A minus B
29 Divide Two Integers
算法:
因为题目给出的条件是不可以用乘除还有mod, 我们便可以想到用加减来代替它们。如果我们单纯的用减法,那就是dividend不断地减去divisor直到下一次(dividend-divisor)不再>=0, 我们可以用一个variable来count它的个数。这个算法需要O(n)。
为了提高效率,我们可以加上bit manipulation, divisor<<1 相当于把 divisor*2,第一轮我们可以看dividend最多有多少个 divisor*2^k, 第二轮我们再看,dividend-divisor*2^k最多有几个divisor*2^k,最后再按照之前提到的算法进行运算。最后把这些都加起来就行。
两点需要注意:
1.overflow:可以用long long来解决大的数字,但是因为return的是个int, 还需要额外考虑return的那个数字overflow的情况(-2147483648/-1应该等于2147483648,但是最大的int是2147483647,按照题意return INT_MAX就行)
note: 在overflow的情况下,abs(INT_MIN) = INT_MIN
2.handle the sign: 可以用一个bool或者int来记忆一下符号; 这行code感觉还不错:int sign = (dividend>=0)^(divisor>=0)?-1:1;
class Solution {
public:
int divide(int dividend, int divisor) {
if(dividend==INT_MIN && divisor==-1) return INT_MAX;
int sign = (dividend>=0)^(divisor>=0) ? -1:1;
long long did = labs(dividend);
long long dis = labs(divisor);
int res = 0;
while((did-dis)>=0){
long long multi=1, temp = dis;
while(did>=(temp<<1)){
temp <<= 1;
multi <<= 1;
}
did -= temp;
res += multi;
}
return sign==1 ? res:-res;
}
};
(看之后时间剩余,可能会加上关于code的详解)
Reference:
http://blog.csdn.net/linhuanmars/article/details/20024907
https://leetcode.com/problems/divide-two-integers/discuss/13407/Detailed-Explained-8ms-C++-solution
Multiply Two Integers
感觉答案太trivial就不讲了
A plus B
算法: use bit manipluation(XOR - sum of two numbers; AND - the carry)
A Minus B
算法:变成加法 (a+(-b))
总结
division and multiplication: 我们可以把它们转化为加减法(loop或者recurison),并用bit manipulation加快进度。
addition and subtraction: 加法可以用bit manipulaiton来代替,减法可以用加法来代替。
这跟我们当时学四则运算的角度,不谋而合。
69 Sqrt(x)
之后会详细讲binary search,which makes me crazy!!!
几何解析题
本系列按照此链接展开:https://www.gitbook.com/book/lefttree/leetcode-categories/details
使用语言C++,之后可能更新Java