leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
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
leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段的更多相关文章
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路 ...
- leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...
- leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST
1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...
- leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero
1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
随机推荐
- XSS 2
通过第一题之后继续进行第二题 我们会发现这个体会将内容放到<textarea></textarea>中然后我们刚才那段代码就失效了 因为这个代码可以将我们输入的内容转换成超文本 ...
- Go流程结构(for)
一.程序的流程结构 程序的流程控制结构一共有三种:顺序结构,选择结构,循环结构. 顺序结构:代码从上向下逐行的执行 选择结构:条件满足,某些代码才会被执行.0-1次 if语句,switch语句 循环结 ...
- 定义列属性:null,default,PK,auto_increment
*定义列属性* 1.列属性NULL: 是否为空,规定一个字段的值是否可以是null: null|not null 2.默认值属性:default value 默认值,在没有为该字段设置值时启用:而且 ...
- 【C语言】(指针) 将两个数排序
原理就不讲了,这里用来理解指针的使用方法 代码1: #include <stdio.h> int main() { int a, b;/* 实际变量的声明 */ int* p, * q;/ ...
- webRTC中回声消除(AEC)模块编译时aec_rdft.c文件报错:
webRTC中回声消除(AEC)模块编译时aec_rdft.c文件报错. 原因是: 局部变量ip跟全局变量冲突的问题,可以将局部变量重新命名一下,就可以通过编译了. aec_rdft.c修改以后文件代 ...
- ie8无法拉伸背景图
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Plastic Bottle Manufacturer: Characteristic Analysis Of Plastic Packaging Bottles
Plastic packaging bottles are usually made of 7 materials. Due to its inherent characteristics, the ...
- PAT 1015 Reversible Primes (判断素数)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- collections模块、时间模块、random模块、os模块、sys模块、序列化模块、subprocess模块
一.collections模块 1.其他数据类型 在内置数据类型(str.dict.list.tuple.set)的基础上,collections模块还提供了了几个额外的数据类型:Counter.de ...
- css 中 max-width 和 min-width 的区别
max-width:规定元素本身最大宽度,即元素本身 (该div) 的宽度应小于等于其最大宽度值. min-width:规定元素本身最小宽度,即元素本身应大于等于其宽度值. 例:min-width:1 ...