当前位置:网站首页>Spring 2021 daily question [week3 not finished]
Spring 2021 daily question [week3 not finished]
2022-06-11 17:59:00 【Hui Xiaoge】
Catalog
- 190. Invert binary bit
- 77. Flip word order
- 74. Search for a two-dimensional matrix
- 15. Search in a two-dimensional array 【 thinking Double pointer 】
- 90. A subset of II【dfs】
- 93. Recursive implementation of combinatorial enumeration
- 1006. Stupid factorials 【 Stack 】
- 1381. Factorial 【 thinking 】
- Interview questions 17.21. The amount of water in the histogram 【 Hang in the air 】
- 592. rain 【 Hang in the air 】
- 1143. Longest common subsequence 【DP】
- 1222. The password fell off 【 Hang in the air DP】
- 781. Rabbits in the forest 【 thinking 】
- 3192. The most frequent number of occurrences 【 Sign in 】
190. Invert binary bit

class Solution {
public:
uint32_t reverseBits(uint32_t n)
{
uint32_t sum=0;
for(int i=0;i<=31;i++)
{
if(n>>i&1) sum=sum*2+1;
else sum=sum*2;
}
return sum;
}
};
77. Flip word order

class Solution {
public:
string reverseWords(string s)
{
reverse(s.begin(),s.end());
stringstream l(s);
string ans,a;
while(l>>a)
{
reverse(a.begin(),a.end());
if(ans.size()) ans+=" ";
ans+=a;
}
return ans;
}
};
74. Search for a two-dimensional matrix

class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target)
{
int x=0,y=0;
int n=matrix.size();
int m=matrix[0].size();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
if(matrix[i][j]==target) return true;
}
return false;
}
};
15. Search in a two-dimensional array 【 thinking Double pointer 】

class Solution {
public:
bool searchArray(vector<vector<int>> array, int target) {
if(!array.size()) return false;
int n=array.size(),m=array[0].size();
int i=0,j=m-1;
while(i<n&&j>=0)
{
if(array[i][j]==target) return true;
else if(array[i][j]>target) j--;
else i++;
}
return false;
}
};
90. A subset of II【dfs】

class Solution {
public:
set<vector<int>>st;
vector<int>ve;
int a[105]={
0};
void dfs(int index,vector<int> nums)
{
if(index==nums.size())
{
st.insert(ve);
return;
}
ve.push_back(nums[index]);
dfs(index+1,nums);
ve.pop_back();
dfs(index+1,nums);
}
vector<vector<int>> subsetsWithDup(vector<int>& nums)
{
sort(nums.begin(),nums.end());
dfs(0,nums);
vector< vector<int> > ans;
for(auto i=st.begin();i!=st.end();i++)
ans.push_back(*i);
return ans;
}
};
93. Recursive implementation of combinatorial enumeration

#include<bits/stdc++.h>
using namespace std;
const int N=35;
int a[N],st[N],n,m;
void dfs(int s,int index)
{
if(index==m)
{
for(int i=0;i<index;i++) cout<<a[i]<<" ";
cout<<endl;
return;
}
for(int i=s;i<=n;i++)
{
if(!st[i])
{
st[i]=1;a[index]=i;
dfs(i,index+1);
st[i]=0;
}
}
}
int main(void)
{
cin>>n>>m;
dfs(1,0);
return 0;
}
1006. Stupid factorials 【 Stack 】

class Solution {
public:
int clumsy(int n)
{
if(n==1) return 1;
char s[10]="*/+-";
int k=0;
bool flag=true;
stack<int>st1; st1.push(n);
stack<int>st2;
for(int i=n-1;i>=1;i--)
{
if(flag)
{
if(st2.size())
{
int temp=st1.top()-st2.top();
st1.pop(),st2.pop();
st1.push(temp);
}
if(k==0)
{
int temp=st1.top()*i;
st1.pop(); st1.push(temp);
k++;
}else if(k==1)
{
int temp=st1.top()/i;
st1.pop(); st1.push(temp);
k++;
}else if(k==2)
{
int temp=st1.top()+i;
st1.pop(); st1.push(temp);
k++;
}
if(k==3) flag=!flag,k=0;
}
else
{
if(!st2.size())
{
st2.push(i);
continue;
}
if(k==0)
{
int temp=st2.top()*i;
st2.pop(); st2.push(temp);
k++;
}else if(k==1)
{
int temp=st2.top()/i;
st2.pop(); st2.push(temp);
k++;
}
if(k==2) flag=!flag;
}
}
int ans=st1.top();
if(st2.size()) ans-=st2.top();
return ans;
}
};
1381. Factorial 【 thinking 】

#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int sum=1,cnt2=0,cnt5=0,n;
cin>>n;
for(int i=1;i<=n;i++)
{
int x=i;
while(x%2==0) cnt2++,x/=2;
while(x%5==0) cnt5++,x/=5;
sum=sum*x%10;
}
for(int i=1;i<=cnt2-cnt5;i++) sum=sum*2%10;
cout<<sum;
return 0;
}
Interview questions 17.21. The amount of water in the histogram 【 Hang in the air 】

592. rain 【 Hang in the air 】

1143. Longest common subsequence 【DP】

class Solution {
public:
int longestCommonSubsequence(string text1, string text2)
{
int f[1010][1010]={
0};
int n=text1.size(),m=text2.size();
text1="0"+text1,text2="0"+text2;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
f[i][j]=max({
f[i-1][j-1],f[i-1][j],f[i][j-1]});
if(text1[i]==text2[j])
f[i][j]=max(f[i][j],f[i-1][j-1]+1);
}
}
return f[n][m];
}
};
1222. The password fell off 【 Hang in the air DP】

781. Rabbits in the forest 【 thinking 】

class Solution {
public:
int numRabbits(vector<int>& answers)
{
map<int,int>mp;
int sum=0;
for(int i=0;i<answers.size();i++) mp[answers[i]]++;
for(auto i=mp.begin();i!=mp.end();i++)
{
int a=i->first;
int b=i->second;
int temp=(b+a)/(a+1)*(a+1);
sum+=temp;
}
return sum;
}
};
3192. The most frequent number of occurrences 【 Sign in 】

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,x,a[N];
int main(void)
{
cin>>n;
for(int i=0;i<n;i++) cin>>x,a[x]++;
int ans=0,cnt=0;
for(int i=0;i<N;i++)
if(a[i]>cnt) cnt=a[i],ans=i;
cout<<ans;
return 0;
}
边栏推荐
- 夜神安装apk,以及bp代理
- 6-3 读文章(*)
- 送给大模型的「高考」卷:442人联名论文给大模型提出204个任务,谷歌领衔
- Hello go (XII). Go language common standard library II
- Difference between require and ES6 import
- 6-2 reverse output of multiple integers recursion
- R语言 mice包 Error in terms.formula(tmp, simplify = TRUE) : ExtractVars里的模型公式不对
- jsfinder,wafw00f安装,nmap配置(缺少msvcr120.dll文件)
- Chorus翻译
- Automated testing selenium
猜你喜欢

vulhub

Service学习笔记02-实战 startService 与bindService

Upload labs failed to pass the customs halfway and the middle road collapsed

Threejs uses indexeddb cache to load GLB model

Three steps of ffmpeg CBR precise bitstream control

有效的括号---2022/02/23

CLP information -5 keywords to see the development trend of the financial industry in 2022

网络安全威胁情报体系
![[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[collect first and use it sooner or later] 49 Flink high-frequency interview questions series (II)

括号生成---2022/02/25
随机推荐
【先收藏,早晚用得到】49个Flink高频面试题系列(二)
开源项目那么多,这次带你了解个版本的区别,明白alpha版、beta版、rc版是什么意思
Seeing the sudden death of a 28 year old employee, I was silent
光纤熔接知识汇总【转载自微信公众号弱电智能化工程2018】
adb 命令学习笔记
CLP information -5 keywords to see the development trend of the financial industry in 2022
6-1 从文件读取字符串(*)
[MySQL] detailed explanation of redo log, undo log and binlog (4)
Rtsp/onvif protocol easynvr video platform arm version cross compilation process and common error handling
Threejs uses indexeddb cache to load GLB model
Initial experience of MariaDB spider sharding engine
6-5 统计单词数量(文件)(*)
[MapReduce] a complete Mr program case teaches you how to package and run with idea
spawn ./ gradlew EACCES at Process. ChildProcess._ handle. onexit
"College entrance examination" volume sent to the big model: 442 people put forward 204 tasks to the big model, led by Google
GB gb28181 protocol video platform easygbs adds or deletes offline channels
关于元素位置和尺寸
Use exe4j to convert The jar file is packaged as Exe file
合并两个有序链表---2022/02/24
sqli-labs通关嘿嘿~