[Swift]LeetCode838. 推多米諾 | Push Dominoes

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-klnjzdbn-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

There are N dominoes in a line, and we place each domino vertically upright.git

In the beginning, we simultaneously push some of the dominoes either to the left or to the right.github

After each second, each domino that is falling to the left pushes the adjacent domino on the left.微信

Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.dom

When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.ide

For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.this

Given a string "S" representing the initial state. S[i] = 'L', if the i-th domino has been pushed to the left; S[i] = 'R', if the i-th domino has been pushed to the right; S[i] = '.', if the i-th domino has not been pushed.spa

Return a string representing the final state. code

Example 1:htm

Input: ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."

Example 2:

Input: "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.

Note:

  1. 0 <= N <= 10^5
  2. String dominoes contains only 'L', 'R' and '.'

一行中有 N 張多米諾骨牌,咱們將每張多米諾骨牌垂直豎立。

在開始時,咱們同時把一些多米諾骨牌向左或向右推。

每過一秒,倒向左邊的多米諾骨牌會推進其左側相鄰的多米諾骨牌。

一樣地,倒向右邊的多米諾骨牌也會推進豎立在其右側的相鄰多米諾骨牌。

若是同時有多米諾骨牌落在一張垂直豎立的多米諾骨牌的兩邊,因爲受力平衡, 該骨牌仍然保持不變。

就這個問題而言,咱們會認爲正在降低的多米諾骨牌不會對其它正在降低或已經降低的多米諾骨牌施加額外的力。

給定表示初始狀態的字符串 "S" 。若是第 i 張多米諾骨牌被推向左邊,則 S[i] = 'L';若是第 i 張多米諾骨牌被推向右邊,則 S[i] = 'R';若是第 i 張多米諾骨牌沒有被推進,則 S[i] = '.'

返回表示最終狀態的字符串。

示例 1:

輸入:".L.R...LR..L.."
輸出:"LL.RR.LLRRLL.."

示例 2:

輸入:"RR.L"
輸出:"RR.L"
說明:第一張多米諾骨牌沒有給第二張施加額外的力。

提示:

  1. 0 <= N <= 10^5
  2. 表示多米諾骨牌狀態的字符串只含有 'L''R'; 以及 '.';

Runtime: 284 ms
Memory Usage: 20.8 MB
 1 class Solution {
 2     func pushDominoes(_ dominoes: String) -> String {
 3         var n:Int = dominoes.count
 4         var arrChar = Array(dominoes)
 5         var cnt:[Int] = [Int](repeating:0,count:n)
 6         for i in 1..<n
 7         {
 8             if arrChar[i - 1] == "R" && arrChar[i] == "."
 9             {
10                 arrChar[i] = "R"
11                 cnt[i] = cnt[i - 1] + 1
12             }
13         }
14         var cur:Int = 0
15         for i in stride(from:n - 2,through:0,by:-1)
16         {
17             if arrChar[i + 1] != "L" {continue}
18             cur = cnt[i + 1] + 1
19             if arrChar[i] == "." || cnt[i] > cur
20             {
21                 arrChar[i] = "L"
22                 cnt[i] = cur
23             }
24             else if arrChar[i] == "R" && cnt[i] == cur
25             {
26                  arrChar[i] = "."
27             }            
28         }
29         return String(arrChar)
30     }
31 }

308ms

  1 class Solution {
  2     func pushDominoes(_ dominoes: String) -> String {
  3     guard dominoes.count > 1 else{
  4         return dominoes
  5     }
  6     var dominoes = Array(dominoes)
  7     var startIndex = 0
  8     var movingIndex = 0
  9 
 10     while movingIndex < dominoes.count{
 11         if dominoes[startIndex] == "."{
 12             if dominoes[movingIndex] == "."{
 13                 movingIndex += 1
 14             }else if dominoes[movingIndex] == "L"{
 15                 pushLeft(dominoes: &dominoes, startIndex: startIndex, endIndex: movingIndex)
 16                 movingIndex += 1
 17                 startIndex = movingIndex
 18             }else{
 19                 // if dominoes[movingIndex] = right
 20                 startIndex = movingIndex
 21             }
 22         }else if dominoes[startIndex] == "L"{
 23             if dominoes[movingIndex] == "L"{
 24                 if startIndex != movingIndex{
 25                     pushLeft(dominoes: &dominoes, startIndex: startIndex, endIndex: movingIndex)
 26                     startIndex = movingIndex
 27                 }
 28                 movingIndex += 1
 29             }else if dominoes[movingIndex] == "."{
 30                 movingIndex += 1
 31             }else{
 32                 // now we R
 33                 startIndex = movingIndex
 34             }
 35         }else{
 36             // if dominoes[startIndex] = R
 37             // then what do we do
 38             if dominoes[movingIndex] == "R"{
 39                 if startIndex != movingIndex{
 40                     pushRight(dominoes: &dominoes, startIndex: startIndex, endIndex: movingIndex)
 41                     startIndex = movingIndex
 42                 }
 43                 movingIndex += 1
 44             }else if dominoes[movingIndex] == "."{
 45                 movingIndex += 1
 46             }else{
 47                 // we meet L
 48                 pushRightLeft(dominoes: &dominoes, startIndex: startIndex, endIndex: movingIndex)
 49                 movingIndex += 1
 50                 startIndex = movingIndex
 51             }
 52         }
 53     }
 54     
 55     if startIndex < movingIndex && dominoes[startIndex] == "R"{
 56         while startIndex < movingIndex{
 57             dominoes[startIndex] = "R"
 58             startIndex += 1
 59         }
 60     }
 61     
 62   
 63     return String(dominoes)
 64 }
 65 func pushLeft(dominoes : inout [Character], startIndex : Int, endIndex : Int){
 66     guard startIndex >= 0 && endIndex < dominoes.count && startIndex < endIndex else{
 67         fatalError("Index Not Correct")
 68     }
 69     var startIndex = startIndex
 70     while startIndex < endIndex{
 71         dominoes[startIndex] = "L"
 72         startIndex += 1
 73     }
 74 }
 75 
 76 func pushRight(dominoes : inout [Character], startIndex : Int, endIndex : Int){
 77     guard startIndex >= 0 && endIndex < dominoes.count && startIndex < endIndex else{
 78         fatalError("Index Not Correct")
 79     }
 80     var startIndex = startIndex
 81     while startIndex < endIndex{
 82         dominoes[startIndex] = "R"
 83         startIndex += 1
 84     }
 85 }
 86 
 87 func pushRightLeft(dominoes : inout [Character], startIndex : Int, endIndex : Int){
 88     guard startIndex >= 0 && endIndex < dominoes.count && startIndex < endIndex else{
 89         fatalError("Index Not Correct")
 90     }
 91     
 92     var startIndex = startIndex
 93     var endIndex = endIndex
 94     while startIndex < endIndex{
 95         dominoes[startIndex] = "R"
 96         dominoes[endIndex] = "L"
 97         startIndex += 1
 98         endIndex -= 1
 99     }
100   }
101 }
相關文章
相關標籤/搜索