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. Centos7搭建docker仓库

    一:安装启动registry 1.1:环境准备 yum install -y python-devel libevent-devel python-pip gcc xz-devel pip insta ...

  2. thinkPHP验证码不显示问题

    使用验证码的时候,不显示图片时,要清理下输出缓存:ob_clean_end();

  3. 使用 navicat 导入导出数据库

    1.使用 navicat 导出数据库 2.使用 navicat 导入数据库导入之前需要先建好数据库 3.可以直接使用navicat 到数据传输功能直接将一个数库copy到另一个数据库

  4. poj1947(树形背包)

    题目链接:http://poj.org/problem?id=1947 Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total ...

  5. 以编程方式使用 Microsoft Office Visio 2003 ActiveX 控件

    以编程方式使用 Microsoft Office Visio 2003 ActiveX 控件 2007/10/29 Mark BukovecEmpire Down Development 适用于:Mi ...

  6. 《贝贝GO》服务条款

    服务条款 一.服务条款的确认与接收 1.贝贝GO客户端软件(以下简称“本软件”)各项电子服务的所有权和运作权归属于“东莞市山水信息技术有限公司”(以下称“本公司”)所有,本软件提供的服务将完全按照其发 ...

  7. Sharepoint 2016 配置FBA(四)添加用户到Membership数据库

    现在还不能用FBA登录,因为数据库还没有用户. 有一些方法来管理membership数据库,有可以用IIS来管理.推荐使用 SharePoint 2016 FBA Pack(https://share ...

  8. CSS效果:动态图标

    HTML: <html lang="en"> <head> <meta charset="UTF-8"> <meta ...

  9. Neural Network Programming - Deep Learning with PyTorch with deeplizard.

    PyTorch Prerequisites - Syllabus for Neural Network Programming Series PyTorch先决条件 - 神经网络编程系列教学大纲 每个 ...

  10. logback-MDC日志唯一标识

    自定义LogbackFilter: import org.slf4j.MDC; import javax.servlet.*; import javax.servlet.annotation.WebF ...