作者Rushia (早瀬ユウカの体操服 )
標題Re: [閒聊] 每日leetcode
時間2024-12-03 21:57:04
※ 引述《JerryChungYC (JerryChung)》之銘言:
: https://leetcode.com/problems/adding-spaces-to-a-string
: 2109. Adding Spaces to a String
: 在指定index插空格
: Example 1:
: Input: s = "LeetcodeHelpsMeLearn", spaces = [8,13,15]
: Output: "Leetcode Helps Me Learn"
: Example 2:
: Input: s = "icodeinpython", spaces = [1,5,7,9]
: Output: "i code in py thon"
: Example 3:
: Input: s = "spacing", spaces = [0,1,2,3,4,5,6]
: Output: " s p a c i n g"
: Constraints:
: 1 <= s.length <= 3 * 10^5
: s 只有大小寫英文字母
: 1 <= spaces.length <= 3 * 10^5
: 0 <= spaces[i] <= s.length - 1
: spaces內的值是嚴格遞增
我以為是考雙指針
隨便寫寫贏過90%
又水了一天
Java code:
--------------------------------------------------------
class Solution {
public String addSpaces(String s, int[] spaces) {
StringBuilder sb = new StringBuilder();
int l = 0;
int r = 0;
for (int space : spaces) {
while (r < space) {
r++;
}
sb.append(s, l, r).append(" ");
l = r;
}
sb.append(s, l, s.length());
return sb.toString();
}
}
----------------------------------------------------------
--
https://i.imgur.com/O931L58.jpeg
--
※ 發信站: 批踢踢實業坊(pttsite.org.tw), 來自: 49.158.191.3 (臺灣)
※ 文章網址: https://pttsite.org.tw/Marginalman/M.1733234227.A.965