哎哟我天啊。这道题快折磨死我了。刚开始连题都没看明白,就是不知道substring是什么意思。研究了好长时间才看出来的。

光辉历史呀。。。菜死了

1、题目

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

Example 1:Input: "abcabcbb"      Output: 3      Explanation: The answer is "abc", with the length of 3.

Example 2:Input: "bbbbb"         Output: 1       Explanation: The answer is "b", with the length of 1.

Example 3:Input: "pwwkew"     Output: 3        Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
 
意思就是给一个字符串,找出最长的无重复子串。
比如
“abcabcbb”:他的无重复子串就是“abc”,“abc”,“b”
“pwwkew”:它的无重复子串就是“pw”,“wke”,“kew”。最长的长度当然是3啦。而不是“pwke”。因为他要找的是子串,并不是要找的不重复序列。
"dvdf":它的无重复子串就是“dv”,“vdf”
说白了,就是一个字符一个字符的遍历,找出来没有重复字符的子字符串。
只是为了看懂这个,就花了好多时间,菜死了。

2、Python解法

  1. class Solution:
  2. def lengthOfLongestSubstring(self, s):
  3. """
  4. :type s: str
  5. :rtype: int
  6. """
  7. r="" #储存无重复子串
  8. maxNum=0 #最大无重复子串的长度
  9. for i in s:
  10. if i not in r: #如果不在子串里,就代表无重复,直接插进去
  11. r=r+i
  12. else: #如果在子串里,就代表重复了,不能直接插进去
  13. if len(r)>maxNum:maxNum=len(r) #先算出来上一个子串的长度
  14. r = r[r.index(i)+1:]+i #把这个相同字符后面的保留。比如"dvdf"。第一个子串是"dv",再遍历到d的时候,需要把第一个d后面的v保留,再形成一个"vd"子串,这样还是无重复子串。不保留v的话,就不是一个完整的无重复子串了
  15. if len(r) > maxNum: maxNum = len(r)
  16. return maxNum
  17.  
  18. s="dvdf"
  19. a=Solution()
  20. print(a.lengthOfLongestSubstring(s))
 我的解释代码里面写的很清楚。
运行效果还不错

C语言版的先不写了吧,我已经花了快两个小时了。按照这个思路写,完全没问题。

 

LeetCode——Problem3:Longest Substring Without Repeating Characters的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

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

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

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

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

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

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

  5. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  6. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  7. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

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

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  9. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

随机推荐

  1. docker使用centos7系统构建tomcat镜像

    FROM shansongxian/centos-oraclejdk8:latest #此镜像使用centos7系统,精简jdk,只运行java程序,无法编译/构建 MAINTAINER huqian ...

  2. 在vue-cli中使用路由

    1.首先npm中是否有vue-router 一般在vue-cli的时候就已经下载好了依赖包了 2.使用vue的话正常的需要涉及这几个文件 demo/src/router/index.js import ...

  3. MovieReview—Avengers: Infinity War(复仇者联盟3:无限战争)

    Antagonist? Thanos,the central figure of the Avengers 3,antagonist. Everyone has his own ideals and ...

  4. file - 确定文件类型

    总览 file [ -bcnsvzL ] [ -f 命名文件 ] [ -m 幻数文件 ] file ... 描述 本手册页说明了3.27版本 file 命令的使用. File 命令试图检查每个参数以判 ...

  5. Java基础面试题:String 和StringBuffer的区别

    package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...

  6. 防止内存泄露 Linux下用Valgrind做检查

    用C/C++开发其中最令人头疼的一个问题就是内存管理,有时候为了查找一个内存泄漏或者一个内存访问越界,需要要花上好几天时间,如果有一款工具能够帮助我们做这件事情就好了,valgrind正好就是这样的一 ...

  7. facebook的infer检测工具的安装

    缘由 由于公司产出代码的时候会使用静态扫描工具检测代码的质量,所以自己就想动手尝试一番infer整个的使用方式和使用效果,便动手安装了infer,结果安装过程中遇见太多的坑,导致很多时候都安装失败,这 ...

  8. JavaScript 常用的排序算法

    冒泡排序 function bubbleSort(array) { for (let i = 0; i < array.length; i++) for (let j = 0; j < a ...

  9. 分享自己写的基于Dapper的轻量级ORM框架~

    1.说明 本项目是一个使用.NET Standard 2.0开发的,基于 Dapper 的轻量级 ORM 框架,包含基本的CRUD以及根据表达式进行一些操作的方法,目前只针对单表,不包含多表连接操作. ...

  10. PHP静态文件缓存

    ob_start(); 2 echo 'aaa'; 3 $string = ob_get_contents(); 4 file_put_contents('a.html', $string); 5 o ...