精華區beta Marginalman 關於我們 聯絡資訊
https://leetcode.com/problems/largest-substring-between-two-equal-characters/description 1624. Largest Substring Between Two Equal Characters 給你一個字串s,求出一個最長子字串,他的左右兩邊需存在一個相同的字元,如果不存 在相同字元返回 -1。 思路: 1.找到每個字元的最左和最右出現的位置,並取寬度最大的返回。 Java Code: -------------------------------------------- class Solution { public int maxLengthBetweenEqualCharacters(String s) { int[] first = new int[26]; int[] last = new int[26]; for (int i = 0; i < s.length(); i++) { last[s.charAt(i) - 'a'] = i; } for (int i = s.length() - 1; i >= 0; i--) { first[s.charAt(i) - 'a'] = i; } int res = -1; for (int i = 0; i < 26; i++) { res = Math.max(res, last[i] - first[i] - 1); } return res; } } -------------------------------------------- -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1704003003.A.F95.html
oin1104: 大師 12/31 14:14
penguinsFly: java string好像可以直接取最前後的字 12/31 14:16
Rushia: JAVA也可以 在面試官面前造輪子 哈 12/31 14:17