原题目
LeetCode原题地址 : 1455. 检查单词是否为句中其他单词的前缀
还是业务代码写多了,在看到这道题的时候,还奇怪了一下这种题目有什么好出的,然后飞快给出了ac代码。
1 |
|
然后就去看题解,发现Java语言的题解没有使用startsWith(),还是自己写了比对代码()。
1 |
|
官方题解中没有使用任何包装好的算法,将split和startsWith都自行实现了。看到这里的时候我开始回忆这两个方法的实现,竟然也要像做题一样重新去想。
startsWith()
调用入口如下:
1 |
|
实现方法如下:
1 |
|
转char遍历实现比较,O(n)
split()
看完startsWith()之后又顺带看了一下split底层实现,基于正则的实现,防御性做得很不错。
1 |
|