網(wǎng)站首頁 編程語言 正文
數(shù)組元素兩兩相加
count = 0
c2 = []
for i in range(len(c)):
if count == 0:
mm = c[i]
#print(mm)
#print(count)
if count == 1:
print(c[i])
value = c[i] + mm
c2 = np.append(c2,value)
count = 0
mm = 0
value = 0
continue
count = count + 1
c是原數(shù)組,我們?cè)O(shè)置一個(gè)count,當(dāng)count=0時(shí)我們不操作,count=1時(shí)我們把當(dāng)前元素與前一元素相加。
count=0其實(shí)相當(dāng)于奇數(shù),count=1相當(dāng)于偶數(shù)
求數(shù)組中兩兩相加等于20的組合(Python實(shí)現(xiàn))
題目
求數(shù)組中兩兩相加等于20的組合。
例:給定一個(gè)數(shù)組[1, 7, 17, 2, 6, 3, 14],這個(gè)數(shù)組中滿足條件的有兩對(duì):17+3=20, 6+14=20。
解析
分為兩個(gè)步驟:
- 先采用堆排序或快速排序?qū)?shù)組進(jìn)行排序,時(shí)間復(fù)雜度為O(nlogn)。
- 然后對(duì)排序的數(shù)組分別從前到后和從后到前進(jìn)行遍歷, 時(shí)間復(fù)雜度為O(n)。
假設(shè)從前到后遍歷的下標(biāo)為begin,從后到前遍歷的下標(biāo)為end。
- 當(dāng)arr[begin] + arr[end] < 20時(shí),滿足條件的數(shù)一定在[begin+1, end]之間;
- 當(dāng)arr[begin] + arr[end] > 20時(shí),滿足條件的數(shù)一定在[begin, end-1]之間;
- 當(dāng)arr[begin] + arr[end] = 20時(shí),找到一組符合條件的數(shù),剩下的組合一定在[begin-1, end-1]之間。
整個(gè)算法的時(shí)間復(fù)雜度為O(nlogn)。
Python實(shí)現(xiàn)
# -*- coding:utf-8 -*-
def quick_sort(arr, left, right):
"""快速排序"""
if left >= right:
return
low = left
high = right
p = arr[left]
while left < right:
while left < right and arr[right] >= p:
right -= 1
arr[left] = arr[right]
while left < right and arr[left] <= p:
left += 1
arr[right] = arr[left]
arr[left] = p
quick_sort(arr, low, left-1)
quick_sort(arr, left+1, high)
def find_sum(arr, sum):
"""尋找數(shù)組中相加等于sum的組合"""
quick_sort(arr, 0, len(arr) - 1)
begin, end = 0, len(arr) - 1
while begin < end:
if arr[begin] + arr[end] < sum:
begin += 1
elif arr[begin] + arr[end] > sum:
end -= 1
else:
print('%s %s' % (arr[begin], arr[end]))
begin += 1
end -= 1
if __name__ == '__main__':
arr = [1, 7, 17, 2, 6, 3, 14]
find_sum(arr, 20)
原文鏈接:https://blog.csdn.net/weixin_44843629/article/details/114366383
相關(guān)推薦
- 2022-06-21 使用Apache?Hudi?加速傳統(tǒng)的批處理模式的方法_Linux
- 2022-05-23 高效的數(shù)據(jù)同步工具DataX的使用及實(shí)現(xiàn)示例_數(shù)據(jù)庫其它
- 2022-07-08 C#獲取應(yīng)用程序路徑或Web頁面目錄路徑_C#教程
- 2022-11-21 Pandas數(shù)據(jù)分析之groupby函數(shù)用法實(shí)例詳解_python
- 2022-01-18 利用css3實(shí)現(xiàn)立體旋轉(zhuǎn)動(dòng)畫效果
- 2022-04-25 Pycharm報(bào)錯(cuò):'NoneType'?object?has?no?attribute?'byte
- 2022-10-15 Go?Excelize?API源碼解讀GetSheetViewOptions與SetPageLayo
- 2022-10-10 python使用pandas讀寫excel文件的方法實(shí)例_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支