leetcode5 Implement strstr() 实现strstr函数功能

  Implement strstr() 实现strstr函数功能

    [email protected]

Question:

Implement strstr(). Returns the index of the first occurrence of needle in haystack, or –1

if needle is not part of haystack.

int strStr(string haystack, string needle)

{

for (int i = 0; ; i++) {

for (int j = 0; ; j++) {

if (j == needle.length()) {

return i;

}

if (i + j == haystack.length()) {

return -1;

}

if (needle[j] != haystack[i + j]) {

break;

}

}

}

}

时间: 2024-07-30 13:40:08

leetcode5 Implement strstr() 实现strstr函数功能的相关文章

c语言 模拟 库函数 strstr()函数

//模拟 库函数  strstr()函数 //从父字符串(较长)找到 完全相同子字符串(较短): //返回相同字符串在父字符串中的首字符的地址: #include<stdio.h> char * my_strstr(char arr[],char arr1[]) { char *p1=NULL,*p2=NULL,*p=NULL; int i = 0,j = 0,k=0,m=0; p1 =&arr[0]; p2 = &arr1[0]; while (1) { if (arr1[0

oracle实现split函数功能

转载: http://blog.csdn.net/jojo52013145/article/details/6758279在实际的应用中,为了让PL/SQL 函数返回数据的多个行,必须通过返回一个 REF CURSOR 或一个数据集合来完成.REF CURSOR 的这种情况局限于可以从查询中选择的数据,而整个集合在可以返回前,必须进行具体化. 9i 通过引入的管道化表函数纠正了后一种情况.表函数是返回整个行的集(通常作为一个集合)的函数,可以直接从 SQL 语句中进行查询,就好像它是一个真正的数

模拟实现兼容低版本IE浏览器的原生bind()函数功能

模拟实现兼容低版本IE浏览器的原生bind()函数功能: 代码如下: if(!Function.prototype.bind){   Function.prototype.bind=function(oThis){     if (typeof this !== 'function'){       throw new TypeError('调用者不是当前函数对象');     }       var aArgs = Array.prototype.slice.call(arguments, 1

通过一个函数,操作一个结构体,实现对应函数功能

指针结构体一直是我的盲点,所以今天有必要整“清理门户”.此种通过一个函数操作一个结构体,实现对应函数功能,用法十分巧妙,使用得当可以使得代码可移植性和易懂性大大的增加,有人说过“代码注释的最高境界是程序的自述,而不是双斜杠然后后面跟着中英文的注释”.哈哈,说远了,下面开始进入今天的加油站,补充体力了. 1 // 头文件 2 #include <stdio.h> 3 4 // 函数声明 5 typedef struct _halDeviceFuncs_t 6 { 7 void (*pfnInit

用JAVA写一个函数,功能如下: 任意给定一组数, 找出任意数相加之后的结果为35(任意设定)的情况

用JAVA写一个函数.功能如下:任意给定一组数,例如{12,60,-8,99,15,35,17,18},找出任意数相加之后的结果为35(任意设定)的情况. 可以递归算法来解: package test1; import java.util.Arrays; public class demo { public static void main(String[] args) { String str = "12,60,-8,99,15,35,17,18,8,10,11,12"; int s

【小计】PostgreSQL实现Oracle的trunc日期函数功能

create or replace function trunc(p_timestamp timestamp with time zone, p_formart varchar default 'DD')  returns timestamp without time zone as $$ declare  v_timestamp timestamp := null;  v_formart varchar(10) := upper(p_formart); begin  /*  * 函数功能:对日

javascript 数组扩展实现 php array_count_values() 函数功能

在PHP中,array_count_values() 这个函数可以统计数组元素出现的次数,这个函数会返回一个数组,键名是原数组的值,键值是这个值出现的次数. 但是JavaScript中没有这样的函数.不过大神无数,前些日子发现这样的一个扩展: /** javascript 数组扩展实现 php array_count_values() 函数功能 */ (function(window){ if ( window.ActiveXObject ) { window.Array.prototype.i

开启关闭mysql函数功能

开启MySQL函数功能: 复制代码代码如下: SET GLOBAL log_bin_trust_function_creators=1; 关闭MySQL函数功能: 复制代码代码如下: SET GLOBAL log_bin_trust_function_creators=0; 查看状态: 复制代码代码如下: show variables like '%func%';

详解calc()函数功能

calc()对大家来说,或许很陌生,不太会相信calc()是css中的部分.因为看其外表像个函数,既然是函数为何又出现在CSS中呢?这一点也让我百思不得其解,今天有一同事告诉我,说CSS3中有一个属性能实现自适应的布局,首先让我想到的是box-sizing,但跟我说还可以计算,这让我不得不想起calc().因为早先在官网和一些blog上看到相关的介绍,但一直没有深入,也没有自己去测试过.今天花了一下午的时间彻底学习了一下calc().于是就有了这篇blog,希望对大家有所帮助. 平时在制作页面的