LintCode ---- 刷题总结
对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1
。
基本:两重for循环,时间复杂度O(n^2)。
- class Solution {
- /**
- * Returns a index to the first occurrence of target in source,
- * or -1 if target is not part of source.
- * @param source string to be scanned.
- * @param target string containing the sequence of characters to match.
- */
- public int strStr(String source, String target) {
- //write your code here
- if (source == null || target == null) {
- return -1;
- }
- for (int i = 0; i < source.length() - target.length() + 1; i++) {
- int j = 0;
- for (j = 0; j < target.length(); j++) {
- if (source.charAt(i + j) != target.charAt(j)) {
- break;
- }
- }
- if (j == target.length()) {
- return i;
- }
- }
- return -1;
- }
- }
高级:Rabin Karp算法,时间复杂度为O(n+m),n为源字符串长度,m为目标字符串长度。该算法时间复杂度与KMP算法一样,但是比KMP简单,不建议使用KMP,不仅写起来麻烦而且容易错。
- public class Solution {
- private static int BASE = 1000000;
- /**
- * @param source a source string
- * @param target a target string
- * @return an integer as index
- */
- public int strStr2(String source, String target) {
- // Write your code here
- if (source == null || target == null) {
- return -1;
- }
- int m = target.length();
- if (m == 0) {
- return 0;
- }
- //31 ^ m
- int power = 1;
- for (int i = 0; i < m; i++) {
- power = (power * 31) % BASE;
- }
- //targetCode
- int targetCode = 0;
- for (int i = 0; i < m; i++) {
- targetCode = (targetCode * 31 + target.charAt(i)) % BASE;
- }
- //hashCode
- int hashCode = 0;
- for (int i = 0; i < source.length(); i++) {
- hashCode = (hashCode * 31 + source.charAt(i)) % BASE;
- if (i < m - 1) {
- continue;
- }
- if (i >= m) {
- hashCode = hashCode - (source.charAt(i - m) * power) % BASE;
- if (hashCode < 0) {
- hashCode += BASE;
- }
- }
- if (targetCode == hashCode) {
- if (source.substring(i - m + 1, i + 1).equals(target)) {
- return i - m + 1;
- }
- }
- }
- return -1;
- }
- }
LintCode ---- 刷题总结的更多相关文章
- lintcode 刷题 by python 总结(1)
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- lintcode 刷题 by python 部分链表题总结(2)
本篇博客对最近做的链表的算法题做个简单的小结,主要描述题目和提供解题思路,具体代码见我的 github:https://github.com/MUSK1881/lintcode-by-python 3 ...
- LintCode刷题指南:字符串处理(C++,Python)
题目:两个字符串是变位词 题目难度:简单 题目描述: 写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 解题思路: C++:引入哈希的思维,这道题就 ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
随机推荐
- hadoop编程技巧(3)---定义自己的区划类别Partitioner
Hadoop代码测试环境:Hadoop2.4 原则:在Hadoop的MapReduce过程.Mapper阅读过程完成后数据.它将数据发送到Partitioner.由Partitioner每个记录应当采 ...
- 站点维护使用app_offline.htm页面提供友好的更新提示
进行站点维护时为了以一个友好的方式提示给用户,比如什么“本网站正在更新”等等的信息可以建立一个叫app_offline.htm 的静态HTM页面文件,其中修改成你要临时显示的内容,将其放在你的应用的根 ...
- 一步一步实现基于Task的Promise库(五)waitFor和waitForAny的实现
在实现waitFor方法之前,我们先要搞明白下面这些问题: 1. waitFor方法的形参有限制吗? 没有!如果形参是Task类型,不应该启动Task,如果是function类型,会执行方法.所以wa ...
- oracle 表导入到powerDesigner 中
最近不忙,之前一直是用powerDesigner看表结构,还没自己导入过,今天试试 oracle 表导入到powerDesigner 中步骤: 1.File--->reverse Enginne ...
- 5 MySQL索引
目录: 1. 索引概述 1.1 为什么引入索引 1.2 什么是索引 1.3 索引的好处 1.4 索引的不足 1.5 索引分类 2. 索引设计原则 3. 索引建立和删除 3.1 索引创建 3.2 索引删 ...
- 高并发非自增ID如何设计?
博友们一起来讨论下高并发非自增ID如何设计? 底层是很重要的,我最近设计底层,通用底层. 我想跟大家谈论下这个话题: 如何在高并发环境下设计出一套好用的非自增ID的添加操作的解决方案?更新的操作我随机 ...
- Spring in action (1)
spring中注入bean的方法: 1.通过xml文件来注入bean. 2.通过java注解来注入 默认的bean生命周期是单例的.每次都会返回相同的类的实例.
- windbg Symbol file path
SOS是一个调试器扩展,用于调试.NET应用程序.它提供了一组非常丰富的命令,这些命令使开发人员可以对CLR进行深入分析,并且有助于找出应用程序中各种复杂错误的原因. 由于SOS能够提供CLR内部 ...
- Mahout之(三)相似性度量
User CF 和 Item CF 都依赖于相似度的计算,因为只有通过衡量用户之间或物品之间的相似度,才能找到用户的“邻居”,才能完成推荐.上文简单的介绍了相似性的计算,但不完全,下面就对常用的相似度 ...
- 配置 SQL Server Email 发送以及 Job 的 Notification通知功能
配置 SQL Server Email 发送以及 Job 的 Notification通知功能 在与数据库相关的项目中, 比如像数据库维护, 性能警报, 程序出错警报或通知都会使用到在 SQL Ser ...