ECNU刷题总结题目集

ECNU总结题目集(2023/8/29)

题目描述

17. 电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按任意顺序返回。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例 1:

输入:digits = "23"

输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]

示例 2:

输入:digits = ""

输出:[]

示例 3:

输入:digits = "2"

输出:["a","b","c"]

提示:

0 <= digits.length <= 4

digits[i] 是范围 ['2', '9'] 的一个数字。

题解

cpp 复制代码
class Solution {
public:
    string s[8] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 
    vector<string> ans;

    void dfs(int now, int len, string nowS, string digits)
    {
        if(now == len)
        {
            ans.push_back(nowS);
            return ;
        }
        string digit = s[digits[now] - '2'];
        for (char c : digit)
            dfs(now + 1, len, nowS + c, digits);
    }

    vector<string> letterCombinations(string digits) {
        int len = digits.size();
        if(len)
            dfs(0, len, "", digits);
        return ans;
        
    }
};

题目描述

46. 全排列

给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

示例 1:

输入:nums = [1,2,3]

输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

示例 2:

输入:nums = [0,1]

输出:[[0,1],[1,0]]

示例 3:

输入:nums = [1]

输出:[[1]]

提示:

1 <= nums.length <= 6

-10 <= nums[i] <= 10

nums 中的所有整数 互不相同

题解

cpp 复制代码
class Solution {
public:
    vector<vector<int>> ans;
    vector<int> vis, nowV;
    void dfs(int now, int len, vector<int>& nums)
    {
        if(now == len)
        {
            ans.push_back(nowV);
            return ;
        }
        for (int i = 0; i < len; i++)
            if(!vis[i])
            {
                vis[i] = 1;
                nowV.push_back(nums[i]);
                dfs(now + 1, len, nums);
                nowV.pop_back();
                vis[i] = 0;
            }
        }

    vector<vector<int>> permute(vector<int>& nums) {
        int len = nums.size();
        for(int i = 0; i < len; i++)
            vis.push_back(0);
        dfs(0, len, nums);

        return ans;
    }
};
相关推荐
凡人叶枫19 分钟前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
会叫的恐龙29 分钟前
C++ 核心知识点汇总(第六日)(字符串)
c++·算法·字符串
小糯米60140 分钟前
C++顺序表和vector
开发语言·c++·算法
独望漫天星辰1 小时前
C++ 多态深度解析:从语法规则到底层实现(附实战验证代码)
开发语言·c++
王老师青少年编程2 小时前
2024年信奥赛C++提高组csp-s初赛真题及答案解析(阅读程序第3题)
c++·题解·真题·csp·信奥赛·csp-s·提高组
凡人叶枫2 小时前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
CSDN_RTKLIB2 小时前
使用三方库头文件未使用导出符号情景
c++
傻乐u兔3 小时前
C语言进阶————指针3
c语言·开发语言
rainbow68893 小时前
Linux文件描述符与重定向原理
c++
CodeSheep程序羊4 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展