You are given a binary string s. In one second, all occurrences of "01" are simultaneously replaced with "10". This process repeats until no occurrences of "01" exist.
Return the number of seconds needed to complete this process.
題目給咱一個二進位字串 s,在一秒內,所有出現的01會同時被替換成10。
一直重複這件事,直到字串中不再出現 "01" 為止。
目標是返回所需秒數。
我的解題思路:
class Solution {
public int secondsToRemoveOccurrences(String s) {
int sec= 0;
// 當有字串有01就執行
while (s.contains("01")) {
// 替換
s = s.replaceAll("01", "10");
sec++;
}
return sec;
}
}
碰巧用上了剛開始鐵人賽時學大神用的方法,最近也快完賽了,有中首尾呼應的感覺!