哎哟我天啊。这道题快折磨死我了。刚开始连题都没看明白,就是不知道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. GitLab一个非标准的端口远程仓库导致clone失败

    首先看下报错信息 当gitlab服务器ssh端口不是默认的22时,使用ssh连接gitlab会出现上面的错误 解决方法: 修改/etc/gitlab/gitlab.rd gitlab_rails['g ...

  2. cms-数据库设计

    业务相关的3张表 1.类型表: CREATE TABLE `t_arctype` (`id` int(11) NOT NULL AUTO_INCREMENT,//id`typeName` varcha ...

  3. 访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”的解决方案

    访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”的解决方案: 打开客戶端浏览器--工具---internet-安全-自定义级别-选择到低到中低. 然后点受信任站点,把你要访问的站点 ...

  4. JS调试debug

    1. debugger; 我以前也说过,你可以在JavaScript代码中加入一句debugger;来手工造成一个断点效果.需要带有条件的断点吗?你只需要用if语句包围它: if (something ...

  5. iOS将大文件映射到内存(读取大文件)

    http://blog.csdn.net/xyt243135803/article/details/40995759 在<中国区GPS偏移纠正(适用于Google地图)>一文中曾读取一个7 ...

  6. 【BZOJ1030】[JSOI2007] 文本生成器(AC自动机上跑DP)

    点此看题面 大致题意: 给你\(N\)个字符串(只含大写字母),要你求出有多少个由\(M\)个大写字母构成的字符串含有这\(N\)个字符串中的至少一个. \(AC\)自动机 看到题目,应该比较容易想到 ...

  7. CSS布局--垂直水平居中

    ···设置两个盒子 <div class="parent"> <div class="child"> </div></ ...

  8. linux用命令行运行matlab的.mat文件

    入m文件所在目录后,运行 $ matlab -nodesktop -nosplash -r matlabfile 只用文件名matlabfile,不能添加.m

  9. GPU并行编程:内核及函数的实现

    原文链接 回想一下我们之前在设备上使用“kernelFunction<<<1,1>>>(..)”执行一个函数的代码,我在那里还曾说过后面会细说,本文就详细介绍一下参 ...

  10. Oracle小技巧_不同表空间不同用户导入导出数据dmp

    [博主注:数据库方面 ITPUB网站及博客内容非常丰富]   转载来源ITPUB 博客 经常有人会问:原来的数据在USERS表空间里面,我想把它IMP进APP表空间,我已经修改了目的用户的默认表空间, ...