一、力扣
1、反转字符串中的单词
class Solution {
public String reverseWords(String s) {
int left = 0, right = s.length() - 1;
// 去掉字符串开头的空白字符
while (left <= right && s.charAt(left) == ' ') {
++left;
}
// 去掉字符串末尾的空白字符
while (left <= right && s.charAt(right) == ' ') {
--right;
}
Deque<String> d = new ArrayDeque<String>();
StringBuilder word = new StringBuilder();
while (left <= right) {
char c = s.charAt(left);
if ((word.length() != 0) && (c == ' ')) {
// 将单词 push 到队列的头部
d.offerFirst(word.toString());
word.setLength(0);
} else if (c != ' ') {
word.append(c);
}
++left;
}
d.offerFirst(word.toString());
return String.join(" ", d);
}
}
2、最小栈
class MinStack {
ArrayDeque<int[]> stack;
public MinStack() {
stack=new ArrayDeque<>();
stack.push(new int[]{0, Integer.MAX_VALUE});
}
public void push(int val) {
int[] ans=stack.peek();
stack.push(new int[]{val,Math.min(ans[1],val)});
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek()[0];
}
public int getMin() {
return stack.peek()[1];
}
}
3、组合总和
class Solution {
List<List<Integer>> res;
List<Integer> path;
int[] candidates;
int target;
public List<List<Integer>> combinationSum(int[] candidates, int target) {
res=new ArrayList<>();
path=new ArrayList<>();
this.candidates=candidates;
this.target=target;
dfs(0,0);
return res;
}
public void dfs(int start,int sum){
if(sum>target) return;
if(sum==target){
res.add(new ArrayList<>(path));
return;
}
for(int i=start;i<candidates.length;i++){
path.add(candidates[i]);
dfs(i,sum+candidates[i]);
path.removeLast();
}
}
}
4、组合总和 II
本题的难点在于:集合(数组candidates)有重复元素,但还不能有重复的组合。
class Solution {
List<List<Integer>> res;
List<Integer> path;
int[] candidates;
int target;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
res = new ArrayList<>();
path = new ArrayList<>();
Arrays.sort(candidates);
int[] vis = new int[candidates.length];
this.candidates = candidates;
this.target = target;
dfs(0, 0, vis);
return res;
}
public void dfs(int start, int sum, int[] vis) {
if (sum > target)
return;
if (sum == target) {
res.add(new ArrayList<>(path));
return;
}
for (int i = start; i < candidates.length; i++) {
if (i > 0 && candidates[i] == candidates[i - 1] && vis[i - 1] == 0)
continue;
path.add(candidates[i]);
vis[i] = 1;
dfs(i + 1, sum + candidates[i], vis);
path.removeLast();
vis[i] = 0;
}
}
}
5、组合总和 III
class Solution {
List<List<Integer>> res;
List<Integer> path;
int k;
int n;
public List<List<Integer>> combinationSum3(int k, int n) {
res = new ArrayList<>();
path = new ArrayList<>();
this.k = k;
this.n = n;
dfs(1, 0);
return res;
}
public void dfs(int start, int sum) {
if (sum > n || path.size() > k)
return;
if (sum == n && path.size() == k) {
res.add(new ArrayList<>(path));
return;
}
for (int i = start; i <= 9; i++) {
path.add(i);
dfs(i + 1, sum + i);
path.removeLast();
}
}
}
6、电话号码的字母组合
class Solution {
List<String> result=new ArrayList<>();
public List<String> letterCombinations(String digits) {
if (digits == null || digits.length() == 0) {
return result;
}
//初始对应所有的数字,为了直接对应2-9,新增了两个无效的字符串""
String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
//迭代处理
backTracking(digits, numString, 0);
return result;
}
StringBuilder temp = new StringBuilder();
//比如digits如果为"23",num 为0,则str表示2对应的 abc
public void backTracking(String digits, String[] numString, int num) {
//遍历全部一次记录一次得到的字符串
if (num == digits.length()) {
result.add(temp.toString());
return;
}
//str 表示当前num对应的字符串
String str = numString[digits.charAt(num) - '0'];
for (int i = 0; i < str.length(); i++) {
temp.append(str.charAt(i));
//递归,处理下一层
backTracking(digits, numString, num + 1);
//剔除末尾的继续尝试
temp.deleteCharAt(temp.length() - 1);
}
}
}