|
1 | | -# Compare Strings - 字符串查找 |
2 | | - |
3 | | -## strstr |
4 | | - |
5 | | -## Source |
6 | | - |
7 | | -- lintcode: [lintcode - (13) strstr](http://www.lintcode.com/zh-cn/problem/strstr/) |
8 | | - |
9 | | -``` |
10 | | -strstr (a.k.a find sub string), is a useful function in string operation. You task is to implement this function. |
11 | | -
|
12 | | -For a given source string and a target string, you should output the "first" index(from 0) of target string in source string. |
13 | | -
|
14 | | -If target is not exist in source, just return -1. |
15 | | -
|
16 | | -Example |
17 | | -If source="source" and target="target", return -1. |
18 | | -
|
19 | | -If source="abcdabcdefg" and target="bcd", return 1. |
20 | | -
|
21 | | -Challenge |
22 | | -O(n) time. |
23 | | -
|
24 | | -Clarification |
25 | | -Do I need to implement KMP Algorithm in an interview? |
26 | | -
|
27 | | - - Not necessary. When this problem occurs in an interview, the interviewer just want to test your basic implementation ability. |
28 | | -``` |
29 | | - |
30 | | -## 题解 |
31 | | - |
32 | | -对于字符串查找问题,可使用双重for循环解决,效率更高的则为KMP算法。 |
33 | | - |
34 | | -### Java |
35 | | - |
36 | | -```java |
37 | | -/** |
38 | | - * http://www.jiuzhang.com//solutions/implement-strstr |
39 | | - */ |
40 | | -class Solution { |
41 | | - /** |
42 | | - * Returns a index to the first occurrence of target in source, |
43 | | - * or -1 if target is not part of source. |
44 | | - * @param source string to be scanned. |
45 | | - * @param target string containing the sequence of characters to match. |
46 | | - */ |
47 | | - public int strStr(String source, String target) { |
48 | | - if (source == null || target == null) { |
49 | | - return -1; |
50 | | - } |
51 | | - |
52 | | - int i, j; |
53 | | - for (i = 0; i < source.length() - target.length() + 1; i++) { |
54 | | - for (j = 0; j < target.length(); j++) { |
55 | | - if (source.charAt(i + j) != target.charAt(j)) { |
56 | | - break; |
57 | | - } //if |
58 | | - } //for j |
59 | | - if (j == target.length()) { |
60 | | - return i; |
61 | | - } |
62 | | - } //for i |
63 | | - |
64 | | - // did not find the target |
65 | | - return -1; |
66 | | - } |
67 | | -} |
68 | | -``` |
69 | | - |
70 | | -### 源码分析 |
71 | | - |
72 | | -1. 边界检查:`source`和`target`有可能是空串。 |
73 | | -2. 边界检查之下标溢出:注意变量`i`的循环判断条件,如果是单纯的`i < source.length()`则在后面的`source.charAt(i + j)`时有可能溢出。 |
74 | | -2. 代码风格:(1)运算符`==`两边应加空格;(2)变量名不要起`s1``s2`这类,要有意义,如`target``source`;(3)即使if语句中只有一句话也要加大括号,即`{return -1;}`;(4)Java 代码的大括号一般在同一行右边,C++ 代码的大括号一般另起一行;(5)`int i, j;`声明前有一行空格,是好的代码风格。 |
75 | | -3. 不要在for的条件中声明`i`,`j`,容易在循环外再使用时造成编译错误,错误代码示例: |
76 | | - |
77 | | -### Another Similar Question |
78 | | - |
79 | | -```java |
80 | | -/** |
81 | | - * http://www.jiuzhang.com//solutions/implement-strstr |
82 | | - */ |
83 | | -public class Solution { |
84 | | - public String strStr(String haystack, String needle) { |
85 | | - if(haystack == null || needle == null) { |
86 | | - return null; |
87 | | - } |
88 | | - int i, j; |
89 | | - for(i = 0; i < haystack.length() - needle.length() + 1; i++) { |
90 | | - for(j = 0; j < needle.length(); j++) { |
91 | | - if(haystack.charAt(i + j) != needle.charAt(j)) { |
92 | | - break; |
93 | | - } |
94 | | - } |
95 | | - if(j == needle.length()) { |
96 | | - return haystack.substring(i); |
97 | | - } |
98 | | - } |
99 | | - return null; |
100 | | - } |
101 | | -} |
102 | | -``` |
103 | | - |
104 | | -## Compare Strings |
| 1 | +# Compare Strings |
105 | 2 |
|
106 | 3 | ## Source |
107 | 4 |
|
@@ -164,7 +61,7 @@ public: |
164 | 61 | }; |
165 | 62 | ``` |
166 | 63 |
|
167 | | -#### 源码解析 |
| 64 | +### 源码解析 |
168 | 65 |
|
169 | 66 | 使用数组`freqA`和`freqB`分别保存A和B中各字母出现的频次,随后遍历比较两数组,若A中相应的频次小于B时,返回false,否则遍历完后返回true. |
170 | 67 |
|
|
0 commit comments