哎哟我天啊。这道题快折磨死我了。刚开始连题都没看明白,就是不知道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解法

class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
r="" #储存无重复子串
maxNum=0 #最大无重复子串的长度
for i in s:
if i not in r: #如果不在子串里,就代表无重复,直接插进去
r=r+i
else: #如果在子串里,就代表重复了,不能直接插进去
if len(r)>maxNum:maxNum=len(r) #先算出来上一个子串的长度
r = r[r.index(i)+1:]+i #把这个相同字符后面的保留。比如"dvdf"。第一个子串是"dv",再遍历到d的时候,需要把第一个d后面的v保留,再形成一个"vd"子串,这样还是无重复子串。不保留v的话,就不是一个完整的无重复子串了
if len(r) > maxNum: maxNum = len(r)
return maxNum s="dvdf"
a=Solution()
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. 基于mllib的协同过滤实战(电影推荐)

    //加载需要的包 import org.apache.spark.rdd._ import org.apache.spark.mllib.recommendation.{ALS, Rating, Ma ...

  2. php生成纯数字、字母数字、图片、纯汉字的随机数验证码

    现在讲开始通过PHP生成各种验证码旅途,新手要开车了,请刷卡! 首先,我们开始先生成一个放验证码的背景图片 注:没有Imagejpg()这个函数,只有imagepng()函数 imagecreatet ...

  3. 【BZOJ1057】[ZJOI2007] 棋盘制作(单调栈的运用)

    点此看题面 大致题意: 给你一个\(N*M\)的\(01\)矩阵,要求你分别求出最大的\(01\)相间的正方形和矩形(矩形也可以是正方形),并输出其面积. 题解 这题第一眼看去没什么思路,仔细想想,能 ...

  4. 【洛谷2051】[AHOI2009] 中国象棋(烦人的动态规划)

    点此看题面 大致题意: 让你在一张\(N*M\)的棋盘上摆放炮,使其无法互相攻击,问有多少种摆法. 辟谣 听某大佬说这是一道状压\(DP\)题,于是兴冲冲地去做,看完数据范围彻底懵了:\(N≤100\ ...

  5. 对ListBox控件中的数据进行排序

    实现效果: 知识运用: ListBox控件的Sorted属性 //ListBox控件中的数据项是否按字母顺序排序 public bool Sorted{get;set;} 实现代码: private ...

  6. CVE-2018-4878

    0x00前言 该漏洞影响 Flash Player 版本28.0.0.137以及之前的所有版本 0x01 poc Poc 这里只列出关键代码 public function triggeruaf() ...

  7. 面向对象编程 -------JavaScrip

    本文摘要:http://www.liaoxuefeng.com/ 一定明白面向对象的两个基本概念: 类:类是对象的类型模板,例如,定义Student类来表示学生,类本身是一种类型,Student表示学 ...

  8. React后台管理系统-file-uploader组件

    1.React文件上传组件github地址: https://github.com/SoAanyip/React-FileUpload 2.Util里边新建file-uploader文件夹,里边新建i ...

  9. 4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍

    4- vue django restful framework 打造生鲜超市 -restful api 与前端源码介绍 天涯明月笙 关注 2018.02.20 19:23* 字数 762 阅读 135 ...

  10. 安全 - 堡垒机 - Jumpserver

    GitHub - jumpserver/Dockerfile: Jumpserver all in one Dockerfile https://github.com/jumpserver/Docke ...