zoukankan      html  css  js  c++  java
  • leetcode146 longest-substring-without-repeating-character

    题目描述

    给定一个字符串,找出最长的不具有重复字符的子串的长度。例如,“abcabcbb”不具有重复字符的最长子串是“abc”,长度为3。对于“bbbbb”,最长的不具有重复字符的子串是“b”,长度为1。

    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.
    import java.util.*;
    import java.util.HashMap;
    /*
    滑动窗口,比方说,abcabccc  当你右边扫描到abca的时候,你得把第一个a删掉得到bca,
    然后,“窗口”继续向右滑动,每当加到一个新的char的时候,左边检查有无重复的char,然后如果没有重复的就正常添加
    有重复的话就左边扔掉一部分,从最左到重复char这段扔掉,在这个过程中记录最大窗口长度

    */

    public class Solution {
        /**
         *
         * @param s string字符串
         * @return int整型
         */
        public int lengthOfLongestSubstring (String s) {
            // write code here
            if (s==null || s.length()==0) return 0;
            HashMap <Character,Integer> map=new HashMap<Character ,Integer>();
            int leftBound=0;
            int max=0;
            for (int i=0;i<s.length();i++){
                char c=s.charAt(i);
                leftBound=Math.max(leftBound,(map.containsKey(c))?map.get(c)+1:0);
                max=Math.max(max,i-leftBound+1);
                map.put(c,i);
                
            }
            return max;
            
        }
    }
  • 相关阅读:
    Oracle Hint的用法
    利用flashback transaction query新特性进行事务撤销
    存储的一些基本概念(HBA,LUN)
    SAN和NAS
    SAN (Storage Attached Network),即存储区域网络
    深入浅出谈存储之NAS是什么
    对于NAS,IP SAN以及iSCSCI SAN存储的一些认识和理解
    Oracle的体系结构
    利用360免费wifi搭建局域网让他人访问Oracle数据库
    杭电ACM id:3783
  • 原文地址:https://www.cnblogs.com/hrnn/p/13338583.html
Copyright © 2011-2022 走看看