这个题的思想很重要,两种方法

第一种,回溯法

 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的更多相关文章

  1. 【Leetcode_easy】784. Letter Case Permutation

    problem 784. Letter Case Permutation 参考 1. Leetcode_easy_784. Letter Case Permutation; 2. Grandyang; ...

  2. 784. Letter Case Permutation 字符串中字母的大小写组合

    [抄题]: Given a string S, we can transform every letter individually to be lowercase or uppercase to c ...

  3. 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 ...

  4. LeetCode 784 Letter Case Permutation 解题报告

    题目要求 Given a string S, we can transform every letter individually to be lowercase or uppercase to cr ...

  5. [LeetCode&Python] Problem 784. Letter Case Permutation

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  6. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

  7. 【easy】784. Letter Case Permutation

    Examples: Input: S = "a1b2" Output: ["a1b2", "a1B2", "A1b2", ...

  8. 784. Letter Case Permutation C++字母大小写全排列

    网址:https://leetcode.com/problems/letter-case-permutation/ basic backtracking class Solution { public ...

  9. LeetCode算法题-Letter Case Permutation(Java实现)

    这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...

随机推荐

  1. 100-days:nine

    Title: Boeing(波音飞机) crash isolates FAA as(伴随,随着) China leads push against Max(出事机型,即737 Max) crash n ...

  2. Delphi: 圆形进度(环形进度)

    起源: 重回DC5项目,资源下载美工提供圆形进度条,复习Delphi,为实现其颇觉有趣,遂研究其. 最终效果图如下: 实现: 制作TCircleProgress控件,实现方法参照系统之TGauge控件 ...

  3. java获取电脑部分信息

    获取mac地址与cpu序列号 参考博客:https://www.jb51.net/article/94793.htm 另一篇参考地址没记录下来 package util; import java.io ...

  4. twitter oa

    字符串括号匹配有效性: 要求从直接return改成了返回yes or no.需要添加到list后break,然后每次循环之前,boolean要重新初始化. array index报错是什么鬼?算了,脑 ...

  5. 移动端meta行大全

    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable= ...

  6. 亚像素Sub Pixel

    亚像素Sub Pixel 评估图像处理算法时,通常会考虑是否具有亚像素精度. 亚像素概念的引出: 图像处理过程中,提高检测方法的精度一般有两种方式:一种是提高图像系统的光学放大倍数和CCD相机的分辨率 ...

  7. centos7下apache2.4反向代理

    apache安装目录在/data/apache24,这里就不介绍apache的安装了. 一.反向代理配置 在/data/apache24/conf/extra下创建htttpd-proxy.conf文 ...

  8. c# 把对象加入队列,对象为全局变量,对象改变队列值也跟着改变

    若程序把对象加入队列,对象为全局变量,对象改变队列值也跟着改变,如下: filecontent  为两个字段的属性值. class FileContent { // public string Htt ...

  9. (转)silverlight应用程序中未处理的错误代码:2104 类别:InitializeError

        解决方案:第一步:默认网站--属性-----http头 第二步:点击mime类型: 第三步:点击新建: 第四步:输入扩展名以及类型: (1) 扩展名:.xaml MIME类型:applicat ...

  10. hdu 1026(BFS+输出路径) 我要和怪兽决斗

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...