蓝桥杯学习笔记01

c++数字转字符串

cpp 复制代码
int num;
string s;
s = to_string(num);

c++字符串转数字

cpp 复制代码
string s;
int num;
num = stoi(s);

解决各位数之和/之积的解决办法,变成字符串

cpp 复制代码
class Solution {
public:
    int subtractProductAndSum(int n) {
        string s = to_string(n);
        int result=1;
        int result2=0;
        for(int i=0;i<s.length();i++){
            result *= s[i]-'0';
            result2 += s[i]-'0';
        }
        return result-result2;
    }
};

二维vector遍历,及其转置

cpp 复制代码
# vector<vector<int>> transposed(col_len, vector<int>(row_len)); 的含义是:
#创建一个二维向量 transposed,其包含 col_len 个一维向量。
#每个一维向量的大小为 row_len。

矩阵转置的代码如下

cpp 复制代码
class Solution {
public:
    vector<vector<int>> transpose(vector<vector<int>>& matrix) {
        int col_len, row_len;
        col_len = matrix[0].size(); //列宽
        row_len = matrix.size();    //行宽
        // 初始化转置后的矩阵
        vector<vector<int>> v1(col_len, vector<int>(row_len));
        int i=0, j=0;
        for(i=0;i<col_len;i++){
            for(j=0;j<row_len;j++){
                v1[i][j] = matrix[j][i];
            }
        }
        return v1;
        
    }
};

c++遍历字符串可以

cpp 复制代码
string s;
for (char c : s)

c++查找某个字符是否属于字符数组中的某个。find,begin,end

cpp 复制代码
class Solution {
public:
    bool isVowel(char c) {
        char vowels[] = {'a', 'e', 'i', 'o', 'u'};
        return find(begin(vowels), end(vowels), c) != end(vowels);
    }
    int vowelStrings(vector<string>& words, int left, int right) {
        int i = 0;
        int cnt=0;
        for(i = left;i<=right;i++){
            string s = words[i];
            if( isVowel(s[0]) && isVowel(s[s.length()-1])) cnt++;
        }
        return cnt;
    }
};
相关推荐
匆匆那年96711 小时前
llamafactory推理消除模型的随机性
linux·服务器·学习·ubuntu
好好学习天天向上~~11 小时前
5_Linux学习总结_vim
linux·学习·vim
笨笨阿库娅12 小时前
从零开始的算法基础学习
学习·算法
羊群智妍20 小时前
2026 AI搜索流量密码:免费GEO监测工具,优化效果看得见
笔记·百度·微信·facebook·新浪微博
阿蒙Amon20 小时前
TypeScript学习-第10章:模块与命名空间
学习·ubuntu·typescript
AI绘画哇哒哒20 小时前
【干货收藏】深度解析AI Agent框架:设计原理+主流选型+项目实操,一站式学习指南
人工智能·学习·ai·程序员·大模型·产品经理·转行
戌中横21 小时前
JavaScript——预解析
前端·javascript·学习
●VON1 天前
React Native for OpenHarmony:2048 小游戏的开发与跨平台适配实践
javascript·学习·react native·react.js·von
山岚的运维笔记1 天前
SQL Server笔记 -- 第18章:Views
数据库·笔记·sql·microsoft·sqlserver
ZH15455891311 天前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter