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

第一种,回溯法

 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. Shc 应用

    1.说明 shc是一个加密shell脚本的工具, 它的作用是把shell脚本转换为一个可执行的二进制文件 2.安装 下载 # mget  http://www.datsi.fi.upm.es/~fro ...

  2. 解决 HDFS 开发 java.lang.IllegalArgumentException: java.net.UnknownHostException: hadoop000

    出现这种问题多半是windows找不到linux主机所以在 这个路径下的hosts加上linux ip地址,主机名就可以了

  3. Python+Selenium学习--浏览器设置

    场景 设置浏览器窗口的大小有下面两个比较常见的用途: 在统一的浏览器大小下运行用例,可以比较容易的跟一些基于图像比对的工具进行结合,提升测试的灵活性及普遍适用性.比如可以跟sikuli结合,使用sik ...

  4. JS中取得<asp:TextBox中的值

    var s = document.getElementById("<%=txt_DaShen.ClientID %>").value; 注:txt_DaShen 为as ...

  5. Codeforces Beta Round #57 (Div. 2)

    Codeforces Beta Round #57 (Div. 2) http://codeforces.com/contest/61 A #include<bits/stdc++.h> ...

  6. django static 无法正确加载目录下的css

    在static->web目录下添加CSS后该css文件一直报404错误,解决问题: 在setting.py文件添加: STATICFILES_DIRS = [ os.path.join(BASE ...

  7. Specified key was too long; max key length is 767 bytes

    MySQL) )); Database changed ERROR (): Specified bytes drop table if exists test; ) primary key)chars ...

  8. css常见问题一

    [1]禁止换行.class {word-break:keep-all;white-space:nowrap;}[2]强制换行.class{word-break:break-all;}普通容器中(Div ...

  9. No appenders could be found for logger

    在运行代码时,出现下面的错误, log4j:WARN No appenders could be found for logger (genericTest.GenericTest). log4j:W ...

  10. MYSQL 优化常用方法总结

    1, 选取最合适的字段属性以及长度 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快. 比如:定义邮政编码 char(6) 最合适,如果char(2 ...