File tree 5 files changed +104
-9
lines changed
5 files changed +104
-9
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @Author : wangshan
3
+ * @Date : 2021-11-28 16:57:18
4
+ * @LastEditors : wangshan
5
+ * @LastEditTime : 2021-11-28 22:59:24
6
+ * @Description : 字符串匹配(模式匹配算法)
7
+ */
8
+
9
+ export function index ( fstr : string , sstr : string , pos : number ) {
10
+ // debugger;
11
+ let i = pos ;
12
+ let j = 1 ;
13
+ let flen = fstr . length ;
14
+ let slen = sstr . length ;
15
+
16
+ while ( i <= flen && j <= slen ) {
17
+ if ( fstr [ i - 1 ] === sstr [ j - 1 ] ) {
18
+ i ++ ;
19
+ j ++ ;
20
+ } else {
21
+ i = i - j + 2 ;
22
+ j = 1 ;
23
+ }
24
+ }
25
+
26
+ if ( j > slen ) {
27
+ return i - slen ;
28
+ } else {
29
+ return 0 ;
30
+ }
31
+ }
32
+
33
+ // 算法二,查找指定位置之后的第一个匹配的子串.
34
+ /**
35
+ *
36
+ * @param {string } target 主串
37
+ * @param {string } substr 子串
38
+ * @param {number } pos 指定位置
39
+ *
40
+ * @returns i
41
+ */
42
+
43
+ export function Index ( target : string , substr : string , pos : number ) {
44
+ // debugger;
45
+ let tlen = target . length ;
46
+ let sublen = substr . length ;
47
+ let i = pos ;
48
+ if ( pos > 0 ) {
49
+ while ( i <= tlen - sublen + 1 ) {
50
+ // 这里循环条件的内表达式意思是,计算pos位置的范围,应当在满足字串长度时的查找范围
51
+ let sub = target . substr ( i - 1 , sublen ) ;
52
+
53
+ if ( sub === substr ) {
54
+ return i ;
55
+ } else {
56
+ i ++ ;
57
+ }
58
+ }
59
+ }
60
+
61
+ return - 1 ;
62
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @Author : wangshan
3
+ * @Date : 2021-11-20 23:18:00
4
+ * @LastEditors : wangshan
5
+ * @LastEditTime : 2021-11-28 22:49:09
6
+ * @Description :串匹配算法测试-方法一
7
+ */
8
+ import { index } from "../../dataStructure/string/StringSearch" ;
9
+
10
+ let tstr = "hello wolrd" ;
11
+
12
+ let needfindStr = "wo" ;
13
+
14
+ console . log ( index ( tstr , needfindStr , 3 ) ) ;
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @Author : wangshan
3
+ * @Date : 2021-11-28 22:49:23
4
+ * @LastEditors : wangshan
5
+ * @LastEditTime : 2021-11-28 23:01:18
6
+ * @Description : 字串模式匹配算法二
7
+ */
8
+
9
+ import { Index } from "../../dataStructure/string/StringSearch" ;
10
+
11
+ // 测试数据
12
+ let target = "hello world, lo" ;
13
+
14
+ // console.log(Index(target, "wo", 3)); // 7
15
+ // console.log(Index(target, "lo", 1)); // 4
16
+ //空白串
17
+ console . log ( Index ( target , " " , 2 ) ) ; // 6
18
+ // 测试查找不存在的子串
19
+ console . log ( Index ( target , "cd" , 4 ) ) ; // -1
20
+
21
+ // 测试成功
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 2
2
* @Author : wangshan
3
3
* @Date : 2021-06-22 00:27:02
4
4
* @LastEditors : wangshan
5
- * @LastEditTime : 2021-11-20 21:09:27
5
+ * @LastEditTime : 2021-11-28 22:51:39
6
6
* @Description : 入口文件
7
7
*/
8
8
// import "@/utils/index"; // 导入自定义模块
19
19
// import "./examples/stack/03";
20
20
// import "./examples/stack/04";
21
21
// import "./examples/queue/01";
22
- import "./examples/queue/02" ;
22
+ // import "./examples/queue/02";
23
+
24
+ // str
25
+
26
+ // import "./examples/string/01";
27
+ import "./examples/string/02" ;
You can’t perform that action at this time.
0 commit comments