網站首頁 編程語言 正文
常用 ASCII 碼表對照表:
注意如下幾點:
0-9:48-57A-Z:65-90a-z:97-122
ord()函數介紹:
?ord() 函數是 chr() 函數(對于 8 位的 ASCII 字符串)的配對函數,它以一個字符串(Unicode 字符)作為參數,返回對應的 ASCII 數值,或者 Unicode 數值。
>>> ord('0')
48
>>> ord('A')
65
>>> ord('a')
97
?應用實例:
ord()函數的一個應用場景就是,利用哈希表解決字母異位詞問題。
利用ord()函數求解每個字母的ASCII數值,再利用每個字母和字母a之間的差值,將26個小寫英文字母映射到下標分別為0-25的數組上,數組中存放的是每個字母的數目。
例如:
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
相關推薦
- 2022-01-16 jQuery 核心函數css和平滑滾動頂部
- 2022-04-22 redis 為什么對數字/字符串append操作后,編碼格式object encoding從int/
- 2022-07-30 如何重置CentOS 7的Root密碼?
- 2022-08-07 pandas中pd.groupby()的用法詳解_python
- 2023-04-02 Android自定義View實現動畫效果詳解_Android
- 2022-12-09 Go?Web實戰之創建項目及增加日志功能_Golang
- 2022-05-13 error hawk@0.10.2: The engine “node“ is incompatib
- 2022-04-25 基于Matplotlib?調用?pyplot?模塊中?figure()?函數處理?figure圖形對
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支