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

【LeetCode 热题 100】全排列 / 子集 / 组合总和 / 分割回文串 / N 皇后

头像
⭐️个人主页:@小羊
⭐️所属专栏:LeetCode 热题 100
很荣幸您能阅读我的文章,诚请评论指点,欢迎欢迎 ~

动图描述

目录

    • 全排列
    • 子集
    • 电话号码的字母组合
    • 组合总和
    • 括号生成
    • 单词搜索
    • 分割回文串
    • N 皇后


全排列

  • 全排列

在这里插入图片描述

class Solution {vector<vector<int>> res;vector<int> path;bool used[7] = {};int n;
public:vector<vector<int>> permute(vector<int>& nums) {n = nums.size();dfs(nums);return res;}void dfs(vector<int>& nums){if (path.size() == n){res.push_back(path);return;}for (int i = 0; i < n; i++){if (!used[i]){used[i] = true;path.push_back(nums[i]);dfs(nums);used[i] = false;path.pop_back();}}}
};

子集

  • 子集

在这里插入图片描述

class Solution {vector<vector<int>> res;vector<int> path;bool used[11] = {};int n;
public:vector<vector<int>> subsets(vector<int>& nums) {n = nums.size();dfs(nums, 0);return res;}void dfs(vector<int>& nums, int pos){res.push_back(path);for (int i = pos; i < n; i++){if (!used[i]){used[i] = true;path.push_back(nums[i]);dfs(nums, i + 1);used[i] = false;path.pop_back();}}}
};

电话号码的字母组合

  • 电话号码的字母组合

在这里插入图片描述

class Solution {string hash[10] = {"", "", "abc", "def","ghi","jkl","mno","pqrs","tuv","wxyz"};vector<string> res;string path;
public:vector<string> letterCombinations(string digits) {if (digits.empty()) return res;dfs(digits, 0);return res;}void dfs(string& digits, int pos){if (pos == digits.size()){res.push_back(path);return;}for (auto e : hash[digits[pos] - '0']){path += e;dfs(digits, pos + 1);path.pop_back();}}
};

组合总和

  • 组合总和

在这里插入图片描述

class Solution {vector<vector<int>> res;vector<int> path;int t;
public:vector<vector<int>> combinationSum(vector<int>& candidates, int target) {t = target;dfs(candidates, 0, 0);return res;}void dfs(vector<int>& candidates, int pos, int sum){if (sum == t){res.push_back(path);return;}else if (sum > t) return;for (int i = pos; i < candidates.size(); i++){path.push_back(candidates[i]);dfs(candidates, i, sum + candidates[i]);path.pop_back();}}
};

括号生成

  • 括号生成

在这里插入图片描述

class Solution {vector<string> res;string path;int left, right, m;
public:vector<string> generateParenthesis(int n) {m = n;dfs();return res;}void dfs(){if (right == m){res.push_back(path);return;}if (left < m){path += "(";left++;dfs();path.pop_back();left--;}if (right < left){path += ")";right++;dfs();path.pop_back();right--;}}
};

单词搜索

  • 单词搜索

在这里插入图片描述

class Solution {int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};bool used[7][7] = {};int m, n;
public:bool exist(vector<vector<char>>& board, string word) {m = board.size(), n = board[0].size();for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){if (board[i][j] == word[0]){if (dfs(board, word, i, j, 1)) return true;}}}return false;}bool dfs(vector<vector<char>>& board, string& word, int i, int j, int pos){if (pos == word.size()) return true;used[i][j] = true;for (int k = 0; k < 4; k++){int x = i + dx[k], y = j + dy[k];if (x >= 0 && x < m && y >= 0 && y < n && !used[x][y] && board[x][y] == word[pos]){if (dfs(board, word, x, y, pos + 1)) return true;}}used[i][j] = false;return false;}
};

分割回文串

  • 分割回文串

在这里插入图片描述

class Solution {vector<vector<string>> res;vector<string> path;
public:vector<vector<string>> partition(string s) {dfs(s, 0);return res;}void dfs(string& s, int pos){if (pos == s.size()){res.push_back(path);return;}for (int i = pos; i < s.size(); i++){if (check(s, pos, i)){path.push_back(s.substr(pos, i - pos + 1));dfs(s, i + 1);path.pop_back();}}}bool check(string& s, int l, int r){while (l < r && s[l] == s[r]){l++, r--;}return l >= r;} 
};

N 皇后

  • N 皇后

在这里插入图片描述

class Solution {vector<vector<string>> res;vector<string> path;bool checkcol[10], checkdig1[20], checkdig2[20];int m;
public:vector<vector<string>> solveNQueens(int n) {m = n;path.resize(n);for (int i = 0; i < n; i++) path[i].append(n, '.');dfs(0);return res;}void dfs(int row){if (row == m){res.push_back(path);return;}for (int col = 0; col < m; col++) // 尝试在当前行放Q{if (!checkcol[col] && !checkdig1[col - row + m] && !checkdig2[col + row]){checkcol[col] = checkdig1[col - row + m] = checkdig2[col + row] = true;path[row][col] = 'Q';dfs(row + 1);checkcol[col] = checkdig1[col - row + m] = checkdig2[col + row] = false;path[row][col] = '.';}}}
};

本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~

头像
http://www.dtcms.com/a/190934.html

相关文章:

  • 论文阅读笔记——双流网络
  • 利用vba替换word中多个表格,相邻单元格的文字
  • 【Lua】Redis 自增并设置有效期
  • spring-cloud-stream学习
  • Halcon案例(二):C#联合Halcon回形针以及方向
  • 【idea】调试篇 idea调试技巧合集
  • 第五部分:第二节 - Node.js 核心模块:厨房里的基本工具
  • 显示的图标跟UI界面对应不上。
  • 无人机数据处理与特征提取技术分析!
  • Thrust库中的Gather和Scatter操作
  • 【Linux】第十六章 分析和存储日志
  • uniapp vue 沉浸式窗体如何获取并排除外部手机浏览器底部菜单栏工具栏高度
  • sqli—labs第六关——双引号报错注入
  • 小白学习java第18天(上):spring
  • 【C++】17. 多态
  • 大型企业数据治理与数据资产化:数字化转型的炼金术革命
  • 如何解决电脑蓝屏错误代码:Oxc0000098
  • 鸿蒙OSUniApp开发支持多语言的国际化组件#三方框架 #Uniapp
  • elementUI调整滚动条高度后与固定列冲突问题解决
  • 高海拔和远距离的人员识别:面部、体型和步态的融合
  • Java 源码 HashMap源码分析
  • PROE 转 STP 全攻略:软件实操、在线转换与问题解决
  • 【网工第6版】第10章 网络规划和设计②
  • D. Eating【Codeforces Round 1005 (Div. 2)】
  • 微机原理与接口技术知识点总结——8086微处理器ddddd
  • 16 - VDMA之视频转发实验
  • 字符串检索算法:KMP和Trie树
  • 解决 PicGo 上传 GitHub图床及Marp中Github图片编译常见难题指南
  • VUE3 -综合实践(Mock+Axios+ElementPlus)
  • 安全合规检查开源项目ComplianceAsCode/content详解及操作系统新产品开发适配指南