leetcode_Longest Substring Without Repeating Characters

昨天看完了以前看一点没看下去的爆裂鼓手,真的说是彻底地被震撼了。要想to be a great one,只有不断地练习、练习、练习。。。

题目描述:

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. 题目的大意是给定一个字符串,找出该字符串中不含重复字母的最大长度的子串,返回其长度。

解题思路:

首先先来说这道题,一年前也曾做着这道题,但现在再翻出来看的话,已经看不出当初的思路。决定重新解这道题时,首先思路是很清晰的,循环找出字符串中每一个不含重复字符的子串,更新不重复子串的最大长度。具体是怎么判断子串中是否有重复字符,选择用数组而不是hashmap来判断字符串中是否有重复的字符,经过找工作的历练,这一点已经很好地成为直觉了。具体到程序的逻辑,还不是很熟练,修改过几次才通过。

另外一个地方,去年面试的时候,有面试官问我flagArr[str.charAt(start)] = false;中字符是否可以当数组的下标,就是上述的str.charAt(start),当时我认为是可以的,因为原来就这么用过,但只是当时还不是非常确定,跟面试官讲时还不是很有底气。还是那句话,要想to be a great one,只有不断地练习、练习、练习。。。

代码:

 public int lengthOfLongestSubstring(String s)
	{
		int start = 0;
		boolean flagArr[] = new boolean[256];
		int index = 0;
		int len = s.length();
		int flagArrIndex = 0;
		int maxLen = 0, curLen = 0;
		while (index < len)
		{
			while (index < len)
			{
				flagArrIndex = s.charAt(index);
				if(!flagArr[flagArrIndex])//move forward as long as possible
				{
					flagArr[flagArrIndex] = true;
					index++;
				}else
					break;
			}
			curLen = index - start;//update the max length
			if (curLen > maxLen)
				maxLen = curLen;
			flagArr[s.charAt(start)] = false;//every time,move forward for a letter
			start = start+1;
		}
		return maxLen;
	}
附论坛优秀代码

the basic idea is, keep a hashmap which stores the characters in string as keys and their positions as values, and keep two pointers which define the max substring. move the right pointer to scan through the string , and meanwhile update the hashmap. If the character is already in the hashmap, then move the left pointer to the right of the same character last found. Note that the two pointers can only move forward.

The variable "j" is used to indicate the index of first character of this substring. If the repeated character's index is less than j itself, which means the repeated character in the hash map is no longer available this time

 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;
    }

小结:

1.思路不能形成详细的代码,有个屁用。

2.边际条件是决定程序能够跑通的必要条件。

3.学习如何设计测试用例是减少无谓的错误的好方法。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值