SlideShare una empresa de Scribd logo
1 de 18
LeetCode 477.
Total Hamming Distance
F110118101 陳佑昇
2022/12/19
Content
Goal description
My solution
Optimization
Summary
ERROR DETECTION & CORRECTION
/15
2
「錯誤偵測」。判斷資料是否損毀變異。
例如網路傳輸,如果發現出錯了,麻煩對方馬上
重傳一次就好。身份證字號,最後一碼是驗證碼,
一方面避免卡號連續、一方面避免行政人員輸入
錯誤;如果輸入錯誤,重新輸入就好。
R. W. Hamming, "Error detecting and error correcting
codes," in The Bell System Technical Journal, vol. 29, no.
2, pp. 147-160, April 1950, doi: 10.1002/j.1538-
7305.1950.tb00463.x.
Primary goal
Hamming distance
兩個字符串對應位置的不同字符的
個數。換句話說,它就是將一個字
符串變換成另外一個字符串所需要
替換的字符個數。
/15
4
Goal description
/15
5
Input: nums = [4,14,2]
Output: 6
Explanation: In binary
representation, the 4 is 0100, 14 is
1110, and 2 is 0010
The answer will be:
HammingDistance
(4, 14) + HammingDistance(4, 2) +
HammingDistance(14, 2) = 2 + 2 +
2 = 6.
My solution
/15
6
from itertools import combinations
nums = [4,14,2,1]
maxLen = 0 #存最長字串(2進制)長度
count = 0 #儲存差異字數
print("所有組合為:" + str(list(combinations(nums,2))))
print("--------")
My solution
/15
7
#儲存最長的數字距離
for item in nums:
if len(bin(item)) > maxLen:
maxLen = len(bin(item))
#print(bin(item))
maxLen = maxLen - 2 #減掉“0b” 兩個字元
My solution
/15
8
#計算漢明距離
for item in list(combinations(nums,2)):
#整數轉2進制
a1 = str(bin(item[0]))[2:].zfill(maxLen)
a2 = str(bin(item[1]))[2:].zfill(maxLen)
for i in range(maxLen):
if a1[i] != a2[i]:
count+=1
print(count)
My solution
/15
9
#計算漢明距離
for item in list(combinations(nums,2)):
#整數轉2進制
a1 = str(bin(item[0]))[2:].zfill(maxLen)
a2 = str(bin(item[1]))[2:].zfill(maxLen)
for i in range(maxLen):
if a1[i] != a2[i]:
count+=1
print(count)
My solution result
/15
10
兩兩一組求 Hamming Distance
最後再加總
• 測試到 27/46會超時錯誤(1000個數字)
• 共需要算 𝑛
2
== n∗(n−1)2 次
= 999000/2 = 499,500次
Optimization
/15
11
最後一列,有三個0和一個1,那麼它們之間相
互的漢明距離就是3,即1和其他三個0分別的
距離累加
第三列,累加漢明距離為4,因為每個1都會跟
兩個0產生兩個漢明距離,同理第二列也是4,
第一列是3
累計漢明距離和0跟1的個數(依照位元)就是答
案,可以發現其實也是0個數乘以1個數的加總
3+ 4+ 4+ 3
=14
My solution (optimization)
/15
12
total = 0
#計算漢明距離
for i in range(32):
count = 0 #儲存差異字數
for item in nums:
#判斷位元數1的
if(str(bin(item))[2:].zfill(32)[i] == "1"):
count = count + 1
total = total + (count* (len(nums)-count)) #加總 0乘以1的個
數
print(total)
My solution result (optimization)
/15
13
My solution result (optimization)
/15
14
Summary
• 憑直覺使用組合方式,找Cn取2的暴力窮舉
法不可取,LeetCode會報超時錯誤無法完成
題目,因此要運用按位統計法進行
• 時間複雜度O(n2) ➜ O(n)
• 改良成大神解法,可減少約1秒的執行時間,
空間複雜度可達O(1)
return sum(x*(len(nums)-x) for x in
[b.count('0’)
for b in zip(*map('{:032b}'.format, nums))])
/15
15
Thank you
參考資料
LeetCode 477. Total Hamming Distance
https://leetcode.com/problems/total-hamming-
distance/description/
Error Detection https://web.ntnu.edu.tw/~algo/Correction.html
python內建itertools模組簡介,窮舉排列組合
https://ithelp.ithome.com.tw/articles/10221633
Hamming Distance & Hamming Weight
https://hackmd.io/@RusselCK/sysprog2020_quiz4
Total Hamming Distance 排列组合与按位统计
https://blog.csdn.net/xiangguang_fight/article/details/117373871

Más contenido relacionado

Más de YOU SHENG CHEN

Más de YOU SHENG CHEN (20)

Paper sharing_How Information Technology Governance Influences Organizational...
Paper sharing_How Information Technology Governance Influences Organizational...Paper sharing_How Information Technology Governance Influences Organizational...
Paper sharing_How Information Technology Governance Influences Organizational...
 
Paper sharing_The interplay of digital transformation and employee competency
Paper sharing_The interplay of digital transformation and employee competencyPaper sharing_The interplay of digital transformation and employee competency
Paper sharing_The interplay of digital transformation and employee competency
 
Paper sharing_A digital twin hierarchy for metal additive manufacturing
Paper sharing_A digital twin hierarchy for metal additive manufacturingPaper sharing_A digital twin hierarchy for metal additive manufacturing
Paper sharing_A digital twin hierarchy for metal additive manufacturing
 
Paper sharing_Digital servitization of symbiotic service composition in produ...
Paper sharing_Digital servitization of symbiotic service composition in produ...Paper sharing_Digital servitization of symbiotic service composition in produ...
Paper sharing_Digital servitization of symbiotic service composition in produ...
 
Paper sharing_The architectural design and implementation of a digital platfo...
Paper sharing_The architectural design and implementation of a digital platfo...Paper sharing_The architectural design and implementation of a digital platfo...
Paper sharing_The architectural design and implementation of a digital platfo...
 
Paper sharing_Legacy information system replacement_Pursuing quality design o...
Paper sharing_Legacy information system replacement_Pursuing quality design o...Paper sharing_Legacy information system replacement_Pursuing quality design o...
Paper sharing_Legacy information system replacement_Pursuing quality design o...
 
Microservice 微服務
Microservice 微服務Microservice 微服務
Microservice 微服務
 
Paper sharing_Standardizing information security _ a structurational analysis
Paper sharing_Standardizing information security _ a structurational analysisPaper sharing_Standardizing information security _ a structurational analysis
Paper sharing_Standardizing information security _ a structurational analysis
 
Paper sharing_data-driven smart manufacturing (include smart manufacturing se...
Paper sharing_data-driven smart manufacturing (include smart manufacturing se...Paper sharing_data-driven smart manufacturing (include smart manufacturing se...
Paper sharing_data-driven smart manufacturing (include smart manufacturing se...
 
Paper sharing_Swarm intelligence goal oriented approach to data-driven innova...
Paper sharing_Swarm intelligence goal oriented approach to data-driven innova...Paper sharing_Swarm intelligence goal oriented approach to data-driven innova...
Paper sharing_Swarm intelligence goal oriented approach to data-driven innova...
 
Paper sharing_Tapping into the wearable device revolution in the work environ...
Paper sharing_Tapping into the wearable device revolution in the work environ...Paper sharing_Tapping into the wearable device revolution in the work environ...
Paper sharing_Tapping into the wearable device revolution in the work environ...
 
Paper sharing_New product development in taiwanese ic design companies
Paper sharing_New product development in taiwanese ic design companiesPaper sharing_New product development in taiwanese ic design companies
Paper sharing_New product development in taiwanese ic design companies
 
Paper sharing_Edge based intrusion detection for IOT devices
Paper sharing_Edge based intrusion detection for IOT devicesPaper sharing_Edge based intrusion detection for IOT devices
Paper sharing_Edge based intrusion detection for IOT devices
 
Paper sharing_examining branding co creation in brand communities on social m...
Paper sharing_examining branding co creation in brand communities on social m...Paper sharing_examining branding co creation in brand communities on social m...
Paper sharing_examining branding co creation in brand communities on social m...
 
Paper sharing_resource optimization scheduling and allocation for hierarchica...
Paper sharing_resource optimization scheduling and allocation for hierarchica...Paper sharing_resource optimization scheduling and allocation for hierarchica...
Paper sharing_resource optimization scheduling and allocation for hierarchica...
 
Paper sharing_alternate strategies for a win win seeking agent in agent–human...
Paper sharing_alternate strategies for a win win seeking agent in agent–human...Paper sharing_alternate strategies for a win win seeking agent in agent–human...
Paper sharing_alternate strategies for a win win seeking agent in agent–human...
 
Paper sharing_deep learning for smart manufacturing methods and applications
Paper sharing_deep learning for smart manufacturing methods and applicationsPaper sharing_deep learning for smart manufacturing methods and applications
Paper sharing_deep learning for smart manufacturing methods and applications
 
Paper shareing_Platform design framework conceptualisation and application
Paper shareing_Platform design framework conceptualisation and applicationPaper shareing_Platform design framework conceptualisation and application
Paper shareing_Platform design framework conceptualisation and application
 
Paper sharing_Personal Values and Ethical Behavior in Accounting Students
Paper sharing_Personal Values and Ethical Behavior in Accounting Students Paper sharing_Personal Values and Ethical Behavior in Accounting Students
Paper sharing_Personal Values and Ethical Behavior in Accounting Students
 
Paper sharing_Modernizing the HPC system
Paper sharing_Modernizing the HPC systemPaper sharing_Modernizing the HPC system
Paper sharing_Modernizing the HPC system
 

LeetCode477_Total Hamming Distance.pptx