網(wǎng)站首頁 編程語言 正文
常用 ASCII 碼表對照表:
注意如下幾點:
0-9:48-57A-Z:65-90a-z:97-122
ord()函數(shù)介紹:
?ord() 函數(shù)是 chr() 函數(shù)(對于 8 位的 ASCII 字符串)的配對函數(shù),它以一個字符串(Unicode 字符)作為參數(shù),返回對應的 ASCII 數(shù)值,或者 Unicode 數(shù)值。
>>> ord('0')
48
>>> ord('A')
65
>>> ord('a')
97
?應用實例:
ord()函數(shù)的一個應用場景就是,利用哈希表解決字母異位詞問題。
利用ord()函數(shù)求解每個字母的ASCII數(shù)值,再利用每個字母和字母a之間的差值,將26個小寫英文字母映射到下標分別為0-25的數(shù)組上,數(shù)組中存放的是每個字母的數(shù)目。
例如:
class Solution(object):
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
result = []
record_s = [0]*26
record_p = [0]*26
if len(s) < len(p):
return result
for i in range(len(p)):
record_s[ord(s[i])-ord('a')] += 1
record_p[ord(p[i])-ord('a')] += 1
if record_s == record_p:
result.append(0)
for i in range(len(s)-len(p)):
record_s[ord(s[i])-ord('a')] -= 1
record_s[ord(s[i+len(p)])-ord('a')] += 1
if record_s == record_p:
result.append(i+1)
return result
原文鏈接:https://blog.csdn.net/m0_46483236/article/details/125629679
相關(guān)推薦
- 2022-07-08 Pytest如何使用mark的方法_python
- 2022-12-15 Pycharm中配置Anaconda解釋器的完整步驟_python
- 2022-03-16 C++訪問者模式模板函數(shù)無法重載的問題解決_C 語言
- 2022-03-14 Springboot讀取外部配置文件,項目部署時配置讀取不到問題
- 2022-10-04 go常用指令之go?mod詳解_Golang
- 2022-03-16 使用Lvs+Nginx集群搭建高并發(fā)架構(gòu)的實現(xiàn)示例_nginx
- 2022-02-03 ionic錨點操作
- 2022-12-09 pygame畫點線方法詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支