830. Positions of Large Groups@python
In a string S
of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like S = "abbxxxxzyy"
has the groups "a"
, "bb"
, "xxxx"
, "z"
and "yy"
.
Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group.
The final answer should be in lexicographic order.
Example:
Input: "abcdddeeeeaabbbcd"
Output: [[3,5],[6,9],[12,14]]
原题地址: Positions of Large Groups
题意: 将数组分组,标记每一组的首尾索引值,返回分组长度等于3的索引值
难度: Easy
思路:
(1)遍历数组并用count数组标记,注意首尾边界
(2)遍历count,找出长度大于等于3的分组
代码:
class Solution(object):
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
count = []
i = 0
j = 1 while j <= len(S):
if j == len(S):
count.append([i, j-1])
break if S[i] == S[j]:
j += 1
else:
count.append([i, j-1])
i = j res = []
for c in count:
if c[1] - c[0] + 1 >= 3:
res.append(c)
return res
时间复杂度: O(n)
空间复杂度: O(n)
在上面的基础上优化一下,一次变量即可
代码:
class Solution(object):
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
res = []
i, j = 0, 1
n = len(S)
while j < n:
if S[j] != S[j-1]: # 找到不相等的值
count = j - i
if count >= 3:
res.append([i, j-1])
i = j
j += 1 if j - i >= 3: # 处理边界问题
res.append([i, j-1]) return res
830. Positions of Large Groups@python的更多相关文章
- 【Leetcode_easy】830. Positions of Large Groups
problem 830. Positions of Large Groups solution1: class Solution { public: vector<vector<int&g ...
- 830. Positions of Large Groups - LeetCode
Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeee ...
- [LeetCode&Python] Problem 830. Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- 【LeetCode】830. Positions of Large Groups 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 830. Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- Positions of Large Groups
Positions of Large Groups In a string S of lowercase letters, these letters form consecutive groups ...
- [LeetCode] Positions of Large Groups 大群组的位置
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- [LeetCode] 830. Positions of Large Groups_Easy tag: Two Pointers
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
随机推荐
- [Xcode 实际操作]七、文件与数据-(16)解析XML文档
目录:[Swift]Xcode实际操作 本文将演示如何解析XML文档. 项目中已添加一份XML文档:worker.xml <?xml version="1.0" encodi ...
- Tcl/Tk语言学习------拆分字符串
字符串的拆分 前言 字符串的处理是每种语言经常会遇到的问题,tcl作为一门脚本语言自然也不例外,关于字符串的拆分,一般会有两种情况:1.使用单个字符拆分,2.使用字符串拆分. 1.使用单个字符拆分字符 ...
- APP上传
原文网址: http://blog.csdn.net/ayangcool 前言:作为一名IOS开发者,把开发出来的App上传到App Store是必须的.下面就来详细介绍下具体流程. 1.打开苹果开发 ...
- 第二篇 HTML5打包发布IOS APP之苹果开发者账号申请流程
打包技术转移到了公众号
- Zynq7000开发系列-7(在Zybo上运行Linaro桌面系统)
目标板:Zybo(7Z010) 主机操作系统:Ubuntu 14.04.5 LTS 64bit 交叉编译链: arm-xilinx-linux-gnueabi- [gcc version ...
- scrapy框架中Spiders用法
scrapy框架中Spiders用法 Spider类定义了如何爬去某个网站,包括爬取的动作以及如何从网页内容中提取结构化的数据 总的来说spider就是定义爬取的动作以及分析某个网页 工作流程分析 以 ...
- [洛谷P2186] 小Z的栈函数
题目链接: 传送门 题目分析: 大模拟,先得存操作,然后再处理每个数-- 有一个小优化,在处理操作的时候顺便判一下最后栈里是不是有且仅有一个数,但A完了才想起来,所以就算了-- 总之就是个模拟题--没 ...
- bzoj 5449 序列
https://www.lydsy.com/JudgeOnline/problem.php?id=5449 话说很早以前做过..算是IDA*的板子吧,一个简单的估价函数就可以过去了 %:pragma ...
- getpass不起作用
#! /usr/bin/env python# -*- coding:utf-8 -*- # login 模块中登录时输入密码,想用getPass模块实现密码的不回显操作.#如下: import ge ...
- php 分析2
a:link,a:visited,a:hover,a:active 1:解释 link:连接平常的状态 visited:连接被访问过之后 hover:鼠标放到连接上的时候 active:连接被按下 ...