【leetcode】3.Longest Substring Without Repeating Characters
来源:互联网 发布:杭州sql培训班 编辑:程序博客网 时间:2024/06/05 22:40
/*问题描述: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 the length of 1.Given "pwwkew", 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.*/class Solution { public int lengthOfLongestSubstring(String s) {int maxLength = 0;if ("".equals(s) || s == null) {return maxLength;}int maxLengthTmp = 0;int length = s.length();Set<String> set = new HashSet<String>();// 元素的不重复性int j = 0;for (int i = 0; i < length; i++) {if (set.add(s.substring(i, i + 1))) {maxLengthTmp++;if (maxLengthTmp > maxLength) {maxLength = maxLengthTmp;}} else {set = new HashSet<String>();// 重置maxLengthTmp = 0;j ++;i = j - 1;}}return maxLength;}}
leetcode最长字符串的消耗时间 183ms
阅读全文
1 0
- [LeetCode]3.Longest Substring Without Repeating Characters
- LeetCode 3.Longest Substring Without Repeating Characters
- LeetCode --- 3. Longest Substring Without Repeating Characters
- [Leetcode] 3. Longest Substring Without Repeating Characters
- [leetcode] 3.Longest Substring Without Repeating Characters
- [leetcode] 3. Longest Substring Without Repeating Characters
- [Leetcode]3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- 【Leetcode】3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- LeetCode-3.Longest Substring Without Repeating Characters
- LeetCode 3.Longest Substring Without Repeating Characters
- LeetCode - 3.Longest Substring Without Repeating Characters
- Leetcode 3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters LeetCode
- (Leetcode)3. Longest Substring Without Repeating Characters
- leetcode 3. Longest Substring Without Repeating Characters
- LeetCode-3. Longest Substring Without Repeating Characters
- JAVA企业面试题精选 Hibernate 11-20
- [PAT]1002. A+B for Polynomials (25)
- zboj 2301 Problem b (莫比乌斯反演)
- 性能测试之linux
- 一个快捷PDF转JPG的方法,有效提升文档办公效率
- 【leetcode】3.Longest Substring Without Repeating Characters
- Maven安装打包失败问题
- 求二叉搜索树(BST)的最小高度
- C语言数据类型
- RxAndroid之操作数据库SqlBrite(RXAndroid实现数据库的增、删、改、查)
- NIO直接缓冲区与非直接缓冲区
- fatal error C1083: 无法打开包括文件:“opencv2/core/core_c.h”: No such file
- 在Windows下编译扩展OpenCV 3.1.0 + opencv_contrib 及一些问题
- 文章标题 leetcode刷题日记——最长子串