Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Lintcode13 strStr solution problem solution

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

[topic description]

For a given source string and a target string, you should output the first index (from 0) of target string in source string.

If target does not exist in source, just return-1.

For a given source string and a target string, you should find the first position in the source string where the target string appears (starting at 0). If it does not exist, return-1.

[topic link]

Http://www.lintcode.com/en/problem/strstr/

[topic Analysis]

Double for loops can be used to solve the string lookup problem, and the KMP algorithm is more efficient.

Source code analysis

1. Boundary check: source and target may be empty strings.

two。 Flag overflow under boundary check: pay attention to the loop judgment condition of variable I, if it is simple I < source.length (), it may overflow in the following source.charAt (I + j).

3. Code style: (1) operator = = both sides should be added spaces; (2) variable names not from such as s1``s2, be meaningful, such as target`source; (3) even if there is only one sentence in the if statement also want to increase parentheses, that is {return-1;}; (4) Java code curly braces generally on the right side of the same line, C++ code curly braces usually start another line; (5) int I, j; statement before a line of spaces, is a good code style.

4. Do not declare iQuery j in the for condition, it is easy to cause compilation errors when it is reused outside the loop.

[answer link]

Http://www.jiuzhang.com/solutions/strstr/

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report