[NineChap 1.1] strStr and Coding Style
来源:互联网 发布:c语言死机病毒怎么处理 编辑:程序博客网 时间:2024/06/14 07:19
strStr Question
Implement strStr
Before solving the problem, it’s very important to ask this questions:
Can we use system library (eg. str.substring() or indexOf())
The answer is ‘No’, but asking this question shows that you think deep into questions. Keep in mind the characteristics for a candidate is judged by 4 things:
- Code readability – how long does it take to review your code?
- Coding convention – do you have the habit to write efficient code?
- Testing – it’s good habit to write test case before asked to.
- Communication skills
Solution of this question is double for-loop. It’s not linear time complexity, but it’s OK. After getting the code correct, it’s best to tell the O(n) time KMP solution. However, it’s most probably not required to implement.
my code
public String strStr(String haystack, String needle) { if (haystack == null || needle == null) { return null; } int len1 = haystack.length(); int len2 = needle.length(); for (int i = 0; i <= len1 - len2; i ++) { // start from i, match chars boolean match = true; for (int j = 0; j < len2; j ++) { if (haystack.charAt(i + j) != needle.charAt(j)) { match = false; break; } } if (match) { return haystack.substring(i); } } return null;}
alternative code from ninechap
public String strStr(String haystack, String needle) { if(haystack == null || needle == null) { return null; } int i, j; for(i = 0; i < haystack.length() - needle.length() + 1; i++) { for(j = 0; j < needle.length(); j++) { if(haystack.charAt(i + j) != needle.charAt(j)) { break; } } if(j == needle.length()) { return haystack.substring(i); } } return null;}
Google Coding Style
if … else …
if (condition) { statements;} else if (condition) { statements;} else { statements;}
自增/自减
while (d++ = s++) { n++;}
To be continued.
0 0
- [NineChap 1.1] strStr and Coding Style
- Lisp programming style and coding efficiency
- Coding Style
- coding style
- coding-style
- 《Recommended C Style and Coding Standards》学习总结
- 史上最全代码风格大全!!!Coding Style Conventions and Standards
- C# Coding Style Guide
- C# Coding Style Guide
- C# Coding Style Guide
- Mozilla Coding Style Guide
- LINUX KERNEL CODING STYLE
- Linux kernel coding style
- C# Coding Style Guide
- Coding Style Conventions
- Python[三]:Coding Style
- Linux Coding Style
- Linux kernel coding style
- 回溯法 解决堡垒问题
- BZOJ1207【HNOI2004】打地鼠
- iptables 的mangle表
- LayoutInflater动态载入
- 友元类
- [NineChap 1.1] strStr and Coding Style
- Coursera课程-r语言-程序作业1
- linux里的shell环境变量
- Android——Intent启动
- python安装beautifulsoup
- VC编辑控件Edit基本操作
- 获取CPUID信息
- (4.4.1.4)图片缓存之内存缓存技术LruCache,软引用
- c++ map 插入元素过程分析