首页 > 代码库 > Leetcode: Longest Substring Without Repeating Characters
Leetcode: Longest Substring Without Repeating Characters
1 public class Solution { 2 public int lengthOfLongestSubstring(String s) { 3 int length = s.length(); 4 if (length == 0) return 0; 5 int begin = 0; 6 int end = 1; 7 int longest = 0; 8 int value = http://www.mamicode.com/0; 9 Hashtable<Integer, Character> checker = new Hashtable<Integer, Character>(); 10 while (end <= length) { 11 String temp = s.substring(begin, end); 12 if (isunique(temp, checker)) { 13 value = http://www.mamicode.com/(int)(s.charAt(end-1) - ‘\0‘); 14 checker.put(value, s.charAt(end-1)); 15 end++; 16 longest++; 17 } 18 else { 19 checker.remove((int)(s.charAt(begin) - ‘\0‘)); 20 begin++; 21 end++; 22 } 23 } 24 return longest; 25 } 26 27 public boolean isunique(String str, Hashtable<Integer, Character> checker) { 28 char c = str.charAt(str.length() - 1); 29 if (checker.containsKey((int) c)) return false; 30 else return true; 31 } 32 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。