From a4aaa25de2725e1dbe14e582bb0ff8d363306264 Mon Sep 17 00:00:00 2001 From: JackieZhu Date: Sun, 29 Mar 2015 15:56:20 +0800 Subject: [PATCH] logestsubstringwithoutrepeatingcharacters --- ...stSubstringWithoutRepeatingCharacters.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 leetcode/src/com/jackiezhu/LongestSubstringWithoutRepeatingCharacters.java diff --git a/leetcode/src/com/jackiezhu/LongestSubstringWithoutRepeatingCharacters.java b/leetcode/src/com/jackiezhu/LongestSubstringWithoutRepeatingCharacters.java new file mode 100644 index 0000000..1af35bd --- /dev/null +++ b/leetcode/src/com/jackiezhu/LongestSubstringWithoutRepeatingCharacters.java @@ -0,0 +1,24 @@ +package com.jackiezhu; + +/** + * Created by JackieZhu on 15/3/29. + */ +public class LongestSubstringWithoutRepeatingCharacters { + public int lengthOfLongestSubstring(String s) { + int []hash = new int[256]; + int start = 0, end = 0, ret = 0; + while(end < s.length()) { + int c = s.charAt(end); + if(hash[c] == 0) { + hash[c] ++; + ret = Math.max(ret, end-start+1); + end ++; + } else { + hash[s.charAt(start)] --; + start ++; + } + } + return ret; + } +} + wqqsxq