`
Cwind
  • 浏览: 262677 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
博客专栏
793bb7df-a2a9-312d-8cb8-b66c3af482d1
LeetCode题解
浏览量:52362
社区版块
存档分类
最新评论

LeetCode[字符串] - #3 Longest Substring Without Repeating Characters

阅读更多

原题链接:#3 Longest Substring Without Repeating Characters

 

要求:

给定一个字符串,找到它没有重复字符的最长子串的长度。例如,"abcabcbb"的无重复字符最长子串为"abc",其长度为3。对于由相同字符组成的字符串"bbbbb",其最长子串为"b",故返回长度为1。

 

难度:中等

 

分析:

遍历给定字符串,对于每一个字符c,若c不在临时字符串temp(初始化为空字符串)中,则将其加入temp;若temp包含c,则计算当前temp的长度并将其与当前记录的最大长度进行比较,以判断最大长度值是否需要更新。然后在temp中截取上一个c出现之后的子串,同时把c追加在后面,继续遍历过程即可。

 

解决方案:

public int lengthOfLongestSubstring(String s) {
        if(s.length()<=1){
            return s.length();
        }
        String temp = s.substring(0,1);
        int maxlength = 1;

        for(int i=1;i<s.length();i++){
            if(!temp.contains(String.valueOf(s.charAt(i)))){
                temp += s.charAt(i);
            }else{
                if(maxlength<temp.length()){
                    maxlength = temp.length();
                }
                temp = temp.substring(temp.indexOf(s.charAt(i))+1) + String.valueOf(s.charAt(i));
            }
        }
        if(maxlength < temp.length()){
            maxlength = temp.length();
        }
        return maxlength;
    }

简单测试程序     

 

分享到:
评论

相关推荐

    LeetCode3 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with...

    leetcode答案-LeetCode-Longest_Substring_Without_Repeating_Characters:Leet

    答案LeetCode-Longest_Substring_Without_Repeating_Characters 这是LeetCode上“最长子串无重复字符”问题的解决方案。 问题描述:给定一个字符串,求没有重复字符的最长子串的长度。 示例 1:输入:“abcabcbb” ...

    leetcode2sumc-LeetCode-3.Longest_Substring_Without_Repeating_Characters

    LeetCode-3.Longest_Substring_Without_Repeating_Characters 给定一个字符串,找出没有重复字符的最长子字符串的长度。 示例 1: 输入:“abcabcbb” 输出:3 解释:答案是“abc”,长度为 3。 解决方案 Python3:...

    leetcode分类-leetcode:leetcode问题的代码

    leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:...

    leetcode题库-leetcode-web:以Web形式展示LeetCode题解,基于PythonFlask

    leetcode题库 LeetCode-Web 初始化 前端库依赖 下载,并将jquery-3.x.x.min.js移动到static目录下。 下载,并将semantic.min.js、semantic.min.css、components和themes...longest-substring-without-repeating-charac

    leetcode中文版-LeetCode:LeetcodeC++/Java

    字符串转整数 string 13 Roman to Integer 罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted Lists 合并两个...

    dna匹配leetcode-leetcode:leetcode刷题

    Longest Substring Without Repeating Characters 哈希表 双指针 滑动窗口 Substring with Concatenation of All Words 哈希表 注意匹配方向 Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 ...

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance Difficulty Topics Solution 0001 Two Sum 46.1%...

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。 序号 中文名称 英文名称 通过率 难度 1 Two Sum 47.0% 简单 2 Add Two Numbers 36.0% 中等 3 Longest Substring Without ...

    leetcode实现strstr-leetcode-js:js刷leetcode

    经典字符串,查找,哈希表,双指针法 2019/09/10 0004 Median of Two Sorted Arrays 二分查找,归并排序 2019/09/11 0006 ZigZag Conversion 逻辑 2019/09/13 0007 Reverse Integer 逻辑 2019/09/13 0008 String To ...

    leetcode2sum-Problems:编程问题的回购

    leetcode 2sum # Programming-Problems This will have many problems from all over the web, As of writing this readme there is Haskell 99: 28 finished + Huffman Coding LeetCode: LC1: 2Sum [Easy] LC2: Add...

    leetcode答案-SH_LeetCode:LeetCode-Swift

    leetcode 答案 SH_LeetCode LeetCode-Swift解答记录(Answer records of LeetCode) 11月21日 更新:添加README文件以及LeetCode前三题代码...无重复字符的最长子串(Longest Substring Without Repeating Characters)

    程序员面试宝典LeetCode刷题手册

    3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17...

    leetcode分类-Leetcode:leetcode

    leetcode 分类 #LeetCode 记录 跟风刷leetcode,cxlove刚好练习一下python Leetcode有个很严重的问题,就是大多没有数据范围。 所以我是在尽可能偷懒的情况下用尽可能优的做法。 ##Two Sum 找到数组中两个数的和为...

    LeetCode判断字符串是否循环-leetcode:leetcode练习,算法部分!

    LeetCode判断字符串是否循环 leetcode leetcode exercises,algorithms part! TwoSum: 1.key point: unorderd_map(16ms), map(24ms) 2.大概是前50%的样子,并不知道如何进一步提升性能 addTwoNums: 1.新建ListNode...

    Leetcode回文串拼接-leetcode_note:用于记录leetcode题目的解析

    Leetcode回文串拼接 leetcode_node 题解 该项目主要用于基于Leetcode的刷题记录,与日常学习,对Leetcode上的题目按照解题方法进行分明别类的整理。 题目列表 1.Two Sum 2.Add Two Numbers 3.Longest Substring ...

    javalruleetcode-LeetCode:LeetCode算法问题

    java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,...字符串 编号 题目 LeetCode 3 Longest Substring Without Repeating Characters LeetCode 13 Roman to Integer LeetCode 6 Zi

    gasstationleetcode-leetcode-rust:莱特代码休息

    3-longest-substring-without-repeating-characters 7 cargo run --bin 7-reverse-integer 9 cargo run --bin 9-palindrome-number 13 cargo run --bin 13-roman-to-integer 14 cargo run --bin 14-longest-common-...

    leetcode2sumc-Leetcode:记录我的大学Leetcode之旅。争取寒假能够整理完刷过的350题

    leetcode 2 sum c Leetcode 记录我的Leetcode之旅 题号 难度 知识点 完成情况 ...字符串 :check_mark_button: 每周打一次周赛防止老年痴呆。 clang++ -Wall -g -std=c++11 -stdlib=libc++ main.cpp -o main

Global site tag (gtag.js) - Google Analytics