1.力扣
谢谢这题给我往下做的信心🌹
思路:看代码👍
class Solution {
public:
int smallestEvenMultiple(int n) {
if(n%2==0)return n;
else return n*2;
}
};
2.力扣
遍历字符串,记录每个符合条件的子字符串,再比较大小即可
class Solution {
public:
int longestContinuousSubstring(string s) {
int ans=0,res=0;
int n=s.size();
for(int i=0;i<n-1;i++){
if(s[i]==s[i+1]-1)res++;
else res=0;
ans=max(ans,res);
}
return ans+1;
}
};
3.力扣
树左右同时进行dfs,遍历点具有对称性
左子树的左子树节点和右子树的右子节点对称,同时遍历的时候进行交换
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
// 遍历对称的二叉树,交换值
void dfs(TreeNode* &left,TreeNode* &right,int k)
{
if(!left)
return;
if(k%2==1)
{
int l=left->val;
left->val=right->val;
right->val=l;
}
dfs(left->left,right->right,k+1);
dfs(left->right,right->left,k+1);
}
TreeNode* reverseOddLevels(TreeNode* root) {
dfs(root->left,root->right,1);
return root;
}
};
因为前两题写出来膨胀了,看到这题想都没想就去做,然后就做不出来了
二叉树这块还得再看看
求出x和y的距离,再求甲乙每秒移动的速度,若可整除,输出c/(a+b),否则输出-1
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
int n;
cin>>n;
while(n--){
int x,y,a,b;
cin>>x>>y>>a>>b;
int c=abs(y-x);
if(c%(a+b)==0){
cout<<c/(a+b)<<endl;
}
else puts("-1");
}
}
现在有a艘,至少击沉1搜====最多可以摆放(a-1)艘,每个区间,构造时,每一段攻击第b个位置。
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, a, b, k;
cin >> n >> a >> b >> k;
string s;
cin >> s;
int cnt = 0;
int winLen = 0;
for (int i = 0; i < n; i ++){
if (s[i] == '1'){
winLen = 0;
}else{
winLen ++;
if (winLen == b){
cnt ++;
winLen = 0;
}
}
}
int need = cnt - (a - 1);
cout << need << endl;
winLen = 0;
for (int i = 0; i < n; i ++){
if (s[i] == '1'){
winLen = 0;
}else{
winLen ++;
if (winLen == b){
cout << i + 1 << ' ';
winLen = 0;
need --;
if (need == 0){
break;
}
}
}
}
cout << endl;
return 0;
}