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 ...
随机推荐
- 主席树 模板题 luogu([POI2014]KUR-Couriers)
求区间内是否有个数大于二分之一的数,有的话输出这个数,没有的话输出0. 在询问的时候,如果左边有sum大于这个limit,就可以继续求,如果右边有sum大于limit 也递归, 如果都不行,返回 0 ...
- 概率DP lightoj 1265
题意: 1.两只老虎相遇 就互相残杀 2.老虎与鹿相遇 鹿死 3.老虎与人相遇 人死 4.人与鹿相遇 鹿死 5.鹿与鹿相遇 无果 求人活的概率 解析:如果老虎为0 则人活得概率为1 ...
- FromBase64String 输入的不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符,或者填充字符间包含非法字符
js前台: <input id="upload_img_input" v-on:change="onFileChange" type="file ...
- Atcoder Beginner Contest 147C(状态压缩)
最多15个人,用N个二进制的数字表示每个人的状态,然后检验.这串数字相当于已经把这些人的状态定了下来,如果和输入的情况不符则这串数字不正确,直接忽略,因为枚举了所有的情况,所以总有正确的,不必在错误的 ...
- Linux shell cut 命令详解
详细资料请参考:博客园骏马金龙 https://www.cnblogs.com/f-ck-need-u/p/7521357.html cut命令将行按指定的分隔符分割成多列,它的弱点在于不好处理多个分 ...
- xampp 配置多域名工程 解决 Access forbidden!
XAMPP虚拟主机配置,多域名绑定访问本地站点 XAMPP有时候你需要一些顶级域名访问方式来访问你本地的项目也就是虚拟主机配置,这时候就需要配置虚拟主机,给你的目录绑定一个域名,实现多域名绑定访问. ...
- MySQL数学函数简明总结
1. ABS(x): 返回x的绝对值 mysql> select ABS(1), ABS(-1), ABS(0); +--------+---------+--------+| ABS(1) | ...
- 安卓开发:图片的显示Mode
安卓开发中将图片放置在ImageView中展示,涉及到图片显示的模式,跟iOS开发中UIView的contentMode属性是一个意思,在安卓UI元素中使用的属性是scaleType,其中枚举值的效果 ...
- socket模块(套接字模块)
socket模块(套接字模块) 一.最简单版本(互传一次就结束) # 客户端 import socket client = socket.socket() client.connect(('127.0 ...
- 关于 appium get_attribute 方法的坑
(得要学着看源码) 问题: self.driver.find_element_by_id("id").get_attribute("content-desc") ...