2020-09-21
强整数
class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
long long tmp;
vector<int> nums;
for(int i = 0; i*i < bound; i++) {
for(int j = 0; j*j < bound; j++) {
if(pow(x,i) > bound || pow(y,j) > bound) break;
tmp = pow(x,i) + pow(y,j);
if(tmp > bound) break;
nums.push_back(tmp);
}
}
//去重
set<int> st(nums.begin(),nums.end());
nums.assign(st.begin(),st.end());
return nums;
}
};
Excel表列名称
class Solution {
public:
string convertToTitle(int n) {
string result = "";
while(n) {
n-=1;
result += char(n%26+'A');
n/=26;
}
reverse(result.begin(),result.end());
return result;
}
};
范围求和 II
class Solution {
public:
int maxCount(int m, int n, vector<vector<int>>& ops) {
if (ops.empty())
return m * n;
int a = INT_MAX, b = INT_MAX;
for (int i = 0; i < ops.size(); i++) {
a = min(a, ops[i][0]);
b = min(b, ops[i][1]);
}
return a * b;
}
};
2020-09-22
快乐数
class Solution {
public:
bool isHappy(int n) {
while(n >= 10) {
n = Divide(n);
}
if(n == 1|| n == 7) return true;
return false;
}
int Divide(int n) {
int result = 0;
while(n) {
result+=(n%10)*(n%10);
n/=10;
}
return result;
}
};
三维形体投影面积
class Solution {
public:
int projectionArea(vector<vector<int>>& grid) {
int res = 0;
vector<int> nums;
vector<int> nums2;
for(int i = 0; i < grid.size(); i++) {
for(int j = 0; j < grid[i].size(); j++) {
if(grid[i][j] != 0) res++;
nums.push_back(grid[j][i]);
nums2.push_back(grid[i][j]);
}
sort(nums.begin(),nums.end());
sort(nums2.begin(),nums2.end());
res += (nums[nums.size()-1]+nums2[nums2.size()-1]);
nums.clear();
nums2.clear();
}
return res;
}
};
三维形体的表面积
class Solution {
public:
int surfaceArea(vector<vector<int>>& grid) {
int res = 0;
for(int i = 0; i < grid.size(); i++) {
for(int j = 0; j < grid[i].size(); j++) {
if(grid[i][j] != 0) res += grid[i][j]*4+2;
if(i>0) res -= min(grid[i-1][j],grid[i][j])*2;
if(j>0) res -= min(grid[i][j-1], grid[i][j])*2;
}
}
return res;
}
};
2020-09-23 (递归)
斐波那契数列
class Solution {
public:
int fib(int n) {
// if(n>2) return fib(n-1)+fib(n-2); // 超出时间限制
// else return n;
int res;
int a = 0;
int b = 1;
if(n == 0 || n == 1) return n;
for(int i = 2; i <= n; i++) {
res = a%(1000000007)+ b%(1000000007);
a = b;
b = res;
}
return res%(1000000007);
}
};
青蛙跳台阶问题
class Solution {
public:
int numWays(int n) {
int res;
int a = 1;
int b = 1;
if(n == 0 || n == 1) return 1;
for(int i = 2; i <= n; i++) {
res = a%(1000000007)+ b%(1000000007);
a = b;
b = res;
}
return res%(1000000007);
}
};
第 N 个泰波那契数
class Solution {
public:
int tribonacci(int n) {
// // 从34开始超出时间限制
// if(n == 0) return 0;
// else if(n == 1 || n == 2) return 1;
// else return tribonacci(n-1)+tribonacci(n-2)+tribonacci(n-3);
int t0 = 0;
int t1 = 1;
int t2 = 1;
long long answer;
if(n == 0) return 0;
else if(n == 1 || n == 2) return 1;
else {
for(int i = 3; i<= n; i++) {
answer = t0+t1+t2;
t0 = t1;
t1 = t2;
t2 = answer;
}
}
return answer;
}
};
汉诺塔问题
class Solution {
public:
void move(int n, vector<int>& A, vector<int>& B, vector<int>& C){
if (n == 1){
C.push_back(A.back());
A.pop_back();
return;
}
move(n-1, A, C, B); // 将A上面n-1个通过C移到B
C.push_back(A.back()); // 将A最后一个移到C
A.pop_back(); // 这时,A空了
move(n-1, B, A, C); // 将B上面n-1个通过空的A移到C
}
void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
int n = A.size();
move(n, A, B, C);
}
};
2020-09-24(位运算)
数组异或操作
class Solution {
public:
int xorOperation(int n, int start) {
int res = start;
int tmp;
for(int i = 1;i < n; i++) {
tmp = start+2*i;
res ^= tmp;
}
return res;
}
};
数字转换为十六进制数
class Solution {
public:
string toHex(int num) {
unsigned int nums = num;
if(num == 0) return "0";
string s = "";
char arr[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
while(nums!=0) {
int tmp = nums&15;
s+=arr[tmp];
nums >>= 4;
}
reverse(s.begin(),s.end());
return s;
}
};
2020-09-25
插入
class Solution {
public:
int insertBits(int N, int M, int i, int j) {
int tmp = (1<<j-i+1)-1<<i;
return (~tmp&N)|(M<<i);
}
};
两整数之和
class Solution {
public:
int getSum(int a, int b) {
return (a^b)+((unsigned int)(a&b)<<1);
}
};
位1的个数 二进制中1的个数
class Solution {
public:
int hammingWeight(uint32_t n) {
int result = 0;
while(n) {
if(n&1){
result++;
}
n>>=1;
}
return result;
}
};
汉明距离
class Solution {
public:
int hammingDistance(int x, int y) {
//异或后看有多少个1;
int tmp = x^y;
int count = 0;
while(tmp) {
if(tmp&1) count++;
tmp>>=1;
}
return count;
}
};
将数字变成 0 的操作次数
class Solution {
public:
int numberOfSteps (int num) {
int count = 0;
while(num) {
if(num%2 == 0) num/=2;
else num -= 1;
count++;
if(num == 0) break;
}
return count;
}
};
2020-09-26
4的幂
class Solution {
public:
bool isPowerOfFour(int num) {
if(num == 1) return true;
unsigned int n = num;
int count1 = 0;
int count2 = 0;
while(n) {
if(n&1) count1++;
else count2++;
n>>=1;
}
if(count2 != 0 && count2%2 == 0 && count1 == 1) return true;
return false;
}
};
不用加号的加法
class Solution {
public:
int add(int a, int b) {
int tmp=1;
int res;
while(tmp) {
res = a^b;
tmp = ((unsigned int)a&b)<<1;
a = res;
b = tmp;
}
return res;
}
};
配对交换
class Solution {
public:
int exchangeBits(int num) {
return ((num & 0x55555555)<<1)|((num & 0xaaaaaaaa)>>1);
}
};
整数转换
class Solution {
public:
int convertInteger(int A, int B) {
unsigned int n = A^B;
int count = 0;
while(n) {
if(n&1) count++;
n>>=1;
}
return count;
}
};
交替位二进制数
class Solution {
public:
bool hasAlternatingBits(int n) {
int flag = 0;
int tmp = 0;
while(n) {
if(n&1) {
if(flag) return false;
flag = 1;
tmp = 1;
}else{
if(!flag&&tmp == 1) return false;
flag = 0;
tmp = 1;
}
n>>=1;
}
return true;
}
};
2020-09-27
颠倒二进制位
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t s = 0;
for(int i = 0; i < 32; i++){
s<<=1;
s|=(n&1);
n>>=1;
}
return s;
}
};
二进制表示中质数个计算置位
class Solution {
public:
int countPrimes(int n) {
int num = 0;
if(n == 0 || n == 1 || n == 2) {
return 0;
}
for(int i = 3; i < n; i+=2) {
for(int j = 3; j <= sqrt(i); j+=2) {
if(i%j == 0) {
num++;
break;
}
}
}
return n/2-num;
}
};
翻转数位
class Solution {
public:
int reverseBits(int num) {
if(num == -1) return 32;
int ans = 0;
int pre = 0, cut = 0;
unsigned int n = num;
if(num == 0) return 1;
while(n) {
if(n&1) cut++;
else {
ans = ans > (pre+cut+1)?ans:(pre+cut+1);
pre = cut;
cut = 0;
}
n >>= 1;
}
ans = ans > (pre+cut+1)?ans:(pre+cut+1);
return ans;
}
};