当前位置:网站首页>String title parsing
String title parsing
2022-08-08 05:24:00 【friedrichor】
String title parsing
Strings are generally considered immutable,So if you simply examine strings,The knowledge points that can be involved are relatively small.
Almost all string questions are done through the following categories,可以划分如下:
备注:The code provided below is python 代码.
数组
由于字符串是有字符组成的,而算法题中的字符串一般都只会包含26个英文字母,更为简单的一般会告诉你仅包含大/小写字母,那么在判断字符串中包含的英文字母数量时,可以使用一个长度为26的数组根据下标 0-25 的value值 对应 a-z 个字符在字符串中出现的次数.
通过这种方式可以解决很大一部分字符串的题目.
Count the number of occurrences of each letter can be used arr = [0] * 26 进行初始化,Then each item has to represent the number of times each word appears.Here I think it can also be used directly Counter 进行统计,But it will take longer,Mainly it is convenient to call,使用方法可以参考 python Counter() 函数.
栈
针对括号匹配、路径拆分等字符串题目,使用栈的方式简直手到擒来闭眼AC.Of course it's explained herePython中,是没有Stack这个数据类型的,它是通过list的append和popPush and pop the stack.有关python中 list 的操作可以参考 Python列表(list)related operations and methods
下面列出list的和pop用法:
移除列表中Specify the subscript的元素(默认移除最后一个元素),并返回移除的数据.
list1 = [1, [2, 4, 8], 0, 2, 3]
print(list1.pop()) # 3
print(list1) # [1, [2, 4, 8], 0, 2]
print(list1.pop(2)) # 0
print(list1) # [1, [2, 4, 8], 2, 3]
哈希表
哈希表在字符串中也是有一定地位的,我们在解题时可以使用哈希表快速完成一些匹配的关系,比如罗马数字转整数中的循环比较:
tmp = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000}
亦或者是判断字符串同构等的题目,也可以通过哈希表的方式轻松解题.
队列
队列由于其先进先出的特点,可以在循环匹配中帮助我们通过枚举的方式完成部分字符串的题目.
字符串方法
上面看到由于我们需要将字符串转化为其他的数据类型,那么不可避免的需要用到一些方法.A quick introduction here:
获取字符串长度
len(some_string)
判断字符串是否相等
some_string == other_string
获取字符串的子串
some_string [begin: end: step]
拆分字符串
some_string.split(regex,maxsplit)
获取下标
python 只有index()
大小写转换
lower() upper()
改自:刷穿剑指offer-Day08-字符串I-014.字符串中的变位词 String knowledge point summary and question type explanation!
边栏推荐
猜你喜欢
随机推荐
0字典树/字符串中等 LeetCode676. 实现一个魔法字典
Personal Summary of OLTP and OLAP Issues
VSCode已经设置过为中文但变成英文的解决办法
MYSQL export data dictionary
类似Bugfree的9大在线缺陷管理软件
11-golang流程控制
gcc/g++使用
2022-08-07 mysql/stonedb slow SQL-subquery-semi-join
Redis set to start automatically at boot
使用ffmpeg解码音频sdl(push)播放
MySQL - Indexes and Transactions
Week 8 Transformer Language Models and Implications
vulnhub-DC-3 drone penetration record
TSF微服务治理实战系列(二)——服务路由
牛客多校第6场赛后学习 B(两种做法)G(两种做法)M(两种写法)J
14.Unity2D 横版 粒子系统特效 飙血粒子+高处落地粒子+对象池管理所有粒子
sqlmap+dnslog注入复现
Unity-CharacterController(角色控制器)
力扣84 双周赛 t4 6144 和力扣305周赛t4 6138
reduce具体使用以及使用reduce,toString,flat进行数组降维








