力扣 模拟专题
263. 丑数
https://leetcode-cn.com/problems/ugly-number/
class Solution {
public:
bool isUgly(int n) {
int f4=0;
if(n==1) return true;
if(n==0) return false;
while(n!=1){
int f1=0,f2=0,f3=0;
if(n%2==0){
n/=2;
f1=1;
continue;
}
else if(n%3==0){
n/=3;
f2=1;
continue;
}
else if(n%5==0){
n/=5;
f3=1;
continue;
}
if(!f1 && !f2 && !f3){
f4=1;
break;
}
}
if(f4) return false;
else return true;
}
};
67. 二进制求和
https://leetcode-cn.com/problems/add-binary/
class Solution {
public:
string addBinary(string a, string b) {
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int t=0;
string c;
for(int i=0;i<a.size() || i<b.size();i++){
int va=i>=a.size() ?0:a[i]-'0'; //如果a已经用完了就默认0
int vb=i>=b.size() ?0:b[i]-'0';
int temp=va+vb+t;
t=temp/2;
temp%=2;
c+=to_string(temp);
}
if(t)//有进位
c+='1';
reverse(c.begin(),c.end());
return c;
}
};
504. 七进制数
https://leetcode-cn.com/problems/base-7/
class Solution {
public:
string convertToBase7(int num) {
bool isFu=false;
if(num<0){
isFu=true;
num*=-1;
}
if(!num) return "0";
string res;
while(num){
res+=to_string(num%7);
num/=7;
}
if(isFu) res+="-";
reverse(res.begin(),res.end());
return res;
}
};
54. 螺旋矩阵
https://leetcode-cn.com/problems/spiral-matrix/
class Solution {
public:
int vis[105][105]={0};
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int>ans;
vis[0][0]=1;
ans.push_back(matrix[0][0]);
int n=matrix.size(),m=matrix[0].size();
int temp=1,N=n*m;
int dir=0;
int ox=0,oy=0;
while(temp<N){
int nx=ox+dx[dir],ny=oy+dy[dir];
if(nx>=0 && nx<n && ny>=0 && ny<m && !vis[nx][ny]){
vis[nx][ny]=1;
ans.push_back(matrix[nx][ny]);
ox=nx;
oy=ny;
temp++;
}
else {
dir+=1;
dir%=4;
}
}
return ans;
}
};
24. 两两交换链表中的节点
https://leetcode-cn.com/problems/swap-nodes-in-pairs/
链表题主要靠画图
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
auto dummy=new ListNode();
dummy->next=head;
auto p=dummy;
while(p && p->next && p->next->next){
auto a=p->next;
auto b=p->next->next;
p->next=b;
a->next=b->next;
b->next=a;
p=a;
}
return dummy->next;
}
};
481. 神奇字符串
https://leetcode-cn.com/problems/magical-string/
class Solution {
public:
int magicalString(int n) {
string s="122";
for(int i=2,k=1;i<n;i++,k=3-k){
for(int j=0;j<s[i]-'0';j++){
s+=to_string(k);
}
}
int res=0;
for(int i=0;i<n;i++){
res+=s[i]=='1';
}
return res;
}
};
71. 简化路径(字符串模拟)
https://leetcode-cn.com/problems/simplify-path/
class Solution {
public:
string simplifyPath(string path) {
path+='/';//处理边界情况
string res,s;
for(auto c:path){
if(res.empty()) res+=c;
else if(c!='/'){
s+=c;
// cout<<s<<endl;
}
else {
if(s==".."){
if(res.size()>1){//存在上一级目录
res.pop_back();//删除尾巴的'/'
while(res.back()!='/') res.pop_back();//一直删除到开头的'/'
}
}
else if(s!="." && s!=""){
//不属于上面两种情况就加入
res+=s;
res+='/';
}
s="";//清空
}
}
if(res.size()>1) res.pop_back();
return res;
}
};
12. 整数转罗马数字
class Solution {
public:
string intToRoman(int num) {
int values[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
string reps[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
string res;
for(int i=0;i<13;i++){
while(num>=values[i]){
num-=values[i];
res+=reps[i];
}
}
return res;
}
};

浙公网安备 33010602011771号