LeetCode 3 — Longest Substring Without Repeating Characters (C++ Java Python)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/dragon_dream/article/details/19431417

题目:http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/

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.

题目翻译:

给定一个字符串,找到最长无重复字符的子串的长度。例如,"abcabcbb"的最长无重复字符的子串为"abc”,它的长度为3。"bbbbb”的最长子串为"b”,长度为1。

分析:

        注意特殊情况的考虑:

1 只有1个字符时,直接返回1

2 当到达字符串结尾时,要把此时的长度考虑进去

C++实现:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
		int length = s.length();
		if(length == 1)
		{
			return 1;
		}

		int longest = 0;
		int len = 0;
		string tmp;

		for(int i = 0; i < length; ++i)
		{
			if(longest > length - i)
			{
				break;
			}

			len = 0;
			for(int j = i + 1; j < length; ++j)
			{
				++len;

				tmp = s.substr(i, j - i);  // !
				auto pos = tmp.find(s[j]);
				if(pos != string::npos)
				{
					i += pos;

					int k = j;
					while((++k < length) && (s[i + 1] == s[k]))
					{
						++i;
					}

					if(longest < len)
					{
						longest = len;
					}

					break;
				}

				if(j == length -1)
				{
					longest = len + 1;
					break;
				}
			}
		}

		return longest;
    }
};

Java实现:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
		int length = s.length();
		if (length == 1) {
			return 1;
		}

		int longest = 0;
		int len = 0;
		String tmp;

		for (int i = 0; i < length; ++i) {
			if (longest >= length - i) {  // (1)
				break; 					  // no need to go on
			}
			
			len = 0;
			for (int j = i + 1; j < length; ++j) {
				++len;

				tmp = s.substring(i, j);
				int index = tmp.indexOf(s.charAt(j));
				if (index != -1) {
					i += index;
					// compare following items
					int k = j;  // in order to make it clear(works well without k)
					while ((++k < length) && (s.charAt(i + 1) == s.charAt(k))) {
						++i;
					}

					if (longest < len) {
						longest = len;
					}

					break;
				}

				if (j == length - 1) {
					longest = len + 1;  // this is true because of (1)
					break;              // to make it clear, do like this: 
							            // if (longest < len + 1) longest = len + 1;
				}
			}
		}

		return longest;
	}
}

Python实现:

class Solution:
    # @return an integer
    def lengthOfLongestSubstring(self, s):
        length = len(s)
        if length == 1:
            return 1
        
        longest = 0
        lenOfSubstr = 0
        
        i = 0
        while i < length:
            if longest > length - i:
                break
            
            lenOfSubstr = 0
            for j in range(i + 1, length):
                lenOfSubstr += 1 
                
                tmp = s[i : j]
                index = tmp.find(s[j]) 
                if index != -1:
                    i += index
                    
                    k = j + 1
                    while (k < length) and (s[i + 1] == s[k]):
                        i += 1
                        k += 1
                    
                    if longest < lenOfSubstr:
                        longest = lenOfSubstr
                        
                    break
                
                if j == length - 1:
                    longest = lenOfSubstr + 1
                    break
                
            i += 1
                
        return longest

        感谢阅读,欢迎评论!

展开阅读全文

Longest Palindromic Substring 最长回文子串问题?JAVA

06-20

rnrn```rnpublic class Solution rn public String longestPalindrome(String s) rn String ret = "";rn for (int i = 0; i < s.length(); i++) rn for (int j = 0; i - j >= 0 && i + j < s.length(); j++) rn if (s.charAt(i - j) == s.charAt(i + j)) rn if (j * 2 + 1 > ret.length()) rn ret = s.substring(i - j, i + j + 1);rn rn else rn break;rn rn rn for (int j = 0; i - j >= 0 && i + 1 + j < s.length(); j++) rn if (s.charAt(i - j) == s.charAt(i + j + 1)) rn if ((j + 1) * 2 + 1 > ret.length()) rn ret = s.substring(i - j, i + j + 2);rn rn else rn break;rn rn rn rn return ret;rn rnrn public static void main(String[] args) rn String s = "cdabbaef";rn Solution a = new Solution();rn System.out.println(a.longestPalindrome(s));rn rn rn```rnrnrnrn思路就是 因为回文字符串是以中心轴对称的,所以如果我们从下标 i 出发,用2个指针向 i 的两边扩展判断是否相等,那么只需要对0到rnn-1的下标都做此操作,就可以求出最长的回文子串。但需要注意的是,回文字符串有奇偶对称之分,即"abcba"与"abba"2种类型,rn因此需要在代码编写时都做判断。 rn编译也通过了。rn但是后来想到一个问题 例如:cdabbaef 里abba的子串 改成abea 源字符串变成cdabeaef 时 按照偶数情况的代码 结果也会输出abea 因为abea里的e没有比较 但是运行是发现 这个问题 已经被解决了 但是我想不通哪里避免了这种情况 请帮我分析一下 问答

没有更多推荐了,返回首页