在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In a string composed of Example: Input: start = "RXXLRXRXL", end = "XRLXXRRLX" Output: True Explanation: We can transform start to end following these steps: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX Note:
在一个由 示例 : 输入: start = "RXXLRXRXL", end = "XRLXXRRLX" 输出: True 解释: 我们可以通过以下几步将start转换成end: RXXLRXRXL -> XRXLRXRXL -> XRLXRXRXL -> XRLXXRRXL -> XRLXXRRLX 注意:
Runtime: 24 ms
Memory Usage: 19.5 MB
1 class Solution { 2 func canTransform(_ start: String, _ end: String) -> Bool { 3 var start = Array(start) 4 var end = Array(end) 5 var n:Int = start.count 6 var cntL:Int = 0 7 var cntR:Int = 0 8 for i in 0..<n 9 { 10 if start[i] == "R" {cntR += 1} 11 if end[i] == "L" {cntL += 1} 12 if start[i] == "L" {cntL -= 1} 13 if end[i] == "R" {cntR -= 1} 14 if cntL < 0 || cntR < 0 || cntL * cntR != 0 15 { 16 return false 17 } 18 } 19 return cntL == 0 && cntR == 0 20 } 21 } 68ms 1 class Solution { 2 func canTransform(_ start: String, _ end: String) -> Bool { 3 let cs = Array(start) 4 let ce = Array(end) 5 let ts = String(cs.filter{ $0 != "X" } ) 6 let te = String(ce.filter{ $0 != "X" } ) 7 8 guard ts == te else { return false } 9 10 var sr = [Int]() 11 var sl = [Int]() 12 13 for (i, char) in cs.enumerated() { 14 if char == "R" { 15 sr.append(i) 16 } else if char == "L" { 17 sl.append(i) 18 } 19 } 20 21 var rCount = 0 22 var lCount = 0 23 24 for (i, char) in ce.enumerated() { 25 if char == "R" { 26 if sr[rCount] > i { 27 return false 28 } else { 29 rCount += 1 30 } 31 } else if char == "L" { 32 if sl[lCount] < i { 33 return false 34 } else { 35 lCount += 1 36 } 37 } 38 } 39 40 return true 41 } 42 } 96ms 1 class Solution { 2 func compare(_ start: String, _ end: String) -> Bool { 3 var rs1 = 0 4 var ls1 = 0 5 var xs1 = 0 6 var rs2 = 0 7 var ls2 = 0 8 var xs2 = 0 9 for i in start.indices { 10 switch start[i] { 11 case "R": 12 rs1 += 1 13 case "L": 14 ls1 += 1 15 case "X": 16 xs1 += 1 17 default: 18 break 19 } 20 switch end[i] { 21 case "R": 22 rs2 += 1 23 case "L": 24 ls2 += 1 25 case "X": 26 xs2 += 1 27 default: 28 break 29 } 30 } 31 return rs1 == rs2 && ls1 == ls2 && xs1 == xs2 32 } 33 34 func canTransform(_ start: String, _ end: String) -> Bool { 35 var rs = [String.Index]() 36 var ls = [String.Index]() 37 for (i, ch) in zip(start.indices, start) { 38 if ch == "R" { 39 rs.append(i) 40 } else if ch == "L" { 41 ls.append(i) 42 } 43 } 44 rs.reverse() 45 ls.reverse() 46 47 if !compare(start, end) { 48 return false 49 } 50 51 for (i, ch) in zip(end.indices, end) { 52 if ch == "R" { 53 if let nextRIdx = rs.last, nextRIdx <= i { 54 if let nextLIdx = ls.last { 55 if nextLIdx < nextRIdx { 56 // impossible to move R through L 57 return false 58 } else { 59 rs.removeLast() 60 } 61 } else { 62 rs.removeLast() 63 } 64 } else { 65 return false 66 } 67 } else if ch == "L" { 68 if let nextLIdx = ls.last, nextLIdx >= i { 69 if let nextRIdx = rs.last { 70 if nextRIdx < nextLIdx { 71 // impossible to move L through R 72 return false 73 } else { 74 ls.removeLast() 75 } 76 } else { 77 ls.removeLast() 78 } 79 } else { 80 return false 81 } 82 } 83 } 84 return true 85 } 86 } 104ms 1 class Solution { 2 func canTransform(_ start: String, _ end: String) -> Bool { 3 guard start.count >= 1 && start.count <= 10000 && end.count >= 1 && end.count <= 10000 else { 4 return false 5 } 6 let startChars = Array(start), endChars = Array(end) 7 var startInfos = [(Character, Int)](), endInfos = [(Character, Int)]() 8 for (index, char) in startChars.enumerated() { 9 if char == "L" || char == "R" { 10 startInfos.append((char, index)) 11 } else if char != "X" { 12 return false 13 } 14 } 15 for (index, char) in endChars.enumerated() { 16 if char == "L" || char == "R" { 17 endInfos.append((char, index)) 18 } else if char != "X" { 19 return false 20 } 21 } 22 let startInfosChars = startInfos.map { 23 $0.0 24 } 25 let endInfosChars = endInfos.map { 26 $0.0 27 } 28 guard startInfosChars == endInfosChars else { 29 return false 30 } 31 for (index, startInfo) in startInfos.enumerated() { 32 let endInfo = endInfos[index] 33 if startInfo.0 == "L" { 34 guard startInfo.1 >= endInfo.1 else { 35 return false 36 } 37 } else { 38 guard startInfo.1 <= endInfo.1 else { 39 return false 40 } 41 } 42 } 43 return true 44 } 45 }
|
请发表评论