3. Longest Substring Without Repeating Characters
来源:互联网 发布:bs点炒股软件 编辑:程序博客网 时间:2024/06/16 13:48
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 asubstring,"pwke"
is a subsequence and not a substring.
我的思路:
每次用两个端点确定一个子字符串,其中左端点固定,右端点一支向右走,当遇到右端点指向的字符在该字符串中出现第二次时,说明去掉这个字符就是该子字符串的长度,和以前的最大长度比较。需要注意不要out of index. 从i到I+j长度为J+1
STRING的方法:
substring,indexOf,cahrAt
class Solution { public int lengthOfLongestSubstring(String s) { if(s.equals("")) return 0; else{ int len = s.length(); int max = 1; for(int i = 0;i<len;i++){ for(int j=1;j<len-i;j++){ String sub = s.substring(i,i+j); if((sub.indexOf(s.charAt(i+j)))!=-1) break; max = ((j+1)>max)?(j+1):max; } } return max; } }}
public int lengthOfLongestSubstring(String s) { if (s.length()==0) return 0; HashMap<Character, Integer> map = new HashMap<Character, Integer>(); int max=0; for (int i=0, j=0; i<s.length(); ++i){ if (map.containsKey(s.charAt(i))){ j = Math.max(j,map.get(s.charAt(i))+1); } map.put(s.charAt(i),i); max = Math.max(max,i-j+1); } return max; }
public int lengthOfLongestSubstring(String s) { int i = 0, j = 0, max = 0; Set<Character> set = new HashSet<>(); while (j < s.length()) { if (!set.contains(s.charAt(j))) { set.add(s.charAt(j++)); max = Math.max(max, set.size()); } else { set.remove(s.charAt(i++)); } } return max;}
hashset:http://blog.csdn.net/chenssy/article/details/21988605
HashSet继承AbstractSet类,实现Set、Cloneable、Serializable接口。其中AbstractSet提供 Set 接口的骨干实现,从而最大限度地减少了实现此接口所需的工作。Set接口是一种不包括重复元素的Collection,它维持它自己的内部排序,所以随机访问没有任何意义.
方法:
iterator()方法返回对此 set 中元素进行迭代的迭代器。返回元素的顺序并不是特定的。
size()返回此 set 中的元素的数量(set 的容量)。底层调用HashMap的size方法,返回HashMap容器的大小。
isEmpty(),判断HashSet()集合是否为空,为空返回 true,否则返回false。
、contains(),判断某个元素是否存在于HashSet()中,存在返回true,否则返回false。
add()如果此 set 中尚未包含指定元素,则添加指定元素。如果此Set没有包含满足(e==null ? e2==null : e.equals(e2)) 的e2时,则将e2添加到Set中,否则不添加且返回false。由于底层使用HashMap的put方法将key = e,value=PRESENT构建成key-value键值对,当此e存在于HashMap的key中,则value将会覆盖原有value,但是key保持不变,所以如果将一个已经存在的e元素添加中HashSet中,新添加的元素是不会保存到HashMap中,所以这就满足了HashSet中元素不会重复的特性。
remove如果指定元素存在于此 set 中,则将其移除。
clear从此 set 中移除所有元素。
hashtable: http://blog.csdn.net/chenssy/article/details/22896871
- [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] 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters
- [leetcode] 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- [leetcode] 3. Longest Substring Without Repeating Characters
- 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
- 小知识点2
- 双向链表的操作
- springboot实现ajax跨域请求
- (十二)定时事件集成到多路IO机制
- windows+tensorflow+fasterRcnn---3
- 3. Longest Substring Without Repeating Characters
- 基于SSM的组织树的实现
- 虚拟机可正常启动但虚拟机窗口无法正常显示
- 我的第一篇博客
- 百度语音合成的使用
- eclipse导入一个项目之后没有JRE System Library怎么办?
- Linux功耗管理(30)Linux CPU core的电源管理(2)_cpu topology
- java中substring与substr的用法
- expect 安装