手写实现indexOf
突然奇想。自己手写一个indexOf
package com.toov5.test; public class Test8 { public static int find(String str1, String str2) { char[] str1Arr = str1.toCharArray();
char[] str2Arr = str2.toCharArray(); int str1Length = str1.length();
int str2Length = str2.length(); int str1Index = 0;
int str2Index = 0; while ( (str1Index < str1Length) && (str2Index < str2Length)) {
if (str1Arr[str1Index] == str2Arr[str2Index]) {
str1Index++;
str2Index++;
}else {
str1Index = str1Index -str2Index +1;
str2Index = 0;
} } int index = str2Length == str2Length ? (str2Length > 1 ? str1Index - str2Index : str2Index -1 ) : -1;
return index; } }
易读性的,练练代码能力:
package com.toov5.test; public class TestOk1 { public static int getIndex(String str1 ,String str2) { String LonggerStr = str1.length() > str2.length() ? str1 : str2;
String ShorterStr = str1.length() > str2.length() ? str2 : str1;
int mark = 0; //标记位置 for(int i=0 ; i< LonggerStr.length(); i++) {
if (LonggerStr.charAt(i) == ShorterStr.charAt(0)) {
mark = i;
int index = i;
int shortIndex = 0;
while (LonggerStr.charAt(index++) == ShorterStr.charAt(shortIndex++) && shortIndex < ShorterStr.length());
if (shortIndex<ShorterStr.length()-1) {
mark=-1;
}
return mark;
} } return mark; } public static void main(String[] args) {
String str1 ="rewrf3eucksfddw";
String str2 = "fuck"; int result = getIndex(str1, str2);
System.out.println(result); } }
手写实现indexOf的更多相关文章
- 手把手集成web端手写公式功能
何为手写公式,很简单,就是在网页上可以写出数学公式,并能够生成Latex格式的字符串.废话不多说,直接走正题. 一.首先大家可以先去官网了解一下myscript这个插件 官方网站:https://de ...
- java24 手写服务器最终版本
手写服务器最终版本; <?xml version="1.0" encoding="UTF-8"?> <web-app> <serv ...
- 『练手』手写一个独立Json算法 JsonHelper
背景: > 一直使用 Newtonsoft.Json.dll 也算挺稳定的. > 但这个框架也挺闹心的: > 1.影响编译失败:https://www.cnblogs.com/zih ...
- HTTP网络协议与手写Web服务容器
Http协议 1.深入概念 Http:HyperText Transfer Protocol,即是超文本传输协议. 2.浅出概念(使用浏览器访问服务器端网页时需要遵循的一系列规则) Http:将各种不 ...
- 手写简易WEB服务器
今天我们来写一个类似于Tomcat的简易服务器.可供大家深入理解一下tomcat的工作原理,本文仅供新手参考,请各位大神指正!首先我们要准备的知识是: Socket编程 HTML HTTP协议 服务器 ...
- JavaSE 手写 Web 服务器(一)
原文地址:JavaSE 手写 Web 服务器(一) 博客地址:http://www.extlight.com 一.背景 某日,在 Java 技术群中看到网友讨论 tomcat 容器相关内容,然后想到自 ...
- 纯手写Myatis框架
1.接口层-和数据库交互的方式 MyBatis和数据库的交互有两种方式: 使用传统的MyBatis提供的API: 使用Mapper接口: 2.使用Mapper接口 MyBatis 将配置文件中的每一个 ...
- 手写一套迷你版HTTP服务器
本文主要介绍如何通过netty来手写一套简单版的HTTP服务器,同时将关于netty的许多细小知识点进行了串联,用于巩固和提升对于netty框架的掌握程度. 服务器运行效果 服务器支持对静态文件css ...
- 如何手写一款KOA的中间件来实现断点续传
本文实现的断点续传只是我对断点续传的一个理解.其中有很多不完善的地方,仅仅是记录了一个我对断点续传一个实现过程.大家应该也会发现我用的都是一些H5的api,老得浏览器不会支持,以及我并未将跨域考虑入内 ...
随机推荐
- Python 石头 剪刀 布
di = {1: '石头', 2: '剪刀', 3: '布'} def win(x, y): if len({x[0], y[0]}) == 1: print('平局.') else: if {x[0 ...
- C#高级编程(32章)ADO.net
32.2高效地使用连接 sqlConnection类是针对sql的,而OleDbConnection 是针对其他比如access的,另外还有odbcConnection是针对odbc的,sql的访问优 ...
- WRNavigationBar 使用记录
最近在做一个导航栏透明度渐变的效果,发现 WRNavigationBar库很好用,一开始导入到项目,发现导航tiltle颜色一直是黑色的,无论怎么用系统方法改都改不了.后来发现原来库里面有一个方法可以 ...
- XSS漏洞基础
什么是XSS? XSS全程Cross-site scripting,跨站脚本攻击.恶意攻击者往Web页面里插入html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意用 ...
- php quotemeta()函数 语法
php quotemeta()函数 语法 作用:在预定义字符前添加反斜杠东莞直线电机 语法:quotemeta(string) 参数: 参数 描述 string 必须,需要处理的字符串 说明:该函数可 ...
- codeforces798C - Mike and gcd problem (数论+思维)
原题链接:http://codeforces.com/contest/798/problem/C 题意:有一个数列A,gcd(a1,a2,a3...,an)>1 时称这个数列是“漂亮”的.存在这 ...
- Pasha and Tea
Pasha and Tea time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 用.NET做圣诞节音乐盒
用.NET做圣诞节音乐盒 我曾经用这个程序送给我女朋友(现老婆)
- fatal: early EOF fatal: index-pack failed & Git, fatal: The remote end hung up unexpectedly
https://stackoverflow.com/questions/15240815/git-fatal-the-remote-end-hung-up-unexpectedly https://s ...
- MultipartFile 图片上传到Linux服务器Tomcat下的webapps目录
第一次接触 linux 服务器,做图片上传的时候遇到了些坑,搞了些天总算成功了,记录一下 /** * 上传图片 * * @param request * @param file * @return * ...