[抄题]:

Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string.  Return a list of all possible strings we could create.

Examples:
Input: S = "a1b2"
Output: ["a1b2", "a1B2", "A1b2", "A1B2"] Input: S = "3z4"
Output: ["3z4", "3Z4"] Input: S = "12345"
Output: ["12345"]

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

不知道怎么写DFS中的回溯法:只知道先写退出条件,再写循环

[一句话思路]:

  1. 先都变成小写再说 简单粗暴。不是不可以,调用java已有的函数行,可见函数很多

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. DFS的循环中,退出条件是return; 不要把添加结果理解成退出
  2. 一般是主函数返回,helper类型为void,注意一下
  3. Character.toUpperCase(a[pos])的操作对象是Character抽象类,不是a[pos]本身,所以要赋值。不要搞错对象了。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. corner case需要带入值来思考,不能一概而论 eg本题null- null

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. 回溯法:先启动,然后进行一步,下一步后再返回

Character是char的包装类,就像Integer和int ,以及Long和long一样。

Character是char的包装类,注意它是一个类,提供了很多方法的。

包装类和基本类型可以自动转换,这是jdk1.5(5.0)的新特性,叫做自动封箱和自动解封

即:

例1:

char ch='a';

Character ch1=ch;//自动封箱

Character c=new Character(a);

char c1=c;//自动解封

Character 方法:

序号 方法与描述
1 isLetter()
是否是一个字母
2 isDigit()
是否是一个数字字符
3 isWhitespace()
是否是一个空格
4 isUpperCase()
是否是大写字母
5 isLowerCase()
是否是小写字母
6 toUpperCase()
指定字母的大写形式
7 toLowerCase()
指定字母的小写形式
8 toString()
返回字符的字符串形式,字符串的长度仅为1

[关键模板化代码]:

void helper (char[] a, int pos, List<String> res) {
//break
if (pos == a.length) {
res.add(new String(a));
return ;
}
//start
helper(a, pos + 1, res);
//backtracing
if (Character.isLetter(a[pos])) {
a[pos] = Character.toUpperCase(a[pos]);
helper(a, pos + 1, res);
a[pos] = Character.toLowerCase(a[pos]);
}

退出-开始-回溯

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public List<String> letterCasePermutation(String S) {
//corner case
List<String> res = new ArrayList<String>();
if (S == null) {
return null;
}
//dfs
char[] a = S.toLowerCase().toCharArray();
helper(a, 0, res);
return res;
} void helper (char[] a, int pos, List<String> res) {
//break
if (pos == a.length) {
res.add(new String(a));
return ;
}
//start
helper(a, pos + 1, res);
//backtracing
if (Character.isLetter(a[pos])) {
a[pos] = Character.toUpperCase(a[pos]);
helper(a, pos + 1, res);
a[pos] = Character.toLowerCase(a[pos]);
}
}
}

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. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

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

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

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

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

  5. LeetCode 784 Letter Case Permutation 解题报告

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

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

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

  7. 784. Letter Case Permutation

    这个题的思想很重要,两种方法 第一种,回溯法 class Solution { public: int sz; vector<string> letterCasePermutation(s ...

  8. 【easy】784. Letter Case Permutation

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

  9. Map集合应用 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)...

    package com.swift; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import ...

随机推荐

  1. react 拖拽排序---原生

    定义css, 两个动画 .drag-up { -webkit-animation: dragup ease 0.2s 1; animation: dragup ease 0.2s 1; -webkit ...

  2. LSTM长短期记忆网络

    Long Short Term Memory networks : http://colah.github.io/posts/2015-08-Understanding-LSTMs/

  3. Luogu2435 染色【状压qwq】【轮廓线DP】

    LINK 题目大意 有一个 n 行 m 列的格点图,你需要给每个点上染上 k 种颜色中的一种,要求没有两个相邻点颜色相同.给定第一行与最后一行的染色,试求总染色方案数. 思路 暴力预处理状态暴力转移可 ...

  4. cratedb joins 原理(官方文档)

      JOINs are essential operations in relational databases. They create a link between rows based on c ...

  5. hasura graphql 引擎基本试用

    hasura 使用一个基于pg数据库的graphql引擎,他的设计比postgrpahql 有好多方便的地方,同时使用也比较简单 安装 docker && docker-compose ...

  6. purescript 基本试用

    安装环境 安装预编译文件 https://github.com/purescript/purescript/releases 配置环境变量: export PATH=$PATH:/Users/dalo ...

  7. 对类 sizeof

    sizeof一个类的时候,都什么会被计算?静态成员会被计算进来么?如果这是一个子类,它的父类成员会被计算么? #include <iostream> using namespace std ...

  8. servlet配置及其生命周期

    servlet配置: 在web.xml中,首先向服务器注册一个servlet.在<servlet>标签下 给定一个servlet名字,这个servlet-name是我们自己用的,方便我们用 ...

  9. usbip install

    # README for usbip-utils## Copyright (C) 2011 matt mooney <mfm@muteddisk.com>#               2 ...

  10. 关于Class.getResource和ClassLoader.getResource的路径问题(转)

    参考博客:http://www.cnblogs.com/yejg1212/p/3270152.html Class.getResource(String path) 当path以/开头,如/a/b/c ...