In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.