给定一个字符串,找出不含有重复字符的最长子串的长度。
示例 1:
输入: "abcabcbb"
输出: 3
解释: 无重复字符的最长子串是 "abc",其
长度为 3。
示例 2:
输入: "bbbbb"
输出: 1
解释: 无重复字符的最长子串是 "b"
,其长度为 1。
示例 3:
输入: "pwwkew" 输出: 3 解释: 无重复字符的最长子串是"wke"
,其长度为 3。 请注意,答案必须是一个子串,"pwke"
是一个子序列 而不是子串。
解法:
class Solution { public int lengthOfLongestSubstring(String s) { int len = 0; String crt = ""; for(int i = 0; i < s.length(); i++) { String c = s.charAt(i) + ""; if(crt.contains(c)) { if(crt.length() > len) { len = crt.length(); } crt = crt.substring(crt.indexOf(c) + 1); } crt += c; } return len > crt.length()? len : crt.length(); } }
相关推荐
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-Longest_Substring_Without_Repeating_Characters 这是LeetCode上“最长子串无重复字符”问题的解决方案。 问题描述:给定一个字符串,求没有重复字符的最长子串的长度。 示例 1:输入:“abcabcbb” ...
LeetCode-3.Longest_Substring_Without_Repeating_Characters 给定一个字符串,找出没有重复字符的最长子字符串的长度。 示例 1: 输入:“abcabcbb” 输出:3 解释:答案是“abc”,长度为 3。 解决方案 Python3:...
Leetcode回文串拼接 leetcode_node 题解 该项目主要用于基于Leetcode的刷题记录,与日常学习,对Leetcode上的题目按照解题方法进行分明别类的整理。 题目列表 1.Two Sum 2.Add Two Numbers 3.Longest Substring ...
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...
leetcode数组下标大于间距 LeetCode_Solutions :party_popper:My LeetCode solutions 0001. Two Sum 0002. Add Two Numbers 0003. Longest Substring Without Repeating Characters 解题思路 使用 st[i] 保存以i结尾...
leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:...
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 Introduction 本文旨在将leetcode的题型分类 Pattern: Sliding window 滑动窗口类型的题目经常是用来执行数组或是链表上某个区间(窗口)上的操作。比如找最长的全为1的子数组长度。滑动窗口...
0003_Longest_Substring_Without_Repeating_Characters :star: 0004_Median_of_Two_Sorted_Arrays :star: 0005_Longest_Palindromic_Substring :star: 0006_ZigZag_Conversion 0007_Reverse_Integer 0008_String_to_...
leetcode算法题主函数如何写 leetcode 1.Two Sum python中的hashmap用字典实现,可以用get方法获取key对应的value,用is not None判断是否在hashmap中. PS:用in/not in来判断是否在list中。list获取特定元素的索引:...
java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...Characters LeetCode 13 Roman to Integer LeetCode 6 Zi
leetcode中国 leetcode_python 通过刷leetcode题目,来锻炼自己的编程思想和编程能力。这里我会首先通过自己的努力去完成每一道题目,解决不了的我也会参考各位大佬的答案。当然每一道题目我都用两种方法去完成,一...
leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance Difficulty Topics Solution 0001 Two Sum 46.1%...
leetcode走方格起点到终点 LeetCode Solution 题目后第一个框框记录耗费时间在提交时击败网友的百分比 题目后第二个框框记录耗费空间在提交时击败网友的百分比 #1 Two Sum 92.82% 39.78% 用Hash Table加速检索,一边...
leetcode Python 001 力码 ├── Algorithms │ ├── cpp │ │ ├── 001 - Two Sum.cpp │ │ ├── 002 - Add Two Numbers.cpp │ │ ├── 003 - Longest Substring Without Repeating ...
gas station leetcode Rust Leetcode 练习使用Rust语言刷或者算法题目, ...p0003_longest_substring_without_repeating_characters; pub mod p0005_longest_palindromic_substring; pub mod p0007_reverse_int
leetcode实现strstr leetcode-js 记录刷leetcode分析过程,希望一点点进步! leetcode地址 刷题顺序 按照顺序刷第一遍,记录实现思路,自己的优化方案,并研究高票大佬的思路。 已完成题目归档 序号 题目 题解 实现...
华为 leetcode mistake-collection This is mistake collection for exercise publoms ...Source2:LeetCode ...Substring Without Repeating Characters code_6.org 二进制链表转整数 code_7.org 最小高度树
Longest Substring Without Repeating Characters 哈希表 双指针 滑动窗口 Substring with Concatenation of All Words 哈希表 注意匹配方向 Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 ...