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. java的main函数组成

    package test;/*public static void main(String[] args)主函数特殊之处:1.格式是固定的2.被jvm(虚拟机)所识别和调用 public:因为权限必须 ...

  2. JS之代理模式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. js中获取时间new Date()详细介绍

    var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1970-????)m ...

  4. console.log()中的运算与打印事件

    var p=function() { var i = 0; function b() { console.log(i--);//先打印再减一 //console.log(--i);先减一再打印 } f ...

  5. WScript与CScript的区别

    WSH有两种形式:一为WScript是一个窗口化的版本:一为CScript是一个命令行的版本.两种版本都可以运行任何脚本.二者之间的区别是,窗口化版本(WScript)使用一个弹出对话框来显示文本输出 ...

  6. 解决使用mybatis分页插件PageHelper的一个报错问题

    在项目中使用PageHelper进行分页,启动运行时报错,报错如下: 27-Aug-2017 09:58:48.017 INFO [localhost-startStop-1] org.apache. ...

  7. 解决 Linux 桌面亮度调整不工作

    工作原因开始使用Ubuntu.桌面环境为GNOME,不过亮度调整和桌面环境没多大关系. 思路: 不管是GNOME还是Unity,都会尝试自己去接管亮度调整,也就是去 /sys/class/backli ...

  8. cocoapod podpackage 自动根据podfile生成framework实现二进制化,原创脚本,转载请注明出处

    #!/bin/bash # created by lichanghong ; mail: lichanghong@soyoung.com # XXX.sh  AFNetworking 3.0.0 se ...

  9. VmwareTools以及搜狗拼音的安装

    已经那么多年工作下来了,结果装linux还是那么 的费劲! 装的是纯净版Ubuntu16.04版本,17.04怕不稳定就没装, 装了发现VmwareTools是暗的,以前也遇到过这个问题,但是真的忘记 ...

  10. 学习Spring中遇到关于BeanFactory及测试类的问题

    最近在学习Spring,使用的是Spring 5.0.1 学习书本中使用的是4.0 学习书本中使用以下来加载配置文件及设置 Resource resource = new ClassPathResou ...