Leetcode 详解(Substing without repeats character)

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 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

我自己的代码:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int slenmax=0;
        for(int i=0;i<s.length();i++)
        {
            int j=i+1;
            boolean flag=true;
            while(j<s.length()&&flag)
            {
                for(int k=i;k<j;k++)
                {

                   if(s.charAt(j)==s.charAt(k))
                   {
                       flag=false;
                       j--;
                   }
                }
                j++;
            }
            int slen=j-i;
            if(slen>slenmax)
                slenmax=slen;
        }
        return slenmax;
    }
}

运行时可以通过,但是在提交时出现超时(思路比较简单,因而时间复杂度相应会比较高)

clean Code:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        boolean[] exist = new boolean[256];             //用表格处理,查找时会很快
        int i = 0, maxLen = 0;
        for (int j = 0; j < s.length(); j++) {
            while (exist[s.charAt(j)]) {                //这个while是精髓
                exist[s.charAt(i)] = false;
                i++;
            }
            exist[s.charAt(j)] = true;
            maxLen = Math.max(j - i + 1, maxLen);
        }
        return maxLen;
    }
} 

注:借用了表的形式,把字符串中的字母依次存入表中并进行相应标记(如 exist[s.charAt(i)] = false;)。解决思路是:设定两个指针(i, j),都放在左头开始,从左到右,遇到有两个相同字母的情况,则计算两相同字母间距,更新最大间距,并且把i 也放在j 处,再进行计算最大间距。

时间: 2024-10-28 12:16:19

Leetcode 详解(Substing without repeats character)的相关文章

Leetcode 详解(ReverseWords)

Leetcode里面关于字符串的一些问题,描述如下: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it inO(1) space. click to show cla

Leetcode 详解(Valid Number)

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 解决方案: public class Solution { public boolean isNumber(String s) { in

Leetcode 详解(valid plindrome)

Question: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome.Example Questions C

The Skyline Problem leetcode 详解

class Solution { public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { vector<pair<int, int> > h, res; multiset<int> m; int pre = 0, cur = 0; for (auto &a : buildings) { h.push_back({

Leetcode 详解(股票交易日)(动态规划DP)

问题描述: 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行).给出一天中的股票变化序列,请写一个程序计算一天可以获得的最大收益.请采用实践复杂度低的方法实现. 给定价格序列prices及它的长度n,请返回最大收益.保证长度小于等于500. class Solution { public: int maxProfit(vector<int>& prices) { //It's wrong if you c

【leetcode】Edit Distance 详解

下图为TI C6xx DSP Nyquist总线拓扑图,总线连接了master与slave,提供了高速的数据传输.有很多种速率不同的总线,如图中的红色方框,最高速总线为CPU/2 TeraNet SCR(即VBUSM SCR),带宽为256bit,其他低速总线为CPU/3,CPU/6,带宽参考图中所示.总线之间用Bridge(桥)连接,作用包括转换总线的速率,使之与所流向总线的速率相同等. 在具体应用中,各种速率的总线完全可以满足复杂的数据传输,而数据传输的瓶颈往往在于连接总线之间的Bridge

LeetCode ---Anagrams() 详解

Notice: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. For example: Input: ["tea","and","ate","eat","den"] Output:   ["tea",&qu

LeetCode Go 并发题详解:交替打印字符串

原文地址:https://mp.weixin.qq.com/s/K032xlARjiyS8ecJrqZXaA 本题 LeetCode 链接: https://leetcode.com/problems/fizz-buzz-multithreaded/ 本题题目 给定一个数列从 1 ~ n,依序输出,但是: 如果 n 可以被 3 整除,输出 "fizz" 如果 n 可以被 5 整除,输出 "buzz" 如果 n 同时可以被 3 与 5 整除,输出 "fizz

leetcode之反转链表图文详解

206-反转链表 题目: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 思路: 迭代法: 新建一个链表的头部,循环遍历旧链表的结点,将其加到新链表的后面 递归法 代码:(迭代法) /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *