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';
相关推荐
闻缺陷则喜何志丹30 分钟前
【背包 组合】P7552 [COCI 2020/2021 #6] Anagramistica|普及+
c++·算法·背包·洛谷·组合
一只机电自动化菜鸟1 小时前
一建机电备考笔记(17) 常用设备—通用设备1(含考频+题型)
笔记·学习·职场和发展·生活·学习方法
小章UPUP1 小时前
2026年第十六届MathorCup数学应用挑战赛D题国奖思路
算法
hssfscv1 小时前
软件设计师下午试题四——C语言(N皇后问题、分治、动态规划)
c语言·算法·动态规划
lolo大魔王2 小时前
Go语言的反射机制
开发语言·后端·算法·golang
白羊by2 小时前
Softmax 激活函数详解:从数学原理到应用场景
网络·人工智能·深度学习·算法·损失函数
故事和你912 小时前
洛谷-算法1-7-搜索3
数据结构·c++·算法·leetcode·动态规划
chipsense2 小时前
霍尔电流传感器选型方法论再升级:从800V平台到TMR竞争的全场景决策树
算法·决策树·机器学习·闭环霍尔·tmr传感
CoderYanger2 小时前
14届蓝桥杯省赛Java A 组Q1~Q3
java·开发语言·线性代数·算法·职场和发展·蓝桥杯
会编程的土豆4 小时前
【日常做题】 代码随想录(岛屿最大面积+寻宝)
数据结构·算法·图论