LeetCode——Problem3:Longest Substring Without Repeating Characters
哎哟我天啊。这道题快折磨死我了。刚开始连题都没看明白,就是不知道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.
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的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
随机推荐
- docker使用centos7系统构建tomcat镜像
FROM shansongxian/centos-oraclejdk8:latest #此镜像使用centos7系统,精简jdk,只运行java程序,无法编译/构建 MAINTAINER huqian ...
- 在vue-cli中使用路由
1.首先npm中是否有vue-router 一般在vue-cli的时候就已经下载好了依赖包了 2.使用vue的话正常的需要涉及这几个文件 demo/src/router/index.js import ...
- MovieReview—Avengers: Infinity War(复仇者联盟3:无限战争)
Antagonist? Thanos,the central figure of the Avengers 3,antagonist. Everyone has his own ideals and ...
- file - 确定文件类型
总览 file [ -bcnsvzL ] [ -f 命名文件 ] [ -m 幻数文件 ] file ... 描述 本手册页说明了3.27版本 file 命令的使用. File 命令试图检查每个参数以判 ...
- Java基础面试题:String 和StringBuffer的区别
package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...
- 防止内存泄露 Linux下用Valgrind做检查
用C/C++开发其中最令人头疼的一个问题就是内存管理,有时候为了查找一个内存泄漏或者一个内存访问越界,需要要花上好几天时间,如果有一款工具能够帮助我们做这件事情就好了,valgrind正好就是这样的一 ...
- facebook的infer检测工具的安装
缘由 由于公司产出代码的时候会使用静态扫描工具检测代码的质量,所以自己就想动手尝试一番infer整个的使用方式和使用效果,便动手安装了infer,结果安装过程中遇见太多的坑,导致很多时候都安装失败,这 ...
- JavaScript 常用的排序算法
冒泡排序 function bubbleSort(array) { for (let i = 0; i < array.length; i++) for (let j = 0; j < a ...
- 分享自己写的基于Dapper的轻量级ORM框架~
1.说明 本项目是一个使用.NET Standard 2.0开发的,基于 Dapper 的轻量级 ORM 框架,包含基本的CRUD以及根据表达式进行一些操作的方法,目前只针对单表,不包含多表连接操作. ...
- PHP静态文件缓存
ob_start(); 2 echo 'aaa'; 3 $string = ob_get_contents(); 4 file_put_contents('a.html', $string); 5 o ...