344. Reverse String

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
return s[::-1]
# easy

541. Reverse String II

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.

Example:

Input: s = "abcdefg", k = 2
Output: "bacdfeg"

Restrictions:

  1. The string consists of lower English letters only.
  2. Length of the given string and k will in the range [1, 10000]
class Solution(object):
def reverseStr(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
n_s = ""
i = 0
while(i<len(s)):
n_s = n_s + s[i:i+k][::-1] + s[i+k:i+2*k]
i+=2*k
return n_s # range(length) or range(start,end,step), while range(length,step) does not exist

657. Judge Route Circle

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Example 1:

Input: "UD"
Output: true

Example 2:

Input: "LL"
Output: false
class Solution(object):
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
moveDic = {'L':0,'R':0,'U':0,'D':0}
for i in moves:
moveDic[i] += 1
if moveDic['L']==moveDic['R'] and moveDic['U']==moveDic['D']:
return True
else:
return False

696. Count Binary Substrings

Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively.

Substrings that occur multiple times are counted the number of times they occur.

Example 1:

Input: "00110011"
Output: 6
Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01".

Notice that some of these substrings repeat and are counted the number of times they occur.

Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.

Example 2:

Input: "10101"
Output: 4
Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.

Note:

  • s.length will be between 1 and 50,000.
  • s will only consist of "0" or "1" characters.
class Solution(object):
def countBinarySubstrings(self, s):
"""
:type s: str
:rtype: int
"""
counts = map(len, s.replace('01','0 1').replace('10','1 0').split())
return sum(map(min, zip(counts, counts[1:]))) # briliant idea
# string.replace('','')
# zip() can zip elements of two iterable into a list of tuple elementwisely

最新文章

  1. CentOS6.3 编译安装LAMP(3):编译安装 MySQL5.5.25
  2. 处理字符串-String类和正则表达式
  3. Updatepanel 注册javascript 方法
  4. Caffe学习系列(18): 绘制网络模型
  5. eclipse编译异常修正:the project cannot be built until its prerequisite...
  6. firefox 自定义快捷键
  7. SSL构建单双向https认证
  8. jq选取对象的方法
  9. Nhibernate 智能提示 以及其他类库智能提示
  10. mysql导出数据到excel表中
  11. HostOnly模式下的Centos克隆虚拟机+配置固定ip
  12. bootstrap 响应式图片自适应图片大小
  13. Socket编程实践(10) --select的限制与poll的使用
  14. 学习笔记——二叉树相关算法的实现(Java语言版)
  15. vue使用element-ui的el-input监听不了回车事件解决
  16. JAVA入门教程 - idea 新建maven spring MVC项目
  17. Webform中&lt;%%&gt;
  18. Groovy动态解析
  19. 初识MQ
  20. JS设置cookie、读取cookie、删除cookie(转载)

热门文章

  1. linux服务器免密钥登录
  2. apply()技巧
  3. python剑指offer 合并两个排序的链表
  4. PHP implode() 函数
  5. C#箴言之用属性来访问类的私有成员
  6. 快速下载jar包
  7. abs的个人博客 http://abs001.top/blog
  8. 笔试算法题(46):简介 - 二叉堆 &amp; 二项树 &amp; 二项堆 &amp; 斐波那契堆
  9. nginx安装php环境
  10. 详解 JS 中 new 调用函数原理