1. 判断子序列(相对位置是不变的)
(1)双指针
使用两个指针分别指向两个字符串的起始索引,每次循环判断两个指针指向的字符是否相等,相等则两个指针向右移动,不相等则只移动 s 的指针。最后如果 t 指针移动到末尾,则 t 是为 s 的子序列。
class Solution {
public boolean isSubsequence(String t, String s) {
int indext = 0, indexs = 0;
while (indext < t.length() && indexs < s.length()) {
if (t.charAt(indext) == s.charAt(indexs)) {
indext++;
}
indexs++;
}
return indext == t.length();
}
}
(2)使用API
直接使用 indexOf 方法,判断从指定下标起,是否存在字符 c。不存在,则 t 不是为 s 的子序列
class Solution {
public boolean isSubsequence(String t, String s) {
int index = -1;
for (int i = 0; i < t.length(); i++) {
//从index + 1开始获取字符t.charAt(i)在s中的的位置
index = s.indexOf(t.charAt(i), index + 1);
if (index == -1) {
return false;
}
}
return true;
}
}
2. 判断子串
(1) contains
方法返回true,当且仅当此字符串包含指定的char值序列
public static void main(String[] args) {
String str = "abc";
boolean status = str.contains("a");
if(status){
System.out.println("包含");
}else{
System.out.println("不包含");
}
}
(2)indexOf方法
java.lang.String.indexOf() 的用途是在一个字符串中寻找一个字的位置,同时也可以判断一个字符串中是否包含某个字符
indexOf的返回值为int
public static void main(String[] args) {
String str1 = "abcdefg";
int result1 = str1.indexOf("ab");
if(result1 != -1){
System.out.println("字符串str中包含子串“ab”"+result1);
}else{
System.out.println("字符串str中不包含子串“ab”"+result1);
}
}
3. 找出字符串中子序列(子集)
(1)无重复元素时:
class Solution {
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
Deque<Integer> path = new LinkedList<>();// 用来存放符合条件结果
public List<List<Integer>> subsets(int[] nums) {
if (nums.length == 0){
result.add(new ArrayList<>());
return result;
}
backTracking(nums, 0);
return result;
}
private void backTracking(int[] nums, int startIndex){
//「遍历这个树的时候,把所有节点都记录下来,就是要求的子集集合」。
result.add(new ArrayList<>(path));
if (startIndex >= nums.length){ //终止条件可不加
return;
}
for (int i = startIndex; i < nums.length; i++){
path.add(nums[i]);
backTracking(nums, i + 1);
path.removeLast();
}
}
}
(2)有重复元素时:
class Solution {
List<List<Integer>> result = new ArrayList<>();// 存放符合条件结果的集合
LinkedList<Integer> path = new LinkedList<>();// 用来存放符合条件结果
boolean[] used;
public List<List<Integer>> subsetsWithDup(int[] nums) {
if (nums.length == 0){
result.add(path);
return result;
}
Arrays.sort(nums);
used = new boolean[nums.length];
backTracking(nums, 0);
return result;
}
private void backTracking(int[] nums, int startIndex){
result.add(new ArrayList<>(path));
if (startIndex >= nums.length){
return;
}
for (int i = startIndex; i < nums.length; i++){
// used[i - 1] == true,说明同一树支candidates[i - 1]使用过
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
// 同一层,上一个nums[i - 1]使用过回溯出来,used[i - 1] == false,即可判断在同一层使用过
// 要对同一树层使用过的元素进行跳过
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false){
continue;
}
path.add(nums[i]);
used[i] = true;
backTracking(nums, i + 1);
path.removeLast();
used[i] = false;
}
}
}
4. 找字符串中所有子串
输入: abc
输出: a ab abc b bc c
(1)列出字符串的所有子串(无重复)
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static void main(String[] args) {
List<String> res = substr("abbc");
for (String str : res) {
System.out.println(str);
}
}
public static List<String> substr(String str) {
List<String> res = new ArrayList<>();
for (int i = 0; i < str.length(); i++) {
for (int j = i; j < str.length(); j++) {
res.add(str.substring(i, j + 1));
}
}
return res;
}
}