在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ A string of We are given a string Return the minimum number of flips to make Example 1: Input: 1
Explanation: We flip the last digit to get 00111.
Example 2: Input: 2
Explanation: We flip to get 011111, or alternatively 000111.
Example 3: Input: 2
Explanation: We flip to get 00000000.
Note:
如果一个由 我们给出一个由字符 返回使 示例 1: 输入:"00110" 输出:1 解释:我们翻转最后一位得到 00111. 示例 2: 输入:"010110" 输出:2 解释:我们翻转得到 011111,或者是 000111。 示例 3: 输入:"00011000" 输出:2 解释:我们翻转得到 00000000。 提示:
36ms 1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 var zeros = 0; 4 var ones = 0; 5 var res = 0; 6 var S = Array(S) 7 for i in S { 8 if i == "0" { 9 zeros+=1 10 } else { 11 ones+=1 12 } 13 } 14 15 var start = 0; 16 var end = S.count - 1; 17 while end > start { 18 if ones >= zeros { 19 if(S[end] == "1") { 20 ones-=1 21 } 22 if S[end] == "0" { 23 zeros-=1 24 res+=1 25 } 26 end-=1 27 } 28 else{ 29 if(S[start] == "0") { 30 zeros-=1 31 } 32 if(S[start] == "1"){ 33 ones-=1; 34 res+=1; 35 } 36 start+=1; 37 } 38 } 39 return res; 40 } 41 } 56ms 1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 4 var chars = Array(S) 5 let totalItems = chars.count 6 var prefixSums = [Int](repeating: 0, count: totalItems + 1) 7 for i in 0..<chars.count { 8 prefixSums[i + 1] = prefixSums[i] + (chars[i] == "1" ? 1 : 0) 9 } 10 var result = Int.max 11 for i in 0..<prefixSums.count { 12 13 let flipCounter = prefixSums[i] + (totalItems - i) - (prefixSums.last! - prefixSums[i]) 14 result = min(result, flipCounter) 15 } 16 return result 17 } 18 } 60ms 1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 var s: [Int] = S.map { $0 == "0" ? 0 : 1} 4 var onlyOnes: [Int] = Array(repeating: 0, count: s.count) 5 var onlyZeros: [Int] = Array(repeating: 0, count: s.count) 6 7 for i in (0..<s.count).reversed() { 8 let prev = (i < (s.count - 1)) ? onlyOnes[i+1] : 0 9 onlyOnes[i] = (s[i] == 0 ? 1 : 0) + prev 10 } 11 12 for i in 0..<s.count { 13 let prev = (i > 0) ? onlyZeros[i-1] : 0 14 onlyZeros[i] = (s[i] == 1 ? 1 : 0) + prev 15 } 16 17 var minFlips = min(onlyZeros.last!, onlyOnes.first!) 18 for i in 0..<(onlyOnes.count - 1) { 19 minFlips = min(minFlips, onlyZeros[i] + onlyOnes[i+1]) 20 } 21 return minFlips 22 } 23 } 108ms 1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 var arr:[Character] = [Character]() 4 for char in S.characters 5 { 6 arr.append(char) 7 } 8 let len = S.count 9 //声明数组 10 var ct:[Int] = [Int](repeating: 0,count: len + 1) 11 var x:Int = 0 12 for i in 0...len 13 { 14 ct[i] += x 15 if i < len && arr[i] == "1" 16 { 17 x += 1 18 } 19 } 20 x = 0 21 for i in (0...len).reversed() 22 { 23 ct[i] += x 24 if i > 0 && arr[i - 1] == "0" 25 { 26 x += 1 27 } 28 } 29 var res = 999999999 30 for i in 0...len 31 { 32 res = min(res,ct[i]) 33 } 34 return res 35 } 36 } 116ms 1 class Solution { 2 func minFlipsMonoIncr(_ S: String) -> Int { 3 var arr:[Character] = [Character]() 4 for char in S.characters 5 { 6 arr.append(char) 7 } 8 let len = S.count 9 //声明数组 10 var ct:[Int] = [Int](repeating: 0,count: len + 1) 11 var x:Int = 0 12 for i in 0...len 13 { 14 ct[i] += x 15 if i < len && arr[i] == "1" 16 { 17 x += 1 18 } 19 } 20 x = 0 21 for i in (0...len).reversed() 22 { 23 ct[i] += x 24 if i > 0 && arr[i - 1] == "0" 25 { 26 x += 1 27 } 28 } 29 var res = 999999999 30 for i in 0...len 31 { 32 res = min(res,ct[i]) 33 } 34 return res 35 } 36 }
|
请发表评论