python数据结构之字符串查找两例
来源:互联网 发布:mib browser for mac 编辑:程序博客网 时间:2024/06/05 05:34
查找字符串中最长连续数字子串
问题描述
查找给定字符串中最长的数字字串,返回其起始下标,长度和字串.例如:
input :abc12345cd123ef234567df
output:15 6 234567
实现
'''查找给定字符串中最长的数字字串,返回其起始下标,长度和字串.例如:input :abc12345cd123ef234567dfoutput:15 6 234567'''def find_max_length_str(string): str_length = len(string) i = 0 max_length = 0 num_length = 0 start_num = 0 while i < str_length: if string[i] > '0' and string[i] < '9': start_num = i num_length = 0 while i < str_length and string[i] > '0' and string[i] < '9': i += 1 num_length += 1 if num_length != 0 and max_length <= num_length: max_length = num_length i += 1 return start_num, num_length, string[start_num:start_num + num_length]
只需遍历一遍字符串,时间复杂度:O(n)
查找数字字符串中最长连续相同数字字串
问题描述
查找给定数字串中最长连续相同字串,返回其起始下标,长度和子串.例如
input:11233344555666666
output:11 6 666666
output:11 6 666666
实现
'''找到数字串中最长连续字串,返回其起始下标,长度和子串input :11233344555666666output:11 6 666666'''def find_same_sequence_num(string): str_length = len(string) i = 0 max_length = 0 start_num = 0 num_length = 0 while i < str_length: if i + 1 < str_length and string[i] == string[i + 1]: start_num = i num_length = 1 while i + 1 < str_length and string[i] == string[i + 1]: i += 1 num_length += 1 if num_length != 0 and max_length <= num_length: max_length = num_length i += 1 return start_num, num_length, string[start_num:start_num + num_length]
同样只需遍历一遍字符串,时间复杂度为O(n)
1 0
- python数据结构之字符串查找两例
- python数据结构之字符串
- python数据结构之字符串
- python 数据结构二 之 字符串
- Python字符串查找算法之BMHBNFS算法
- 【数据结构】字符串查找
- 数据结构-字符串查找
- 数据结构-查找字符串1
- 数据结构-查找字符串2
- 数据结构-查找字符串
- 数据结构示例之查找子字符串的起始位置
- 数据结构 -- 查找之 二分法查找
- python 查找字符串
- python字符串查找
- Python 字符串查找数字
- Python字符串查找
- Python 字符串查找
- python字符串查找
- asp.net MVC _LayOut布局以及用户控件的使用
- Eclipse输入时自动提示
- OpenGL笔记(一)
- Git详解之四 服务器上的Git
- 串口流控 软件流控与硬件流控
- python数据结构之字符串查找两例
- 前端js干货
- 289. Game of Life
- maven web 项目中启动报错java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener
- Objective-C与Runtime的那点事儿消息机制
- HDU5380
- python用BeautifulSoup抓取知乎小药丸
- 即将正式关闭MSN中文网
- CSS