3-Longest Substring Without Repeating Characters @LeetCode

题目

题目中得到的信息有:

一段字符串找出不重复子串的最大长度,只需要长度信息。

思路

肯定是需要将字符串遍历一遍,在遍历过程中就需要查找前面字符串是否出现该字符,因此这是该算法的重点。若没找到,长度加一,若找到了,长度会从前面该字符位置+1处开始算起。下面以图来说明:

假如我们以begin为子串的开始,current表示当前处理的位置。1)当前位置的字符没有出现在[begin,current)的区间内,说明将该值加入到区间内满足没有重复条件,长度加一;

2)若当前值已经在该区间内,加入后肯定会出现重复,则应该将begin移动到没有该值的最左边位置,图中 new begin位置满足该条件。

在移动begin之前首先需要判断[begin, current)是否是现有的最大长度,若是则更新,然后将begin移动到该新位置。

还有一个关键点,就是如何查找当前字符是否出现在[begin, current)区间里,若在从该区间遍历一遍肯定费时间,有没有办法可以一次就能判断出来呢?

对了,可以采用hash的思想,将每个值在字符串的位置放入到以该值为下标的数组中,若数组中保存有值,并且是在[begin,current)范围内,则说明存在。

C算法

int lengthOfLongestSubstring(char* s) {
int tmp[128]; //用于保存字符的下标 int begin = 0, max = 0; //begin和最大长度默认为0
int i, index;
for (i = 0; s[i] != '\0'; i++) { //遍历字符串
if (tmp[s[i]] != 0) { //判断该值是否出现在前面的字符中
index = tmp[s[i]] - 1; //取保存的上一个该值的位置
if (index >= begin) { //判断是否在[begin, current)
if (i - begin > max) //对比是否最大
max = i - begin;
begin = index + 1;//begin新的位置,在前一个该字符的后面,即new begin位置
}
}
tmp[s[i]] = i + 1; //保存该值的最新下标
}
return i - begin > max ? i - begin : max;//最后还需要判断下
}

结果

3-Longest Substring Without Repeating Characters @LeetCode的更多相关文章

  1. Longest Substring Without Repeating Characters leetcode java

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  2. Longest Substring Without Repeating Characters ---- LeetCode 003

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. Longest Substring Without Repeating Characters -- LeetCode

    原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 这道题用的方法是在LeetC ...

  4. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  6. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  7. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  8. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  9. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  10. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

随机推荐

  1. idea 工具中项目文件上有灰色的小X号去除方法

    初使用idea,在项目中发现类上有这样的灰色X号,启动项目后idea会报找不到这个类的错误,原因是它没有被编译, 解决方法 setting->Build->Compiler->Exc ...

  2. (转)ArcGIS Runtime for Android 使用异步GP服务绘制等值线

    关于基于Android上ArcGIS Server GP服务的调用,已经有前辈给出了很好的例子: http://blog.csdn.net/esrichinacd/article/details/92 ...

  3. GUI开发:实时显示摄像头图像

    import tkinter as tk from PIL import Image, ImageTk import cv2 import numpy as np import time g_exit ...

  4. Intellij IDEA xxx.properties变成纯文本模式解决方案

    今天在创建xxx.properties的时候不知道按到了哪里,结果让它编程了纯文本模式,重命名这个文件或者删掉,重新创建这个同名文件,换一个项目,始终是文本文件类型,就估计不是项目问题,是intell ...

  5. Vue语法学习第一课——插值

    学习关于Vue的插值语法 ① 文本值 : "Mustache"语法,即双大括号 <span>Message:{{msg}}</span> 注:双大括号中的m ...

  6. Qt的子窗口和父窗口阻塞问题

    在图形界面中,软件设计者通常需要将活跃窗口限制为一个.在某个窗口活跃时,它的父窗口被它挡住或者挡住一部分,这时候用鼠标去点击父窗口是没有作用的.问题的关键在于将子窗口设置模态: void MainWi ...

  7. jmeter 常用组件详解

    1.测试计划(test plan) 描述一个性能测试,包含本次测试所有相关功能 2.threads(users)线程 Setup thread group: 一种特殊类型的线程,可用于执行预测试操作. ...

  8. 2018-2019-2 《网络对抗技术》Exp5 MSF基础应用 20165326

    Exp5 MSF基础应用 实践内容 主动攻击实践 ms17_010_enternalblue 靶机:win7 x64成功 针对浏览器的攻击 ms14_064_ole_code_execution 靶机 ...

  9. 2018-2019-2 学号20175223 实验二《Java面向对象程序设计》实验报告

    目录 北京电子科技学院(BESTI)实验报告 实验名称:实验二 面向对象程序设计 实验内容.步骤与体会: 一.实验二 面向对象程序设计-1 二.实验二 面向对象程序设计-2 三.实验二 面向对象程序设 ...

  10. protel项目创建

    File->New->Project->PCB Project//新建PCB项目 Save Project As... Project->Add New to Project- ...