基础算法-字符串
基本的数据结构和算法-String; String经常出现在面试题,平常用的也比较多,这里总结其用法和常见题目.
Java字符串常见用法
Implement strStr
Question:
- Problem Statement:Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
- leetcode: Implement strStr()
- lintcode: strstr
- Difficult::Easy; kmp算法需详细看;
Solution
- kmp算法理解
- 参考 [code lang=java] public class StrStr { /** * 方法1,直接用java indexOf函数 * 测试: LeetCode: 6ms; */ public static int indexOf(String source, String target) { return source.indexOf(target); } /** * 方法2,暴力匹配, 时间复杂度最坏情况下为O(m*(m-n+1)) * 测试: LeetCode: 10ms * 分析: * 1. 双重循环, 外层注意i<source.length()-target.length()+1,否则可能溢出 * 2. 内层注意j不在for中声明, 返回时判断条件为j==length() */ public static int forceSearch(String source, String target) { for (int i = 0; i < source.length() - target.length() + 1; i++) { int j = 0; for (; j < target.length(); j++) if (target.charAt(j) != source.charAt(i + j)) break; if (j == target.length()) return i; } return -1; } /** * KMP算法 * 测试: LeetCode: 18ms * * @see * <a href="http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html">kmp算法理解</a> * 分析: 1. next数组计算: 已知next[i-1] = j,利用递归的思想求出next[i]的值 */ public static int kmp(String source, String target) { int[] next = next(target); int i = 0, j = 0; while (i < source.length() && j < target.length()) { if (source.charAt(i) == target.charAt(j)) { i++; j++; } else if (j == 0) { i++; } else { j = next[j - 1]; } } if (j == target.length()) return i - j; return -1; } public static int[] next(String target) { int[] next = new int[target.length()]; int i = 1, j = 0; while (i < target.length()) { if (target.charAt(i) == target.charAt(j)) { next[i] = j + 1; j = next[i]; i++; } else if (j == 0) { next[i] = 0; i++; } else { j = next[j - 1]; } } return next; } public static void main(String[] args) { } } [/code]
Valid Palindrome
Question:
Solution
- 思路: 首尾指针法,忽略大小写,值考虑字母和数字(运用Character API); 两部走:1,找到最左和最右的第一个合法字母(字母或者字符);2,一致转换为小写进行比较
[code lang=java] public class Solution { public boolean isPalindrome(String s) { if(s==null||s.isEmpty()) return true; int l = 0, r = s.length() - 1; while (l < r) { if(!Character.isLetterOrDigit(s.charAt(l))){ l++; continue; } if(!Character.isLetterOrDigit(s.charAt(r))){ r--; continue; } if (Character.toLowerCase(s.charAt(l)) == Character.toLowerCase(s.charAt(r))) { l++; r--; }else { return false; } } return true; } } [/code]