当前位置: 首页 > article >正文

1126刷题

1.3206. 交替组 I - 力扣(LeetCode)

class Solution {
public:
    int numberOfAlternatingGroups(vector<int>& colors) {
        int n = colors.size();
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (colors[i] != colors[(i - 1 + n) % n] &&
                colors[i] != colors[(i + 1 ) % n])
                ans++;
        }
        return ans;
    }
};

2.小F的永久代币卡回本计划 - MarsCode

v

#include <iostream>
using namespace std;

int solution(int a, int b) {
  // write code here
  int tmp = 0;
  if (a % b != 0)
    tmp = 1;
  return a / b + tmp;
}

int main() {
  cout << (solution(10, 1) == 10) << endl;
  cout << (solution(10, 2) == 5) << endl;
  cout << (solution(10, 3) == 4) << endl;
  return 0;
}

3.数组元素之和最小化 - MarsCode

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int solution(int n, int k) {
    int ans = 0,begin = k;
    while(n--){
        ans+=begin;
        begin+=k;
    }
    return ans;
}

int main() {
    std::cout << (solution(3, 1) == 6) << std::endl;
    std::cout << (solution(2, 2) == 6) << std::endl;
    std::cout << (solution(4, 3) == 30) << std::endl;
}

4.最大矩形面积问题 - MarsCode

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int solution(int n, std::vector<int> A) {
  // Edit your code here
  stack<int> s;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    while (!s.empty() && A[i] < A[s.top()]) {
      int a = A[s.top()];
      s.pop();
      int b = s.empty() ? i : i - s.top() - 1;
      ans = max(ans, a * b);
    }
    ans = max(ans, A[i]);
    s.push(i);
  }
  while (!s.empty()) {
    int a = A[s.top()];
    s.pop();
    int b = s.empty() ? n : n - s.top() - 1;
    ans = max(ans, a * b);
  }
  // cout << ans << endl;
  return ans;
}
// [7,8,19,10,6,22,10,5,2,14]
// 15,5,22,19,5,20,13,12,12
int main() {
  // Add your test cases here
  std::vector<int> A_case1 = std::vector<int>{1, 2, 3, 4, 5};
  std::cout << (solution(5, A_case1) == 9) << std::endl;
  std::vector<int> B_case1 = std::vector<int>{5, 4, 3, 2, 1, 6};
  std::cout << (solution(6, B_case1)) << std::endl;
  std::vector<int> C_case1 = std::vector<int>{4, 4, 4, 4};
  std::cout << (solution(4, C_case1)) << std::endl;
  std::vector<int> D_case1 = std::vector<int>{15, 5, 22, 19, 5, 20, 13, 12, 12};
  std::cout << (solution(9, D_case1)) << std::endl;
  return 0;
}

5.比赛配对问题 - MarsCode

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int solution(int n) {
    // write code here
    int ans = 0;
    while(n != 1){
        ans += n/2;
        n = n -n/2;
    }
    return ans;
}

int main() {
    cout << (solution(7) == 6) << endl;
    cout << (solution(14) == 13) << endl;
    cout << (solution(1) == 0) << endl;

    return 0;
}

6.DNA序列编辑距离 - MarsCode

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int solution(std::string dna1, std::string dna2) {
  // Please write your code here
  int n1 = dna1.size(), n2 = dna2.size();
  vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 1000000));
  for (int i = 0; i <= n1; i++)
    dp[i][0] = i;
  for (int i = 0; i <= n2; i++)
    dp[0][i] = i;
  for (int i = 0; i < n1; i++) {
    for (int j = 0; j < n2; j++) {
      dp[i + 1][j + 1] = min(dp[i][j] + (dna1[i] != dna2[j]),
                             min(dp[i + 1][j] + 1, dp[i][j + 1] + 1));
      // if(dna1[i] != dna2[j])
      //     dp[i + 1][j + 1]++;
      // cout << dp[i + 1][j + 1] << ' ';
    }
    // cout << endl;
  }
  return dp[n1][n2];
}

int main() {
  //  You can add more test cases here
  std::cout << (solution("AGT", "AGCT")) << std::endl;
  // std::cout << (solution("AACCGGTT", "AACCTTGG")) << std::endl;
  // std::cout << (solution("ACGT", "TGC")) << std::endl;
  std::cout << (solution("CCCCACTGGAAGTCCTCCTAT", "T")) << std::endl; // 20
  return 0;
}

7组成字符串ku的最大次数 - MarsCode

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int solution(const string& s) {
    int k = 0,u = 0;
    for(auto e : s){
        if(e == 'k'|| e == 'K')
            k++;
        else if(e == 'u'|| e == 'U')
            u++;
    }
    return min(k,u);
}

int main() {
    cout << (solution("AUBTMKAxfuu") == 1) << endl;
    cout << (solution("KKuuUuUuKKKKkkkkKK") == 6) << endl;
    cout << (solution("abcdefgh") == 0) << endl;
}

8.游戏排名第三大的分数 - MarsCode

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool fun(int a,int b){
    return a > b;
}
int solution(int n, std::vector<int> nums) {
    sort(nums.begin(),nums.end(),fun);
    int pos = 0;
    // cout << nums[0] <<endl;
    for(int i = 1;i < n;i++){
        if(nums[i] != nums[i - 1])
            pos++;
        if(pos == 2)
            return nums[i];
    }
    return nums[0];
}

int main() {
    std::cout << (solution(3, {3, 2, 1})) << std::endl;
    std::cout << (solution(2, {1, 2})) << std::endl;
    std::cout << (solution(4, {2, 2, 3, 1})) << std::endl;
    return 0;
}

9完美偶数计数 - MarsCode

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int solution(int n, int l, int r, vector<int>& a) {
    int ans = 0;
    for(auto e : a){
        if(e%2 == 0 && e>= l &&e <= r)
            ans++;
    }
    return ans;
}

int main() {
    vector<int> a1 = {1, 2, 6, 8, 7};
    cout << (solution(5, 3, 8, a1) == 2) << endl;

    vector<int> a2 = {12, 15, 18, 9};
    cout << (solution(4, 10, 20, a2) == 2) << endl;

    vector<int> a3 = {2, 4, 6};
    cout << (solution(3, 1, 10, a3) == 3) << endl;
}

10RGB色值转换为整数值 - MarsCode

#include <iostream>
#include <string>
using namespace std;
int solution(std::string rgb) {
    // Please write your code here
    auto pos0 = rgb.find('(');
    auto pos1 = rgb.find(',',pos0);
    auto pos2 = rgb.find(',',pos1 + 1);
    auto pos3 = rgb.find(')',pos2);
    int a = stoi(rgb.substr(pos0 + 1,pos1 - 1));
    int b = stoi(rgb.substr(pos1 + 2,pos2 - 1));
    int c = stoi(rgb.substr(pos2 + 2,pos3 - 1));
    // cout <<a <<' '<<b <<' '<<c<<endl;
    return a*256*256+b*256+c;
}

int main() {
    //  You can add more test cases here
    std::cout << (solution("rgb(192, 192, 192)") == 12632256) << std::endl;
    std::cout << (solution("rgb(100, 0, 252)") == 6553852) << std::endl;
    std::cout << (solution("rgb(33, 44, 55)") == 2174007) << std::endl;
    return 0;
}


http://www.kler.cn/a/411307.html

相关文章:

  • springboot获取配置文件中的值
  • 解决JWT解析CDN不稳定问题
  • 浏览器缓存与协商缓存
  • svn 崩溃、 cleanup失败 怎么办
  • Large Spatial Model:End-to-end Unposed Images to Semantic 3D 论文解读
  • docker pull命令拉取镜像失败的解决方案
  • 堆的实现(完全注释版本)
  • pikachu文件上传漏洞通关详解
  • 利用 Vue 组合式 API 与 requestAnimationFrame 优化大量元素渲染
  • Paddle Inference部署推理(一)
  • QT-installEventFilter
  • GaussDB高智能--库内AI引擎:模型管理数据集管理
  • 蓝桥杯c++算法秒杀【6】之动态规划【下】(数字三角形、砝码称重(背包问题)、括号序列、异或三角:::非常典型的必刷例题!!!)
  • 前端 Vue 3 后端 Node.js 和Express 结合cursor常见提示词结构
  • C语言解析命令行参数
  • xiaolin coding 图解网络笔记——TCP篇
  • 2686694 - 操作方法:MSEG - DBSQL_REDIRECT_INCONSISTENCY
  • 道路机器人识别交通灯,马路,左右转,黄线,人行道,机器人等路面导航标志识别-使用YOLO标记
  • Python毕业设计选题:基于django+vue的期货交易模拟系统的设计与实现
  • PyTorch3
  • SD-WAN加速是怎么实现的?
  • MODBUS规约的秘密之五-----如何用C++编写MODBUS规约
  • 基于物联网技术的智能家居安全监控体系构建
  • 利用HTML5和CSS来实现一个漂亮的表格样式
  • Spring Boot——统一功能处理
  • Rust学习笔记_01——基础