Skip to content

Commit 902b1d0

Browse files
committed
leetcode
1 parent 90b16a7 commit 902b1d0

File tree

6 files changed

+261
-22
lines changed

6 files changed

+261
-22
lines changed

leetcode/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,61 @@ double findKth(int a[], int m, int b[], int n, int k){
468468
* [Longest Palindromic Substring](https://oj.leetcode.com/problems/longest-palindromic-substring/) [Manacher’s Algorithm](http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html)算法。能在O(N)的复杂度内找到一个字符串的最长回文子串。假设用动态规划或者暴力,复杂度都为O(N^2). 后缀数组也能解决这个问题。
469469
470470
471+
* [Regular Expression Matching ](https://oj.leetcode.com/problems/regular-expression-matching/)没找到什么线性算法。 暴力算法来源于leetcode,假设`s=abbbbbbbbbbbbbbbbbb`, `t=ab*cd`这样的话。可能会出现如下情况:当有一个`*`字符时,最坏的情况下会达到O(n*m),这还的用KMP算法去做字串匹配. 当多个 `\*`字符时,就是`\*`的指数级复杂度了。
472+
473+
```
474+
s = abbbbbbbbbbbbbbbbbb
475+
^
476+
t = acd
477+
478+
s = abbbbbbbbbbbbbbbbbb
479+
^
480+
t = a cd
481+
482+
s = abbbbbbbbbbbbbbbbbb
483+
^
484+
t = a cd
485+
486+
s = abbbbbbbbbbbbbbbbbb
487+
^
488+
t = a cd
489+
490+
....
491+
```
492+
493+
494+
* [Scramble String ](https://oj.leetcode.com/problems/scramble-string/) DFS 类似卡特兰解空间搜索加上减枝,减枝技巧包括: 两串长度相等; 两串无序hash值相等; 两串同类字符数必须相等。
495+
复杂度分析:
496+
497+
```
498+
h[n] = 2 * sum( h[i] * h[n-i] ) (1<= i < n )
499+
```
500+
501+
* [Recover Binary Search Tree ](https://oj.leetcode.com/problems/recover-binary-search-tree/)
502+
503+
假设一个序列 , 其中 8 和 14 被误交换了。 如图, 导致序列会有两个`>` , 记录第1个`>`的前驱和第2个`>`的后继两个指针,交换即可.
504+
505+
```
506+
# noraml
507+
508+
< < < < < <
509+
1 5 8 10 11 14 23
510+
511+
# missing swap
512+
513+
< < > < > <
514+
1 5 14 10 11 8 23
515+
^ ^
516+
517+
# after
518+
519+
< < > < > <
520+
1 5 8 10 11 14 23
521+
^ ^
522+
```
523+
524+
525+
* [Minimum Window Substring](https://oj.leetcode.com/problems/minimum-window-substring/) 字符串滑动窗口, 维护4个变量: left 左端点 ; right 右端点 ; cur[256] 各字符当前窗口内的计数值; curlen 当前窗口内目标串字符的个数。 时间复杂度O(N). 类似的题: [Substring with Concatenation of All Words ](https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/), [Longest Substring Without Repeating Characters ](https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/) .
471526
472527
473528

leetcode/a.out

13.7 KB
Binary file not shown.

leetcode/minimum-window-substring.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,35 @@ using namespace std;
1515
class Solution {
1616
public:
1717
string minWindow(string S, string T) {
18-
int anslen = S.size() + 1 , start, end, i = 0, j = 0 , curlen = 0;
19-
unordered_map<char, int> tm , dict ;
20-
for(i = 0 ; i < T.size(); ++ i) tm[ T[i] ] ++ ;
21-
for(i = 0 ; i < S.size(); ++ i){
22-
if( tm[ S[i] ] == 0 ) continue;
23-
while( j < i && ( dict[ S[i] ] >= tm[ S[i] ] || tm[ S[j] ] == 0)) {
24-
j ++ ;
25-
curlen -- ;
26-
if( dict[ S[j] ] > 0 ) dict[ S[j] ] -- ;
27-
}
28-
dict[ S[i] ] ++ ;
29-
curlen ++;
30-
if(curlen >= T.size()){
31-
if( anslen > (i - j + 1)){
32-
anslen = i - j + 1;
33-
start = j ;
34-
end = i ;
35-
}
36-
}
37-
}
38-
cout << "answer length:" << anslen << " start:" << start << " end:" << end << endl;
39-
return anslen != S.size() + 1 ? S.substr(start, end - start + 1): "";
18+
int i, n , m , l, r, curlen, len, start;
19+
int need[256] = {0}, cur[256] = {0};
20+
n = S.size(); m = T.size();
21+
for(i = 0 ; i < m; ++ i)
22+
need[T[i]] ++ ;
23+
len = n + 1 ; start = 0;
24+
for(curlen = l = r = 0 ; r < n ; ++ r){
25+
if(need[ S[r] ] == 0 ) continue;
26+
if( ++ cur[ S[r] ] <= need[ S[r] ])
27+
++ curlen;
28+
if(curlen == m){
29+
while( need[ S[l] ] == 0 || cur[ S[l] ] > need[ S[l] ]) {
30+
if( cur[ S[l] ] > need[ S[l] ]) --cur[ S[l] ];
31+
++ l;
32+
}
33+
if( r - l + 1 < len){
34+
len = r - l + 1;
35+
start = l ;
36+
}
37+
}
38+
}
39+
return len == n + 1 ? "" : S.substr(start, len);
4040
}
4141
};
4242

4343
int main(){
4444
Solution sol;
4545
cout << sol.minWindow("a", "a") << endl;
4646
cout << sol.minWindow("ADOBECODEBANC", "ABC") << endl;
47+
cout << sol.minWindow("acbbaca", "aba") << endl;
4748
return 0;
4849
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
8+
struct TreeNode{
9+
int val;
10+
TreeNode *left;
11+
TreeNode *right;
12+
TreeNode(int x): val(x), left(NULL), right(NULL){}
13+
};
14+
15+
/**
16+
* Definition for binary tree
17+
* struct TreeNode {
18+
* int val;
19+
* TreeNode *left;
20+
* TreeNode *right;
21+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
22+
* };
23+
*/
24+
class Solution {
25+
public:
26+
27+
void traval(TreeNode *root){
28+
if(root == NULL) return;
29+
traval(root->left);
30+
if(prev == NULL){
31+
prev = root;
32+
}else{
33+
if(prev->val > root->val){
34+
if(first == NULL){
35+
first = prev;
36+
}
37+
second = root;
38+
}
39+
prev = root;
40+
}
41+
traval(root->right);
42+
}
43+
44+
void recoverTree(TreeNode *root) {
45+
first = second = prev = NULL;
46+
traval(root);
47+
if(first != NULL ){
48+
swap(first->val, second->val);
49+
}
50+
}
51+
private:
52+
TreeNode *first, *second, *prev;
53+
};
54+
55+
56+
void dfs(TreeNode * root, int &index){
57+
if(root == NULL) return;
58+
dfs(root->left, index);
59+
cout << "## " << (++index) << ":"<< root->val << endl ;
60+
dfs(root->right, index);
61+
}
62+
63+
void test1(){
64+
int n , x ;
65+
TreeNode *a1, *a3, *a6, *a8, *a7, *a4, *a10 , *a14, *a13;
66+
a1 = new TreeNode(1);
67+
a3 = new TreeNode(3);
68+
a6 = new TreeNode(6);
69+
a8 = new TreeNode(8);
70+
a7 = new TreeNode(7);
71+
a4 = new TreeNode(4);
72+
a10 = new TreeNode(10);
73+
a14 = new TreeNode(14);
74+
a13 = new TreeNode(13);
75+
a4->left = a3 ;
76+
a4->right = a10;
77+
a3->left = a1 ;
78+
a3->right = a6;
79+
a6->left = a8 ;
80+
a6->right = a7 ;
81+
a10->right = a14;
82+
a14->left = a13;
83+
int index = 0 ;
84+
dfs(a4, index);
85+
86+
Solution sol;
87+
sol.recoverTree(a4);
88+
89+
index = 0;
90+
dfs(a4, index);
91+
}
92+
93+
94+
void test2(){
95+
TreeNode *a0, *a1;
96+
a0 = new TreeNode(0);
97+
a1 = new TreeNode(1);
98+
a0->left = a1;
99+
100+
Solution sol;
101+
sol.recoverTree(a0);
102+
103+
int index = 0;
104+
dfs(a0, index);
105+
}
106+
107+
int main(){
108+
test2();
109+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <algorithm>
2+
#include <vector>
3+
#include <string>
4+
#include <cstring>
5+
#include <iostream>
6+
#include <cassert>
7+
8+
using namespace std;
9+
10+
class Solution {
11+
public:
12+
bool isMatch(const char *s, const char *p) {
13+
assert(s && p);
14+
if (*p == '\0') return *s == '\0';
15+
if (*(p+1) != '*') {
16+
assert(*p != '*');
17+
return ((*p == *s) || (*p == '.' && *s != '\0')) && isMatch(s+1, p+1);
18+
}
19+
while ((*p == *s) || (*p == '.' && *s != '\0')) {
20+
if (isMatch(s, p+2)) return true;
21+
s++;
22+
}
23+
return isMatch(s, p+2);
24+
}
25+
};
26+
27+
28+
int main(){
29+
Solution sol;
30+
cout << sol.isMatch("helloworld", "hb*...*ld") << endl;
31+
return 0;
32+
}

leetcode/scramble-string.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <algorithm>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <string>
5+
#include <cstring>
6+
#include <iostream>
7+
8+
using namespace std;
9+
10+
class Solution {
11+
public:
12+
bool isScramble(string s1, string s2) {
13+
int i, n = s1.size(), m = s2.size(), val1 = 0, val2 = 0;
14+
string sl1, sr1, sl2, sr2;
15+
if( n != m ) return false;
16+
if(s1 == s2) return true;
17+
for( i = 0 ; i < n ; ++ i){
18+
val1 += s1[i] - '0';
19+
val2 += s2[i] - '0';
20+
}
21+
if(val1 != val2) return false;
22+
for(int i = 1; i < n ; ++ i){
23+
sl1 = s1.substr(0, i); sr1 = s1.substr(i);
24+
sl2 = s2.substr(0, i); sr2 = s2.substr(i);
25+
if(isScramble(sl1,sl2) && isScramble(sr1, sr2))
26+
return true;
27+
sl2 = s2.substr(n-i); sr2 = s2.substr(0, n-i);
28+
if(isScramble(sl1, sl2) && isScramble(sr1, sr2))
29+
return true;
30+
}
31+
return false;
32+
}
33+
};
34+
35+
int main(){
36+
string a, b ;
37+
Solution sol;
38+
cout << sol.isScramble("great", "rgeat") << endl;
39+
cout << sol.isScramble("rgtae", "great") << endl;
40+
cout << sol.isScramble("rgtae", "geart") << endl;
41+
return 0;
42+
}

0 commit comments

Comments
 (0)