Skip to content

Commit 0ffa87e

Browse files
committed
add all
1 parent c637a52 commit 0ffa87e

File tree

12 files changed

+686
-0
lines changed

12 files changed

+686
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 521. Longest Uncommon Subsequence I
2+
3+
## #1 暴力搜索[TLE]
4+
5+
### 思路
6+
7+
用深度搜索的方式遍历所有的可能的子序列,然后在这些搜索的子序列里面找出只出现过一次的最大子序列。为了统计哪些子序列只出现1次,我们可以使用`HashMap`来记录每个字序列出现的次数。
8+
9+
### 算法
10+
11+
```java
12+
class Solution {
13+
Map<String, Integer> res = new HashMap<>();
14+
public int findLUSlength(String a, String b) {
15+
if(a == null || a.length() == 0) return b==null||b.length()==0?-1:b.length();
16+
if(b == null || b.length() == 0) return a==null||a.length()==0?-1:a.length();
17+
dfs(a.toCharArray(), 0, new StringBuilder());
18+
dfs(b.toCharArray(), 0, new StringBuilder());
19+
int max = -1;
20+
for(Map.Entry<String, Integer> entry : res.entrySet()){
21+
if(entry.getValue() == 1)
22+
max = Math.max(max, entry.getKey().length());
23+
}
24+
return max;
25+
}
26+
27+
public void dfs(char[] chars, int step, StringBuilder sb){
28+
if(step == chars.length){
29+
res.put(sb.toString(), res.getOrDefault(sb.toString(), 0) + 1);
30+
return;
31+
}
32+
//要该字符
33+
sb.append(chars[step]);
34+
dfs(chars, step+1, sb);
35+
//不要该字符
36+
sb.setLength(sb.length()-1);
37+
dfs(chars, step+1, sb);
38+
}
39+
}
40+
```
41+
42+
### 复杂度分析
43+
44+
- 时间复杂度:$O(2^n)$
45+
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# 555. Split Concatenated Strings
2+
3+
## #1 深度优先搜索[TLE]
4+
5+
### 思路
6+
7+
通过深度优先遍历所有可能的组合,然后对每种可能的组合,遍历所有可能的切分点。
8+
9+
### 算法
10+
11+
```java
12+
class Solution {
13+
String res = "";
14+
public String splitLoopedString(String[] strs) {
15+
dfs(strs, "", 0, strs.length);
16+
return res;
17+
}
18+
19+
public void dfs(String[] strs, String s, int i, int n){
20+
if(i < n){
21+
dfs(strs, s + strs[i], i+1, n);
22+
dfs(strs, s + new StringBuffer(strs[i]).reverse().toString(), i+1, n);
23+
}else{
24+
for(int j=0; j<s.length(); j++){
25+
String cut = s.substring(j) + s.substring(0, j);
26+
if(cut.compareTo(res) > 0)
27+
res = cut;
28+
}
29+
}
30+
}
31+
}
32+
```
33+
34+
### 复杂度分析
35+
36+
## #2 广度优先遍历[MLE]
37+
38+
### 思路
39+
40+
想法和上面是一样的,就是在枚举的时候采用广度优先的做法。
41+
42+
### 算法
43+
44+
```java
45+
class Solution {
46+
public String splitLoopedString(String[] strs) {
47+
String res = "";
48+
Queue<String> queue = new LinkedList<>();
49+
queue.offer("");
50+
int i=0, j=0;
51+
while(i < strs.length){
52+
String cur = queue.poll();
53+
queue.offer(cur+strs[i]);
54+
queue.offer(cur + new StringBuffer(strs[i]).reverse().toString());
55+
j++;
56+
if(j == 1<<i){
57+
i++;
58+
j=0;
59+
}
60+
}
61+
while(!queue.isEmpty()){
62+
String cur = queue.poll();
63+
for(int k=0; k<cur.length(); k++){
64+
String cut = cur.substring(k) + cur.substring(0, k);
65+
if(cut.compareTo(res) > 0)
66+
res = cut;
67+
}
68+
}
69+
return res;
70+
}
71+
}
72+
```
73+
74+
### 复杂度分析
75+
76+
## #3 优化枚举
77+
78+
### 思路
79+
80+
### 算法
81+
82+
```java
83+
class Solution {
84+
public String splitLoopedString(String[] strs) {
85+
for(int i=0; i<strs.length; i++){
86+
String rev = new StringBuffer(strs[i]).reverse().toString();
87+
if(rev.compareTo(strs[i]) > 0)
88+
strs[i] = rev;
89+
}
90+
String res = "";
91+
for(int i=0; i<strs.length; i++){
92+
String rev = new StringBuffer(strs[i]).reverse().toString();
93+
for(String s : new String[]{strs[i], rev}){
94+
for(int k=0; k<s.length(); k++){
95+
StringBuffer temp = new StringBuffer();
96+
temp.append(s.substring(k));
97+
for(int j=i+1; j<strs.length; j++)
98+
temp.append(strs[j]);
99+
for(int j=0; j<i; j++){
100+
temp.append(strs[j]);
101+
}
102+
temp.append(s.substring(0, k));
103+
if(temp.toString().compareTo(res) > 0)
104+
res = temp.toString();
105+
}
106+
}
107+
}
108+
return res;
109+
}
110+
}
111+
```
112+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 556. Next Greater Element III
2+
3+
## #1 深度优先遍历[AC]
4+
5+
### 思路
6+
7+
遍历所有可能的情况,然后排序,最后选出第一个大于N的结果。
8+
9+
### 算法
10+
11+
```java
12+
class Solution {
13+
List<Long> total = new ArrayList<>();
14+
public int nextGreaterElement(int n) {
15+
String ns = String.valueOf(n);
16+
int[] nums = new int[10];
17+
for(int i=0; i<ns.length(); i++){
18+
nums[ns.charAt(i) - '0']++;
19+
}
20+
dfs(nums, 0, ns.length(), new StringBuffer());
21+
Collections.sort(total);
22+
// System.out.print(total);
23+
for(long val : total){
24+
if(val > n && val < Integer.MAX_VALUE)
25+
return (int)val;
26+
}
27+
return -1;
28+
}
29+
30+
public void dfs(int[] nums, int step, int length, StringBuffer sb){
31+
if(step == length){
32+
total.add(Long.parseLong(sb.toString()));
33+
return;
34+
}else{
35+
for(int i=0; i<nums.length; i++){
36+
if(nums[i] <= 0)
37+
continue;
38+
sb.append(""+i);
39+
nums[i]--;
40+
dfs(nums, step+1, length, sb);
41+
sb.setLength(sb.length() - 1);
42+
nums[i]++;
43+
}
44+
}
45+
}
46+
}
47+
```
48+
49+
### 复杂度分析
50+
51+
- 时间复杂度
52+
- 空间复杂度
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# 557. Reverse Words in a String III
2+
3+
## #1 识别单词[AC]
4+
5+
### 思路
6+
7+
将字符串转为字符数组,然后线性扫描字符数组,通过判断当前是否为空格,来识别字符数组中的单词,一旦识别到单词,就用`i,j`指向单词的起始点和终止点,然后逆序这个单词。
8+
9+
### 算法
10+
11+
```java
12+
class Solution {
13+
public String reverseWords(String s) {
14+
/*
15+
用i,j保存每个单词的起始和终止位置
16+
用k表示已经遍历到的位置
17+
*/
18+
if(s == null || s.length() == 0) return s;
19+
char[] chars = s.toCharArray();
20+
int i=0, j=0;
21+
for(int k=0; k<s.length(); k++){
22+
// System.out.print("k="+k);
23+
if(chars[k] == ' ')
24+
continue;
25+
i = k;
26+
j = k;
27+
while(j < s.length() && chars[j] != ' ')
28+
j++;
29+
j--;
30+
k = j;
31+
for(;i<j; i++, j--){
32+
char temp = chars[i];
33+
chars[i] = chars[j];
34+
chars[j] = temp;
35+
}
36+
// System.out.println(",String="+new String(chars));
37+
}
38+
return new String(chars);
39+
}
40+
}
41+
```
42+
43+
### 复杂度分析
44+
45+
- 时间复杂度
46+
- 空间复杂度
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#583. Delete Operation for Two Strings
2+
3+
## #1 最大子序列[AC]
4+
5+
### 思路
6+
7+
该问题可以转化为求解两个字符串的最长子序列,如果我们找到了最长子序列,那么其他的字符就是我们要删掉的。
8+
9+
### 算法
10+
11+
```java
12+
class Solution {
13+
/*
14+
两个字符串要完全相同,我们要找到两个字符串的最长公共子序列!
15+
我们可以通过动态规划的方法来求解!
16+
*/
17+
public int minDistance(String word1, String word2) {
18+
if(word1 == null || word1.length() == 0)
19+
return word2 == null ? 0 : word2.length();
20+
if(word2 == null || word2.length() == 0)
21+
return word1 == null ? 0 : word1.length();
22+
int m = word1.length();
23+
int n = word2.length();
24+
int[][] dp = new int[m+1][n+1];
25+
for(int i=1; i<=m; i++){
26+
for(int j=1; j<=n; j++){
27+
int path1 = word1.charAt(i-1) == word2.charAt(j-1) ? dp[i-1][j-1] + 1 : 0;
28+
int path2 = dp[i-1][j];
29+
int path3 = dp[i][j-1];
30+
dp[i][j] = Math.max(path1, Math.max(path2, path3));
31+
}
32+
}
33+
return m + n - 2 * dp[m][n];
34+
}
35+
}
36+
```
37+
38+
### 复杂度分析
39+
40+
- 时间复杂度
41+
- 空间复杂度
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 606. Construct String from Binary Tree
2+
3+
## #1 控制递归条件
4+
5+
### 思路
6+
7+
对于当前节点的不同状态,执行不同的递归条件,从而可用控制加括号的方式。
8+
9+
### 算法
10+
11+
```java
12+
/**
13+
* Definition for a binary tree node.
14+
* public class TreeNode {
15+
* int val;
16+
* TreeNode left;
17+
* TreeNode right;
18+
* TreeNode(int x) { val = x; }
19+
* }
20+
*/
21+
class Solution {
22+
public String tree2str(TreeNode t) {
23+
StringBuffer sb = new StringBuffer();
24+
preorder(t, sb);
25+
return sb.toString();
26+
}
27+
28+
public void preorder(TreeNode t, StringBuffer sb){
29+
if(t == null)
30+
return;
31+
if(t.left!= null && t.right != null){
32+
sb.append("" + t.val);
33+
// 左子树
34+
sb.append("(");
35+
preorder(t.left, sb);
36+
sb.append(")");
37+
38+
// 右子树
39+
sb.append("(");
40+
preorder(t.right, sb);
41+
sb.append(")");
42+
}else if(t.left == null && t.right != null){
43+
sb.append("" + t.val);
44+
sb.append("(");
45+
sb.append(")");
46+
sb.append("(");
47+
preorder(t.right, sb);
48+
sb.append(")");
49+
}else if(t.right == null && t.left != null){
50+
sb.append("" + t.val);
51+
// 左子树
52+
sb.append("(");
53+
preorder(t.left, sb);
54+
sb.append(")");
55+
}else{
56+
sb.append("" + t.val);
57+
}
58+
}
59+
}
60+
```
61+
62+
### 复杂度分析
63+
64+
- 时间复杂度
65+
- 空间复杂度
66+

0 commit comments

Comments
 (0)