算法学习
3、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | package main import ( "fmt" ) func max(x int, y int) int { if x < y { return y } return x } // 窗口滑动 解决子串问题 // 获取最长非重复子串 示例: "acdfeabac" > "acdfe" func getLongestNotRepeat(s string) int { l := 0 r := 0 slen := len(s) currentLen := 1 currentShou := s[l] for ;r<slen;r++ { if l >= r { continue } else if currentShou == s[r] { fmt.Println(s[l:r]) currentLen = max(currentLen, r-l) l++ } } return currentLen } func climbStairs(n int) int { result := 0 if n <= 0 { return 0 } for _,step := range []int{1,2} { if n-step == 0 { result++ } else if n-step >0 { result = result + climbStairs(n-step) } else if n-step <0 { continue } } return result } type ListNode struct { data int Next *ListNode } func main() { a := getLongestNotRepeat( "acdfeabac" ) fmt.Println(a) fmt.Println(climbStairs(10)) } |
2、读取大文件(5G/10G/1T)
for line in fp.readlines(): #如果line很长时,不合适
for a in fp.read(block_size): # 按块读取
1. threading.Semaphore(value=1) 线程信号量,可以用来控制线程线程的阻塞和释放
sm.acquire() 获取一个信号量,信号量-1,不够-1,则线程阻塞
sm.release() 释放一个信号量,信号量+1
示例如下,控制三个线程的执行顺序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | # -*- coding: utf-8 -*- import threading import time sm0 = threading.Semaphore() sm1 = threading.Semaphore( 0 ) sm2 = threading.Semaphore( 0 ) a = 0 def print1(): global a while True : sm1.acquire() if a % 2 = = 1 : print (a, "----" , a, threading.currentThread().name) a = a + 1 time.sleep( 1 ) sm0.release() def print2(): global a while True : sm2.acquire() if a % 2 = = 0 : print (a, "----" , a, threading.currentThread().name) a = a + 1 time.sleep( 1 ) sm0.release() def print0(): global a while True : sm0.acquire() print ( 0 , "----" , a) time.sleep( 1 ) if a % 2 = = 1 : sm1.release() else : sm2.release() if __name__ = = '__main__' : t1 = threading.Thread(target = print1) t0 = threading.Thread(target = print0) t2 = threading.Thread(target = print2) t0.start() t1.start() t2.start() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架