【C++】vector相关OJ

文章目录

    • [1. 只出现一次的数字](#1. 只出现一次的数字)
    • [2. 杨辉三角](#2. 杨辉三角)
    • [3. 电话号码字母组合](#3. 电话号码字母组合)

ヾ(๑╹◡╹)ノ" 人总要为过去的懒惰而付出代价 ヾ(๑╹◡╹)ノ"


1. 只出现一次的数字

力扣链接

代码展示:

cpp 复制代码
class Solution {
public:
    int singleNumber(vector<int>& nums) 
    {
        int value = 0;
        for(auto e : nums)
        {
            value ^= e;
        }
        return value;
    }
};

思路:异或

2. 杨辉三角

力扣链接

代码展示:

cpp 复制代码
class Solution {
public:
    vector<vector<int>> generate(int numRows) 
    {
        vector<vector<int>> vv;
        vv.resize(numRows);//开空间,numRows个vector<int>对象
        //杨辉三角
        for (size_t i = 0; i < vv.size(); ++i)
        {
            //开空间
            vv[i].resize(i + 1, 0);//开空间
            vv[i][0] = 1;
            vv[i][vv[i].size() - 1] = 1;
        }

        for (size_t i = 0; i < vv.size(); ++i)
        {
            for (size_t j = 0; j < vv[i].size(); ++j)
            {
                if (vv[i][j] == 0)
                {
                    vv[i][j] = vv[i - 1][j - 1] + vv[i - 1][j];
                }
            }
        }
        return vv;
    }
};

思路 :

(1)规律:头尾都是1,中间第j个数等于上一行j-1+j;

(2)通过两次operator,来访问每一个数据。本质上是先访问第i个vector< int >对象,然后再访问这个对象的第j个int类型的数据(动态二维数组);

注意:(1)vector< vector < int >> 类比二维数组,vector < int > 类比于int数组,vector< vector < int >> 类比于对象数组,对象全是vector< int >类型的

3. 电话号码字母组合

力扣链接

代码展示:

cpp 复制代码
class Solution {
    //数字对应英文字母的映射
    string _numToStr[10] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public:
    //递归函数
    void _letterCombine(string digits, size_t di, string combineStr, vector<string>& retV)
    {
        //返回条件
        if(di == digits.size())
        {
            retV.push_back(combineStr);
            return;
        }
        //取到数字字符转换为数字,在取到对应的字符串
        int num = digits[di] - '0';//数字字符转换为数字
        string str = _numToStr[num];//取到对应的字符串
        for (auto ch : str)
        {
            _letterCombine(digits, di + 1, combineStr + ch, retV);
        }


    }
    vector<string> letterCombinations(string digits) 
    {
        vector<string> retV;
        if(digits.empty())
            return retV;
        size_t di = 0;
        string str;
        _letterCombine(digits, di, str, retV);
        return retV;
    }
};

思路 :(1)排列组合,多路递归。

(2)当di等于数字字符串的大小【digits.size()】时,此时的combineStr尾插到retV【类型是vector< string >】并递归返回。

注意:(1)指的是,2-9键所对应的字母 的组合【比如:2对应的就是"abc",3对应的就是"def",4对应的就是"ghi",7对应的就是"pqrs"等】。

(2)输出多少个数字,字符串的长度就是多少。

相关推荐
“AI国潮设计-小江”13 小时前
【AIGC实战】Python+SDXL批量生成“潮汕英歌舞不倒翁”IP | 角色一致性与多场景融合方案,附完整代码
开发语言·人工智能·python·prompt·aigc
传奇开心果编程13 小时前
【Rust入门练中学】第6课:结构体与方法
开发语言·rust·结构体·编程语言理论·结构体方法·编程通用语言
code tsunami14 小时前
【无标题】
开发语言·python
CodeGL14 小时前
10、Python基础之进程和线程
开发语言·python
hetao173383715 小时前
2026-10-22~23 hetao1733837 的刷题记录
c++·算法
TechWayfarer15 小时前
PHP漏洞被利用,怎么揪出攻击IP?
开发语言·python·tcp/ip·网络安全·php
王老师青少年编程16 小时前
2025年6月GESP真题及题解(C++三级): 选择题和判断题(题解)
c++·真题·gesp·答案·csp·三级·信奥赛
卡卡~~17 小时前
“101计划”核心课程实践教材《数据实验结构指导-C++语言版》题目集
开发语言·c++·学习·算法
qq_4069810317 小时前
大白话rust系列09 函数调用两次有什么错?
开发语言·python·rust
学逆向的17 小时前
C 多文件开发
c语言·开发语言·网络安全