当前位置: 首页 > news >正文

上海市城乡建设部网站首页网站文章要求

上海市城乡建设部网站首页,网站文章要求,百度推广外推联系方式,wordpress标签tags页1. 全排列 给定一个不含重复数字的数组 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: 输入&#…

1. 全排列

给定一个不含重复数字的数组 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 中的所有整数 互不相同

题解

class Solution {
public:void dfs(vector<int>& nums, vector<vector<int>>& ans, vector<bool>& used, vector<int>& path, int idx) {if(idx == nums.size()) {ans.push_back(path);return;}for(int i = 0; i < nums.size(); i ++ ) {if(used[i] == true) continue;used[i] = true;path[idx] = nums[i];dfs(nums, ans, used, path, idx + 1);used[i] = false;}}vector<vector<int>> permute(vector<int>& nums) {vector<vector<int>> ans;vector<bool> used(nums.size(), false);vector<int> path(nums.size());dfs(nums, ans, used, path, 0);return ans;}
};

2. 子集

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的

子集

(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

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

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • 10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同

题解

用二进制数表示 nums 的每个位置上的值是否在集合中

class Solution {
public:vector<vector<int>> subsets(vector<int>& nums) {vector<vector<int>> ans;vector<int> path;int n = nums.size();for(int mask = 0; mask < (1 << n); mask ++ ) {path.clear();for(int i = 0; i < n; i ++ ) {if(mask & (1 << i)) path.push_back(nums[i]);}ans.push_back(path);}return ans;}
};

3. 电话号码的组合

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

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

!https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/11/09/200px-telephone-keypad2svg.png

示例 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'] 的一个数字。

题解

class Solution {
public:vector<string> d2s = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};void dfs(vector<string>& ans, string& digits, int idx, string& path) {if(idx == digits.length()) {ans.push_back(path);return;}int c = digits[idx] - '2';for(int i = 0; i < d2s[c].length(); i ++ ) {path.push_back(d2s[c][i]);dfs(ans, digits, idx + 1, path);path.pop_back();}}vector<string> letterCombinations(string digits) {if(digits.size() == 0) return {};vector<string> ans;string path;int n = digits.size();dfs(ans, digits, 0, path);return ans;}
};

4. 组合总和

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 **不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates =[2,3,6,7], target =7输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。

示例 2:

输入:candidates = [2,3,5],target = 8
输出:[[2,2,2,2],[2,3,3],[3,5]]

示例 3:

输入:candidates =[2],target = 1
输出:[]

提示:

  • 1 <= candidates.length <= 30
  • 2 <= candidates[i] <= 40
  • candidates 的所有元素 互不相同
  • 1 <= target <= 40

题解

dfs函数中要枚举两种情况:

  1. 放该元素,要求放了后和应该小于等于target,再dfs(idx)
  2. 不放该元素,dfs(idx + 1)
class Solution {
public:void dfs(vector<vector<int>>& ans, vector<int>& path, vector<int>& candidates, int target, int sum, int idx) {if(idx == candidates.size()) return;if(sum == target) {ans.push_back(path);return;}// 不放dfs(ans, path, candidates, target, sum, idx + 1);// 放if(target >= sum + candidates[idx]) {path.push_back(candidates[idx]);dfs(ans, path, candidates, target, sum + candidates[idx], idx);path.pop_back();}}vector<vector<int>> combinationSum(vector<int>& candidates, int target) {vector<vector<int>> ans;vector<int> path;dfs(ans, path, candidates, target, 0, 0);return ans;}
};
http://www.dtcms.com/a/558839.html

相关文章:

  • 深入洞察:AI技术突破及应用成熟评估方法
  • wordpress网址域名2019做网站seo行不行
  • 可以免费进的服务器网站上海市网站建设公司
  • 怎么做点播网站论坛网站免费建设模板下载
  • 品牌网站建设策划书陕西 网站建设 陕ICP
  • 网站404页面在哪查看孔夫子旧书网网站谁做的
  • tag 网站备案湖南省郴州市北湖区
  • 网站开发微盘百度搜索推广的五大优势
  • 智慧交通红绿灯检测数据集VOC+YOLO格式1215张3类别
  • 做网站用小公司还是大公司好广西住房城乡建设厅官方网站
  • 网站开发报价技巧上海招聘信息最新招聘
  • 长沙做彩票网站公司打金新开传奇网站
  • 网站根目录权限设置wordpress背景设置
  • 计算机操作系统:与设备无关的I/O软件
  • 怎么做一个公司网站广州装修公司哪家好
  • 百度网站优化升上去营销策略ppt模板
  • 仓颉语言实战:无重复字符的最长子串工具库
  • pg_stat 视图介绍
  • 游戏网站cms网站做好了前端 后端怎么做
  • 青岛住房和城乡建设厅网站个人注册域名和公司注册域名区别
  • 京东e卡 滑块分析
  • 嘉兴制作网站深圳网站建设最专业的
  • 网站打开速度慢 如何优化做网站收会员费
  • 网站制作公司网址wordpress 无刷新翻页
  • 临汾网站建设 吕梁网站建设郑州新闻最新消息
  • 网站开发学些什么软件电商平台是干什么的
  • 宜城网站定制seo推广计划
  • 合川建网站网站建设_seo技术支持
  • 广东住房和城乡建设部网站alexa排名助手
  • 做网站的电话江苏seo培训