【LeetCode-面試演算法經典-Java實現】【003-Longest Substring Without Repeating Characters(最長非重複子字串)】,longestsubstring

來源:互聯網
上載者:User

【LeetCode-面試演算法經典-Java實現】【003-Longest Substring Without Repeating Characters(最長非重複子字串)】,longestsubstring
【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 substring is “b”, with the length of 1.

題目大意

  給定一個字串,找字元中的最大非重複子串

解題思路

  用start記錄當前處理的開始位置 曆遍字串,噹噹前字元從開始位置start開始已經出現過的時候,子串開始位置+1,否則更新map中的hash值為當前位置

代碼實現
import java.util.Arrays;import java.util.HashMap;import java.util.Map;/** * Author: 王俊超 * Date: 2015-06-17 * Time: 20:46 * Declaration: All Rights Reserved !!! */public class Solution {    /**     * 003-Longest Substring Without Repeating Characters(最長非重複子字串)     *     * @param s 輸入字串     * @return 最大非重複子串長度     */    // 可以處理所有的UTF-8字元    public int lengthOfLongestSubstring(String s) {        // 字串輸入不合法        if (s == null) {            return 0;        }        // 當前處理的開始位置        int start = 0;        // 記錄到的最大非重複子串長度        int result = 0;        // 訪問標記,記錄最新一次訪問的字元和位置        Map<Character, Integer> map = new HashMap<>(s.length());        for (int i = 0; i < s.length(); i++) {            char ch = s.charAt(i);            // 如果字元已經出現過(在標記開位置算起),就重新標記start            if (map.containsKey(ch) && map.get(ch) >= start) {                start = map.get(ch) + 1;            }            // 如果沒有出現過就求最大的非重複子串的長度            else {                result = Math.max(result, i - start + 1);            }            // 更新訪問記錄            map.put(ch, i);        }        return result;    }    // 只考慮ASCII字元【解法二】    public int lengthOfLongestSubstring2(String s) {        // 字串輸入不合法        if (s == null) {            return 0;        }        // 標記字元是否出現過,並且記錄是的最新一次訪問的元素的位置        int[] appear = new int[256];        // 初始化為-1        Arrays.fill(appear, -1);        // 當前處理的開始位置        int start = 0;        // 儲存結果        int result = 0;        for (int i = 0; i < s.length(); i++) {            // 如果字元已經出現過(在標記開位置),就重新標記start            if (appear[s.charAt(i)] >= start) {                start = i + 1;            }            // 如果沒有出現過就求最大的非重複子串的長度            else {                result = Math.max(result, i - start + 1);            }            // 標記第i個字元已經被訪問過(最新是第i個位置)            appear[s.charAt(i)] = i;        }        return result;    }}
評測結果

  點擊圖片,滑鼠不釋放,拖動一段位置,釋放後在新的視窗中查看完整圖片。

特別說明歡迎轉載,轉載請註明出處【http://blog.csdn.net/derrantcm/article/details/46921991】

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.