LeetCode 003

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) {
			return 0;
		}
		int maxLength = 0;
		char[] array = s.toCharArray();
		int[] record = new int[array.length];
		for (int i = 0; i < array.length; i++) {

			if (i == 0) {
				record[i] = 1;
			} else {
				record[i] = record[i - 1]+ (checkRepeat(s.substring(i - record[i - 1], i + 1)) ? 1: 0);
			}
		}
		return record[record.length - 1];
	}

	public boolean checkRepeat(String s) {
		char[] array = s.toCharArray();
		boolean[] map = new boolean[256];
		for (int i = 0; i < map.length; i++) {
			map[i] = false;
		}
		for (int i = 0; i < array.length; i++) {
			if (map[array[i]]) {
				return false;
			}
			map[array[i]] = true;
		}
		return true;
	}
}

第一次做这到题的思路,从0到字符串长度,一个字符一个字符的添加。record数组index记录 0-index长度字串的解。

1. 一个字符的时候的解肯定为1;

2. 后面添加一个字符就取 之前解的长度的字串并上最后一个字符的字串,检查是否有重复。在之前字符的解的基础上有重复的话解+0,没重复的话解+1。  

然后,参考了一些大神的解法:

public class Solution {
    public int lengthOfLongestSubstring(String string) {
        int[] a = new int[256];
        char[] s = string.toCharArray();
        int len = s.length;
        int result = 0, max = 0;
        int temp = 0, low = 0;
        for (int i = 0; i < len; ++i) {
            temp = a[s[i]];
            if (temp != 0) {
                for (int j = low; j < temp - 1; j++) {
                    a[s[j]] = 0;
                }
                result -= temp - low - 1;
                low = temp;
                a[s[i]] = i + 1;
            } else {
                a[s[i]] = i + 1;
                if (++result > max)
                    max = result;
            }
        }
        return max;
    }
}

这种方法的复杂度为O(n),对字符串长度的数组只扫描一次。思路是low记录起点,a记录起点之后的字符出现的位置+1,result记录起点之后不含重复字母的字串的长度。

时间: 2024-10-10 05:54:29

LeetCode 003的相关文章

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 003.Longest_Substring_Without_Repeating_Characters (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Substring-Without-Repeating-Characters/ 代码(github)

Java for LeetCode 003 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 subst

LeetCode 003. Longest Substring Without Repeating

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 subst

LEETCODE 003 找出一个字符串中最长的无重复片段

题目: 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

[leetCode][003] Intersection of Two Linked Lists

[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2   c1 → c2 → c3       B: b1 → b2 → b3 begin to intersect at node c1. Notes: 1. If the two linke

【LeetCode】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-1.所以得到如下的递推公式: 另外由于不确定字符串长度的

Leetcode刷题第003天

一.只出现一次的数字 1 class Solution { 2 public: 3 int singleNumber(vector<int>& nums) 4 { 5 int res = 0; 6 for (auto num : nums) 7 res ^= num; 8 return res; 9 } 10 }; 原文地址:https://www.cnblogs.com/sunbines/p/10585733.html

【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Exp

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med