对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1

基本:两重for循环,时间复杂度O(n^2)。

  1. class Solution {
  2. /**
  3. * Returns a index to the first occurrence of target in source,
  4. * or -1 if target is not part of source.
  5. * @param source string to be scanned.
  6. * @param target string containing the sequence of characters to match.
  7. */
  8. public int strStr(String source, String target) {
  9. //write your code here
  10. if (source == null || target == null) {
  11. return -1;
  12. }
  13. for (int i = 0; i < source.length() - target.length() + 1; i++) {
  14. int j = 0;
  15. for (j = 0; j < target.length(); j++) {
  16. if (source.charAt(i + j) != target.charAt(j)) {
  17. break;
  18. }
  19. }
  20. if (j == target.length()) {
  21. return i;
  22. }
  23. }
  24. return -1;
  25. }
  26. }

高级:Rabin Karp算法,时间复杂度为O(n+m),n为源字符串长度,m为目标字符串长度。该算法时间复杂度与KMP算法一样,但是比KMP简单,不建议使用KMP,不仅写起来麻烦而且容易错。

  1. public class Solution {
  2. private static int BASE = 1000000;
  3. /**
  4. * @param source a source string
  5. * @param target a target string
  6. * @return an integer as index
  7. */
  8. public int strStr2(String source, String target) {
  9. // Write your code here
  10. if (source == null || target == null) {
  11. return -1;
  12. }
  13. int m = target.length();
  14. if (m == 0) {
  15. return 0;
  16. }
  17. //31 ^ m
  18. int power = 1;
  19. for (int i = 0; i < m; i++) {
  20. power = (power * 31) % BASE;
  21. }
  22. //targetCode
  23. int targetCode = 0;
  24. for (int i = 0; i < m; i++) {
  25. targetCode = (targetCode * 31 + target.charAt(i)) % BASE;
  26. }
  27. //hashCode
  28. int hashCode = 0;
  29. for (int i = 0; i < source.length(); i++) {
  30. hashCode = (hashCode * 31 + source.charAt(i)) % BASE;
  31. if (i < m - 1) {
  32. continue;
  33. }
  34. if (i >= m) {
  35. hashCode = hashCode - (source.charAt(i - m) * power) % BASE;
  36. if (hashCode < 0) {
  37. hashCode += BASE;
  38. }
  39. }
  40. if (targetCode == hashCode) {
  41. if (source.substring(i - m + 1, i + 1).equals(target)) {
  42. return i - m + 1;
  43. }
  44. }
  45. }
  46. return -1;
  47. }
  48. }

LintCode ---- 刷题总结的更多相关文章

  1. lintcode 刷题 by python 总结(1)

    博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...

  2. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  3. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  4. lintcode 刷题 by python 部分链表题总结(2)

    本篇博客对最近做的链表的算法题做个简单的小结,主要描述题目和提供解题思路,具体代码见我的 github:https://github.com/MUSK1881/lintcode-by-python 3 ...

  5. LintCode刷题指南:字符串处理(C++,Python)

    题目:两个字符串是变位词 题目难度:简单 题目描述: 写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 解题思路: C++:引入哈希的思维,这道题就 ...

  6. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  7. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  8. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  9. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  10. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

随机推荐

  1. hadoop编程技巧(3)---定义自己的区划类别Partitioner

    Hadoop代码测试环境:Hadoop2.4 原则:在Hadoop的MapReduce过程.Mapper阅读过程完成后数据.它将数据发送到Partitioner.由Partitioner每个记录应当采 ...

  2. 站点维护使用app_offline.htm页面提供友好的更新提示

    进行站点维护时为了以一个友好的方式提示给用户,比如什么“本网站正在更新”等等的信息可以建立一个叫app_offline.htm 的静态HTM页面文件,其中修改成你要临时显示的内容,将其放在你的应用的根 ...

  3. 一步一步实现基于Task的Promise库(五)waitFor和waitForAny的实现

    在实现waitFor方法之前,我们先要搞明白下面这些问题: 1. waitFor方法的形参有限制吗? 没有!如果形参是Task类型,不应该启动Task,如果是function类型,会执行方法.所以wa ...

  4. oracle 表导入到powerDesigner 中

    最近不忙,之前一直是用powerDesigner看表结构,还没自己导入过,今天试试 oracle 表导入到powerDesigner 中步骤: 1.File--->reverse Enginne ...

  5. 5 MySQL索引

    目录: 1. 索引概述 1.1 为什么引入索引 1.2 什么是索引 1.3 索引的好处 1.4 索引的不足 1.5 索引分类 2. 索引设计原则 3. 索引建立和删除 3.1 索引创建 3.2 索引删 ...

  6. 高并发非自增ID如何设计?

    博友们一起来讨论下高并发非自增ID如何设计? 底层是很重要的,我最近设计底层,通用底层. 我想跟大家谈论下这个话题: 如何在高并发环境下设计出一套好用的非自增ID的添加操作的解决方案?更新的操作我随机 ...

  7. Spring in action (1)

      spring中注入bean的方法: 1.通过xml文件来注入bean. 2.通过java注解来注入 默认的bean生命周期是单例的.每次都会返回相同的类的实例.

  8. windbg Symbol file path

    SOS是一个调试器扩展,用于调试.NET应用程序.它提供了一组非常丰富的命令,这些命令使开发人员可以对CLR进行深入分析,并且有助于找出应用程序中各种复杂错误的原因.   由于SOS能够提供CLR内部 ...

  9. Mahout之(三)相似性度量

    User CF 和 Item CF 都依赖于相似度的计算,因为只有通过衡量用户之间或物品之间的相似度,才能找到用户的“邻居”,才能完成推荐.上文简单的介绍了相似性的计算,但不完全,下面就对常用的相似度 ...

  10. 配置 SQL Server Email 发送以及 Job 的 Notification通知功能

    配置 SQL Server Email 发送以及 Job 的 Notification通知功能 在与数据库相关的项目中, 比如像数据库维护, 性能警报, 程序出错警报或通知都会使用到在 SQL Ser ...