Re: [閒聊] 每日LeetCode

看板Marginalman作者 (みけねこ的鼻屎)時間1年前 (2023/12/31 14:10), 編輯推噓1(102)
留言3則, 3人參與, 1年前最新討論串590/719 (看更多)
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

12/31 14:14, 1年前 , 1F
大師
12/31 14:14, 1F

12/31 14:16, 1年前 , 2F
java string好像可以直接取最前後的字
12/31 14:16, 2F

12/31 14:17, 1年前 , 3F
JAVA也可以 在面試官面前造輪子 哈
12/31 14:17, 3F
文章代碼(AID): #1baGMx-L (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1baGMx-L (Marginalman)