3.Longest Substring Without Repeating Characters(7.66%)
2016-01-06 20:10
417 查看
题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.
个人理解:求最长的没有重复的字符串的长度。刚开始的时候贪图便利,用Java自带的字符串方法进行操作,结果超时了:
后来百度了一下,才想起Java字符数组:
个人理解:求最长的没有重复的字符串的长度。刚开始的时候贪图便利,用Java自带的字符串方法进行操作,结果超时了:
public class Solution { public int lengthOfLongestSubstring(String s) { int inputLength = s.length(); int longestLength = 0; for (int i = 0; i < inputLength; i++) { String tempString = String.valueOf(s.charAt(i)); for (int j = i + 1; j < inputLength; j++) { String character = String.valueOf(s.charAt(j)); if (tempString.contains(character)) { break; } else { tempString = tempString.concat(character); } } longestLength = longestLength > tempString.length() ? longestLength : tempString.length(); } return longestLength; } }
后来百度了一下,才想起Java字符数组:
public class Solution { public int lengthOfLongestSubstring(String s) { int inputLength = s.length(); char[] chars = s.toCharArray(); int longestLength = 0; for (int i = 0; i < inputLength; i++) { int oneLength = 0; for (int j = i + 1; j < inputLength; j++) { boolean isLegal = true; for (int k = i; k < j; k++) { if (chars[k] == chars[j]) { isLegal = false; break; } } if (isLegal) { oneLength++; } else { break; } } oneLength++; longestLength = longestLength > oneLength ? longestLength : oneLength; if (longestLength == inputLength) { return longestLength; } } return longestLength; } }
相关文章推荐
- 【POJ】[1852]Ants
- 机器学习入门系列三(关键词:逻辑回归,正则化)
- jQuery Dialog API (一)
- 淘宝的架构 -君子不器原创文章
- java jar war 打包
- jquery uploadify插件多文件上传
- Spark NMF(非负矩阵分解)算法实现与测试
- binder驱动-接收缓存区管理
- java+selenium Q&A
- Java中的抽象类与接口
- 安卓相对布局的基本属性
- 隐马尔可夫模型(HMM)
- 如何制作gif图片?如何制作你项目的动态效果图到你的csdn?
- 【南理oj】6 - 喷水装置(一)(贪心)
- TableView Segues
- INSERTDELETES&NOINSERTDELETES--GoldenGate Parameter
- @property 的基本认识
- Linux基本的命令与快捷建(在centos版本下)
- Pascal's Triangle leetcode
- Sicily 1741. Jaunt around the Zhuhai Campus