10.10 LeetCode 3285 3280 3274

目录

  • [leetcode 3285](#leetcode 3285)
  • [leetcode 3280](#leetcode 3280)
  • [leetcode 3274](#leetcode 3274)

leetcode 3285


cpp 复制代码
class Solution {
public:
    vector<int> stableMountains(vector<int>& height, int threshold) {
        vector<int> ans;
        for(int i=1; i<height.size(); i++){
            if(height[i-1] > threshold){  //判断下标为 i 山是否稳定
                ans.push_back(i);
            }
        }
        return ans;
    }
};

leetcode 3280


思路如下:

先定义转化为二进制的函数

进行切割前,先找到切割位置,即 "-" 的位置,记录下来

切割字符串,年月日

将字符串类型转化为数字类型

cpp 复制代码
class Solution {
public:

    string to_binary(int x){
        stringstream res;  // 可变长字符串

        while(x){
            res << char((x % 2) + '0');   //加 '0' 变成字符  (int + char = int) 强制转换
            x = x / 2;
        }

        string r = res.str();
        reverse(r.begin(), r.end());

        return r;
    }

    string convertDateToBinary(string date) {
        string year = "";
        string month = "";
        string day = "";
        int n = date.length();
        vector<int> pos;   // 动态数组:记录 "-" 的位置
        for(int i=0; i<n; i++){
            if(date[i] == '-'){
                pos.push_back(i);
            }
        }

        year = date.substr(0, pos[0]);
        month = date.substr(pos[0] + 1, pos[1] - pos[0] -1);
        day = date.substr(pos[1] + 1, n - pos[1] - 1);

        int y = stoi(year);
        int m = stoi(month);
        int d = stoi(day);

        string ans = to_binary(y) + "-" + to_binary(m) + "-" + to_binary(d);
        return ans;

    }
};

注:

(1)可变长字符串

定义可变长字符串:stringstream res; // 可变长字符串

写入:res << char((x % 2) + '0');

取出:string r = res.str();

(2)类型强制转换:char((x % 2) + '0')

int + char = int ---> char

(3)翻转(reverse)reverse(r.begin(), r.end());

数组(数组、动态数组、字符串)

(4)切割(substr):year = date.substr(0, pos[0]);

(5)字符串类型转化为数字类型:int y = stoi(year);

leetcode 3274


思路如下:

先写一个函数来获取每个字符串所表示的颜色

将给的字符串进行拆分,得到行列,找规律得到颜色

cpp 复制代码
class Solution {
public:
    //获取字符串表示的颜色
    // 1 表示为白
    // 2 表示为黑
    int get_color(string id){
        char col = id[0];
        char row_char = id[1];
        int row =  row_char - '0';

        if(id[0] == 'a' || id[0] == 'c' || id[0] == 'e' || id[0] == 'g'){
            if(row%2 == 0){
                return 1;
            }
            else{
                return 2;
            }
        }
        else{
            if(row%2 == 0){
                return 2;
            }
            else{
                return 1;
            }
        }
        return 0;
    }
    
    bool checkTwoChessboards(string coordinate1, string coordinate2) {
        int color1 = get_color(coordinate1);
        int color2 = get_color(coordinate2);
        // if(color1 == color2) return true;
        // else return false;
        return color1 == color2;
    }
};

注:

(1)row 为 int 类型,可以通过两个字符相减得到。

cpp 复制代码
char row_char = id[1];
int row =  row_char - '0';
相关推荐
知来者逆12 分钟前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊17 分钟前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北17 分钟前
力扣-160.相交链表
算法·leetcode·链表
爱研究的小陈1 小时前
Day 3:数学基础回顾——线性代数与概率论在AI中的核心作用
算法
渭雨轻尘_学习计算机ing1 小时前
二叉树的最大宽度计算
算法·面试
BB_CC_DD2 小时前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类
梁下轻语的秋缘3 小时前
每日c/c++题 备战蓝桥杯 ([洛谷 P1226] 快速幂求模题解)
c++·算法·蓝桥杯
CODE_RabbitV3 小时前
【深度强化学习 DRL 快速实践】逆向强化学习算法 (IRL)
算法
mit6.8244 小时前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
keep intensify4 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法