-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 字符集的方式 - 字符集配合String内置方法实现.
- Loading branch information
wangshan
committed
Nov 28, 2021
1 parent
71aee54
commit 8c3891e
Showing
5 changed files
with
104 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* @Author: wangshan | ||
* @Date: 2021-11-28 16:57:18 | ||
* @LastEditors: wangshan | ||
* @LastEditTime: 2021-11-28 22:59:24 | ||
* @Description: 字符串匹配(模式匹配算法) | ||
*/ | ||
|
||
export function index(fstr: string, sstr: string, pos: number) { | ||
// debugger; | ||
let i = pos; | ||
let j = 1; | ||
let flen = fstr.length; | ||
let slen = sstr.length; | ||
|
||
while (i <= flen && j <= slen) { | ||
if (fstr[i - 1] === sstr[j - 1]) { | ||
i++; | ||
j++; | ||
} else { | ||
i = i - j + 2; | ||
j = 1; | ||
} | ||
} | ||
|
||
if (j > slen) { | ||
return i - slen; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
// 算法二,查找指定位置之后的第一个匹配的子串. | ||
/** | ||
* | ||
* @param {string} target 主串 | ||
* @param {string} substr 子串 | ||
* @param {number} pos 指定位置 | ||
* | ||
* @returns i | ||
*/ | ||
|
||
export function Index(target: string, substr: string, pos: number) { | ||
// debugger; | ||
let tlen = target.length; | ||
let sublen = substr.length; | ||
let i = pos; | ||
if (pos > 0) { | ||
while (i <= tlen - sublen + 1) { | ||
// 这里循环条件的内表达式意思是,计算pos位置的范围,应当在满足字串长度时的查找范围 | ||
let sub = target.substr(i - 1, sublen); | ||
|
||
if (sub === substr) { | ||
return i; | ||
} else { | ||
i++; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* @Author: wangshan | ||
* @Date: 2021-11-20 23:18:00 | ||
* @LastEditors: wangshan | ||
* @LastEditTime: 2021-11-28 22:49:09 | ||
* @Description:串匹配算法测试-方法一 | ||
*/ | ||
import { index } from "../../dataStructure/string/StringSearch"; | ||
|
||
let tstr = "hello wolrd"; | ||
|
||
let needfindStr = "wo"; | ||
|
||
console.log(index(tstr, needfindStr, 3)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* @Author: wangshan | ||
* @Date: 2021-11-28 22:49:23 | ||
* @LastEditors: wangshan | ||
* @LastEditTime: 2021-11-28 23:01:18 | ||
* @Description: 字串模式匹配算法二 | ||
*/ | ||
|
||
import { Index } from "../../dataStructure/string/StringSearch"; | ||
|
||
// 测试数据 | ||
let target = "hello world, lo"; | ||
|
||
// console.log(Index(target, "wo", 3)); // 7 | ||
// console.log(Index(target, "lo", 1)); // 4 | ||
//空白串 | ||
console.log(Index(target, " ", 2)); // 6 | ||
// 测试查找不存在的子串 | ||
console.log(Index(target, "cd", 4)); // -1 | ||
|
||
// 测试成功 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters