1.原題:
https://leetcode.com/problems/split-a-string-in-balanced-strings/
Split a String in Balanced Strings:
Balanced strings are those who have equal quantity of 'L' and 'R' characters.
Given a balanced string s split it in the maximum amount of balanced strings.
Return the maximum amount of splitted balanced strings.
翻譯:
對稱的欄位意思就是: LLRR 或者 LR 這種,現在給定一個字串,然后查找里面有幾個對稱的欄位,
2.解題思路:
這道題其實非常直白,你只需要用for loop去對照數量就行,因為字串里面是由好幾對對稱的欄位組成,所以你只需要記錄每一次L和R數量一樣的時刻就行了,因為這時候肯定是一個新的對稱,
這題我因為偷懶就用了python2,實際上語法任何一個語言都行:
class Solution(object):
def balancedStringSplit(self, s):
count_tmp1 = count_tmp2 = count_tmp3 = 0
for S in s: #檢索整個字串
if (S == "R"):
count_tmp1 += 1 #R的數量
else:
count_tmp2 += 1 #L的數量
if (count_tmp1 == count_tmp2): #當兩者數量一致的時候,說明有一個新的對稱
count_tmp3 += 1
return count_tmp3
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/43436.html
標籤:其他
上一篇:2019個人總結
下一篇:不念過去,不畏將來
