日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

python中內(nèi)置函數(shù)ord()返回字符串的ASCII數(shù)值實例詳解_python

作者:m0_46483236 ? 更新時間: 2022-08-29 編程語言

常用 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

欄目分類
最近更新