哎哟我天啊。这道题快折磨死我了。刚开始连题都没看明白,就是不知道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. [转贴] ASP.NET -- Web Service (.asmx) & JSON

    [转贴] ASP.NET -- Web Service (.asmx) & JSON 以前没做过,但临时被要求 ASP.NET Web Service 要传回 JSON格式 找到网络上两篇好文 ...

  2. MySQL binlog server

    从5.6版本开始,可以利用 mysqlbinlog命令把远程机器的日志备份到本地目录,这样就更加方便快捷的实现一个binlog server. 环境介绍:192.168.56.100是备份服务器,19 ...

  3. 【洛谷1337】[JSOI2004] 吊打XXX(模拟退火经典题)

    点此看题面 大致题意: 一个平面上有\(n\)个点,每个点有1个权值,现在要选择平面上的一个点,使这\(n\)个点的权值乘上到达选定点的距离之和最小. 模拟退火 我们可以用模拟退火来做这道题. 先将\ ...

  4. 利用kvo实现列表倒计时

    自己稍微记录一下,方便以后用到: 先创建一个定时器的类: #import "TimeCenter.h" @interface TimeCenter () @property (no ...

  5. BundleConfig的作用

    在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和css进行压缩(多个文件可以打包成一个文件),并且可以区分调试和非调试,在调试时不进行压缩,以原 ...

  6. java中 二进制 八进制 十六进制 十进制 相互转换 最简方法

    package com.swift; import java.util.Scanner; public class Hex2Decimal { public static void main(Stri ...

  7. vue 采坑

    1.ref 在父组件中访问子组件实例,或者直接操作DOM元素时需要ref <input ref="ipt"> 通过this.$refs.ipt 得到此input $re ...

  8. SummerVocation_Learning--java的线程同步

    public class Test_XCTB implements Runnable{ Timer timer = new Timer(); public static void main(Strin ...

  9. yum安装报错

    检查了好久才知道原来是 sudo nano /etc/sysconfig/network-scripts/ifcfg-ens33 下的DNS配错了,改好之后,sudo service network ...

  10. ngin负载均衡集群(一)

    一.nginx负载均衡集群介绍: 1.反向代理与负载均衡概念简介严格地说, nginx仅仅是作为 Nginx Proxy反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果,所以本文称之 ...