久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Java怎么使用指針進行搜索

150次閱讀
沒有評論

共計 3063 個字符,預計需要花費 8 分鐘才能閱讀完成。

這篇文章主要講解了“Java 怎么使用指針進行搜索”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“Java 怎么使用指針進行搜索”吧!

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.*
package com.lifeibigdata.algorithms.leetcode;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
 * Created by lifei on 16/5/27.
 *
 *  時間復雜度為 O(N) 的算法
  使用 i 和 j 兩個指針進行搜索,i 代表候選的最長子串的開頭,j 代表候選的最長子串的結尾。  先假設 i 不動,那么在理想的情況下,我們希望可以一直右移 j,直到 j 到達原字符串的結尾,此時 j - i 就構成了一個候選的最長子串。每次都維護一個 max_length,就可以選出最長子串了。  實際情況是,不一定可以一直右移 j,如果字符 j 已經重復出現過(假設 i 在位置 k),就需要停止右移了。記錄當前的候選子串并和 max_length 做比較。接下來為下一次搜尋做準備。  在下一次搜尋中,i 應該更新到 k +1。這句話的意思是,用這個例子來理解,abcdef 是個不重復的子串,abcdefc 中(為了方便記錄為 abc1defc2),c1 和 c2 重復了。  那么下一次搜尋,應該跨過出現重復的地方進行,否則找出來的候選串依然有重復字符,且長度還不如上次的搜索。所以下一次搜索,直接從 c1 的下一個字符 d 開始進行,  也就是說,下一次搜尋中,i 應該更新到 k +1
 */
public class LengthOfLongestSubstring { public static void main(String[] args) { LengthOfLongestSubstring lols = new LengthOfLongestSubstring();
 System.out.println(lols.lengthOfLongestSubstring( abcdefcgh));
 }
 public int lengthOfLongestSubstring(String s) { int n = s.length(), ans = 0;
 int[] index = new int[128]; // current index of character
 // try to extend the range [i, j]
 for (int j = 0, i = 0; j   n; ++j) { i = Math.max(index[s.charAt(j)], i);//index[a] 會將 char 轉為 ascII 碼,a 是 97
 ans = Math.max(ans, j - i + 1);
 index[s.charAt(j)] = j + 1; // 將 j 所在的值, 的對應位置存到 index 數組中
 }
 return ans;
 }

// public int lengthOfLongestSubstring(String s) {// int n = s.length(); // Set Character  set = new HashSet (); // int ans = 0, i = 0, j = 0; // while (i   n   j   n) {// // try to extend the range [i, j] // if (!set.contains(s.charAt(j))){ // 如果 j 沒有出現重復字符, 將 j 添加到 set 中, 這個過程中,i 保持不變 // set.add(s.charAt(j++)); // ans = Math.max(ans, j - i); // 比較獲取最大的 ans // } // else { // 出現重復字符, 這個字符在 i - j 之間, 所以從 i 開始逐個刪除, 直到不包含重復字符 // set.remove(s.charAt(i++)); // } // } // return ans; // }
// public int lengthOfLongestSubstring(String s) { // 使用 map 存儲字母和索引 // int n = s.length(), ans = 0; // Map Character, Integer  map = new HashMap (); // current index of character // // try to extend the range [i, j] // for (int j = 0, i = 0; j   n; ++j) {// if (map.containsKey(s.charAt(j))) {// i = Math.max(map.get(s.charAt(j)), i); // } // ans = Math.max(ans, j - i + 1); // map.put(s.charAt(j), j + 1); // } // return ans; // }
// public int lengthOfLongestSubstring(String s) {// int n = s.length(); // int ans = 0; // for (int i = 0; i   n; ++i) // for (int j = i + 1; j  = n; ++j) // if (allUnique(s, i, j)) ans = Math.max(ans, j - i); // return ans; // } // public boolean allUnique(String s, int start, int end) {// Set Character  set = new HashSet (); // for (int i = start; i   end; ++i) {// Character ch = s.charAt(i); // if (set.contains(ch)) return false; // set.add(ch); // } // return true; // } }

感謝各位的閱讀,以上就是“Java 怎么使用指針進行搜索”的內容了,經過本文的學習后,相信大家對 Java 怎么使用指針進行搜索這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-16發表,共計3063字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 神农架林区| 华坪县| 榕江县| 锡林郭勒盟| 炉霍县| 十堰市| 普安县| 遂昌县| 正蓝旗| 德昌县| 万安县| 江油市| 孝昌县| 南川市| 武清区| 丰台区| 池州市| 绥阳县| 沅陵县| 广州市| 巴马| 若尔盖县| 望奎县| 城市| 宁河县| 都匀市| 昌吉市| 呼伦贝尔市| 平凉市| 塔河县| 新泰市| 蕉岭县| 精河县| 喜德县| 石渠县| 杭锦后旗| 柳林县| 和平区| 金华市| 门源| 子洲县|