784. Letter Case Permutation
这个题的思想很重要,两种方法
第一种,回溯法
class Solution
{
public:
int sz;
vector<string> letterCasePermutation(string S)
{
vector<string> res;
sz=S.size();
backtrack(S,,res);
return res;
} void backtrack(string &s,int i,vector<string> &res)
{
if(i==sz)
{
res.push_back(s);
return ;
}
backtrack(s,i+,res);
if(isalpha(s[i]))
{
s[i]^=(<<);
backtrack(s,i+,res);
}
}
};
这个方法是先扫到末尾,再往回走,往回时遇到一个字母,改变其大小写,然后再把这个序列扫到末尾,改变之后的字母大小写。往回扫到第一个字符时,结束运行,输出结果。
第二种,顺序法
class Solution
{
public:
int sz;
vector<string> letterCasePermutation(string S)
{
vector<string> res;
sz=S.size();
letterCasePermutation(S,,res);
return res;
} void letterCasePermutation(string &s,int idx,vector<string> &res)
{
res.push_back(s);
for(int i=idx;i<sz;i++)
{
char temp=s[i];
if(isalpha(s[i]))
{
s[i]^=(<<);
letterCasePermutation(s,i+,res);
}
s[i]=temp;
}
}
};
这个方法和回溯法有点类似,只是这个是从头扫到尾,扫到第一个字符,改变大小写,然后递归,扫之后的字符。递归完成后,说明改变这个字符之后,右边字符序列改变的组合已经完成了。然后把字符变回去,继续向右扫,直到扫描完成。
784. Letter Case Permutation的更多相关文章
- 【Leetcode_easy】784. Letter Case Permutation
problem 784. Letter Case Permutation 参考 1. Leetcode_easy_784. Letter Case Permutation; 2. Grandyang; ...
- 784. Letter Case Permutation 字符串中字母的大小写组合
[抄题]: Given a string S, we can transform every letter individually to be lowercase or uppercase to c ...
- leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- LeetCode 784 Letter Case Permutation 解题报告
题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...
- [LeetCode&Python] Problem 784. Letter Case Permutation
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【easy】784. Letter Case Permutation
Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", ...
- 784. Letter Case Permutation C++字母大小写全排列
网址:https://leetcode.com/problems/letter-case-permutation/ basic backtracking class Solution { public ...
- LeetCode算法题-Letter Case Permutation(Java实现)
这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...
随机推荐
- fullCalendar插件基本使用
效果图 html代码,需要引入jquery,layui,fullCalendar <!DOCTYPE html> <html lang="en"> < ...
- docker容器和镜像
这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行中的容器之间的区别. 当我对Docker技术还是一知半解的时候,我 ...
- git删除提交历史
精准入口==>官方文档 Q:我们在提交代码时,把密码或者敏感信息也提交了,怎么办? A:“在本地删除密码或敏感信息后,再push到远程仓库” Q:"但这样删除后,在仓库的commit历 ...
- composer ip2city配置
//根据ip获取地址信息composer require "mylukin/ip2city: dev-master" // vendor/mylukin/ip2city/src/I ...
- ifconfig 运行command not found
vi /home/lx/.bash_profile中添加 PATH=$PATH:$HOME/bin:/sbin:/bin export PATH linux查看服务自启动 chkconfig - ...
- 使用swoole编写简单的echo服务器
server.php代码如下: <?php class EchoServer { protected $serv = null; public function __construct() { ...
- linux小笔记
1. 安装go并设置环境变量 Add /usr/local/go/bin to the PATH environment variable. You can do this by adding thi ...
- EF语句拦截器-匹配当前的Controller,Action,User
示例代码,ps:一切都能实现,关键是你尝试的方向,别把简单问题复杂化导致进入死胡同出不来. using Mobile360.Core.Interfaces; using Mobile360.Core. ...
- sql backup
create or replace procedure P_updateasbegin update security_price p set p.closing_price = (select MI ...
- IE7下面踩得坑
bug1.position:fixed:z-index:99; 出现了z-index:2的层级跑到他上面了, 为什么?会出现这问题??? 检查: 1你的固定定位的容器是不是被其他容器包裹,你包裹得容器 ...