突然一下子就二月份了,休息了三天,三天都只是写了每日一题。
一、记一下这三天的每日一题
class Solution {
public:
int numberOfSteps(int num) {
int ans=0;
while(num)
{
if(num%2==0) num/=2;
else num-=1;
ans++;
}
return ans;
}
};
class Solution {
public:
string longestNiceSubstring(string s) {
int n=s.size();
int maxPos = 0;
int maxLen = 0;
for (int i = 0; i < n; ++i) {
int lower = 0;
int upper = 0;
for (int j = i; j < n; ++j) {
if (islower(s[j])) {
lower |= 1 << (s[j] - 'a');
} else {
upper |= 1 << (s[j] - 'A');
}
if (lower == upper && j - i + 1 > maxLen) {
maxPos = i;
maxLen = j - i + 1;
}
}
}
return s.substr(maxPos, maxLen);
}
};
#include<algorithm>
using namespace std;
class Solution {
public:
string reversePrefix(string word, char ch) {
if(word.find(ch)!=string::npos)
{
reverse(word.begin(),word.begin()+word.find(ch)+1);
}
return word;
}
};
class Solution {
public:
int findMinFibonacciNumbers(int k) {
vector<int> nums;
int sum=2;
if(k==1) return 1;
nums.push_back(1);
int a=1,b=1;
while(a+b<=k)
{
int c=a+b;
nums.push_back(c);
a=b;
b=c;
}
int ans=0;
for(int i=nums.size()-1;i>=0&&k>0;i--)
{
int num=nums[i];
if(k>=num)
{
k-=num;
ans++;
}
}
return ans;
}
};
二、递归问题
记录一下遇到的问题
这个问题解法很暴力,枚举1-9九个数字的全排列,然后枚举每个数字。
#include <iostream>
using namespace std;
const int N=1000010;
int n;
bool st[10];
int cnt;
int way[10];
int cal(int l, int r) {
int res = 0;
for (int i = l; i <= r; i++) {
res = res * 10 + way[i];
}
return res;
}
void dfs(int u)
{
if(u==9)
{
for(int i=0;i<7;i++)
{
for(int j=i+1;j<8;j++)
{
int a=cal(0,i);
int b = cal(i+1,j);
int c=cal(j+1,8);
if(a*c + b == n*c) cnt++;
}
}
return;
}
for(int i=1;i<=9;i++)
{
if(!st[i])
{
st[i] = true;
way[u] = i;
dfs(u+1);
st[i] = false;
}
}
}
int main()
{
cin>>n;
dfs(0);
cout<<cnt;
}