哎哟我天啊。这道题快折磨死我了。刚开始连题都没看明白,就是不知道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. HDU3954 线段树(区间更新 + 点更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3954 , 一道比较好的线段树题,值得做. 题目是NotOnlySuccess大神出的,借此题来膜拜一下 ...

  2. linux 命令——45 free(转)

    free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参 ...

  3. 使用SAP云平台 + JNDI访问Internet Service

    以Internet Service http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Walldorf&destin ...

  4. Python常见编程规范总结

    Pythonic定义 Python最常用的编码风格还是PEP8,详见:http://jython.cn/dev/peps/pep-0008/ Pythonic确实很难定义,先简单引用下<Pyth ...

  5. POJ 3666 Making the Grade(区间dp)

    修改序列变成非递减序列,使得目标函数最小.(这题数据有问题,只要求非递减 从左往右考虑,当前a[i]≥前一个数的取值,当固定前一个数的取值的时候我们希望前面操作的花费尽量小. 所以状态可以定义为dp[ ...

  6. 【BZOJ1060】[ZJOI2007] 时态同步(树形DP)

    点此看题面 大致题意: 给你一棵带权树,每次使用道具可以将某条边的边权加\(1\),问你至少需要使用多少次道具,才能使每个叶子节点到根节点的距离相等. 贪心的思想 首先,我们应该先有一个贪心的思想. ...

  7. linux怎么进home目录下

    可以使用cd命令,cd命令的功能是切换到指定的目录: 命令格式:cd [目录名] 有几个符号作为目录名有特殊的含义: “/”代表根目录.“..”代表上一级目录.“~”代表HOME目录.“-”代表前一目 ...

  8. 六、react添加多个className报错解决方法

    例如<div className={style.calss1,style.class2}></div> 该方法会报错 想得到最终渲染的结果:<div class='cla ...

  9. 进入Windows之前发出警告

    实现效果: 知识运用: 通过注册表中HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\子键下的LegalNoticeCaption ...

  10. 设置RichTextBox控件的文本的对齐方式

    实现效果: 知识运用: RichTextBox控件的SelectionAlignment属性 //获取或设置在当前选择或插入点的对齐方式 public HorizontalAlignment Sele ...