1 求出等于目標(biāo)值的數(shù)的下標(biāo)
func main(){
list:=[]int{1,3,6,9,34,67,99}
t:=10
for i:=len(list)-1;i>=0;i--{
for j:=0;j<i;j++{
if list[i]+list[j]==t{
fmt.Println("[索引下標(biāo)是]____:",i,j) // 0,3
}
}
}
}
2 快排
def quick_sort(arr):
if len(arr) <= 1:
return arr
else:
p = arr[len(arr)//2]
left = [x for x in arr if x < p]
middle = [x for x in arr if x == p]
right = [x for x in arr if x > p]
return quick_sort(left) + middle + quick_sort(right)
if __name__ == "__main__":
arr = [12,11,4,66,99,107,202]
v = quick_sort(arr)
print(v)
func main(){
li := []int{54, 26, 93, 55, 77, 31, 44, 55, 20}
qulckSort(li,0,len(li)-1)
fmt.Println(li) // [20 26 31 44 54 55 55 77 93]
}
func qulckSort(li []int, start,end int){
if start >= end{
return
}
left:=start
right:=end
baseValue := li[left]
for left<right{
for left<right && li[right]>=baseValue{
right-=1
}
li[left]=li[right]
for left<right && li[left] < baseValue{
left+=1
}
li[right]=li[left]
}
li[left] = baseValue
qulckSort(li, start, left-1)
qulckSort(li, left+1, end)
}
3 冒泡排序
func main(){
s:=[]int{2,4,1,8,9,9,6,0}
for i:=0;i<=len(s)-1;i++{
flag:=false
for j:=0;j<len(s)-1-i;j++{
if s[j] > s[j+1]{
s[j],s[j+1] = s[j+1],s[j]
flag=true
}
}
if flag==false{
break
}
}
fmt.Println(s) // [0,1,2,4,6,8,9,9]
}
4 選擇排序
// 選擇排序 (不斷的跟左右比較,找到最小的下標(biāo)的值,記錄下來交換位置)
func main(){
s:=[]int{2,4,6,9,11,22,3}
for i:=0;i<=len(s)-1;i++{
index:=i
for j:=i+1;j<len(s);j++{
if s[j]<s[index]{
index=j
}
}
if index!=i{
s[index],s[i] = s[i],s[index]
}
}
fmt.Println(s) // [2 3 4 6 9 11 22]
}
5 對數(shù)組進(jìn)行排序缠犀,以便當(dāng) A[i] 為奇數(shù)時(shí),i 也是奇數(shù)0 0葬毫;當(dāng) A[i] 為偶數(shù)時(shí)挖腰,i也是偶數(shù)
func main(){
result:=[]int{4,2,5,7}
list:=make([]int, len(result))
a,b:=0,0
for i:=0;i<=len(result)-1;i++{
if result[i]%2==0{
list[b] = result[i]
b +=2
}else{
list[a]=result[i]
a+=2
}
}
fmt.Println(list) // [4 5 2 7]
}
6 計(jì)算字符串中不重復(fù)的最長子串的長度 abcabcbb ==3
func main(){
str:="pwwkew"
max := 0
m:=make(map[byte]int)
for i,j:=0,0;j<len(str);j++{
if v,ok := m[byte(str[j])];ok{
if i<v{
i=v
}
}
if max<j-i+1{
max=j-i+1
}
m[byte(str[j])] = j+1
}
fmt.Println("【最長的不重復(fù)的字符串長度為】___:",max) // 3
}
7 求中位數(shù)
func main(){
fmt.Println(m()) // [2 4] [3] ==>[3]
}
func m()float64{
num1 := []int{2,4}
num2 := []int{3}
num1 = append(num1, num2...)
n:=len(num1)
for i:=0;i<n;i++{
for j:=0;j<i;j++{
if num1[j]>num1[j+1]{
num1[j],num1[j+1] = num1[j+1],num1[j]
}
}
}
if n%2==0 {
return float64((num1[n/2] + num1[n-1]))/2
}else{
return float64(num1[n/2])
}
}
8 切片數(shù)字反轉(zhuǎn)
// 輸入:[1 2 3 4 5] 輸出:[5 4 3 2 1]
func main(){
s := []int{11,33,22,55,66}
for i,j:=0,len(s)-1;i<j;i,j = i+1, j-1{
s[i],s[j]=s[j],s[i]
}
fmt.Println("反轉(zhuǎn)后的切片__:",s) //[66 55 22 33 11]
}
9 求素?cái)?shù)
func main(){
// 找1000內(nèi)的素?cái)?shù)
origin, wait := make(chan int), make(chan struct{})
Processor(origin, wait)
for num:=2;num<1000;num++{
origin <- num
}
close(origin)
<- wait
}
func Processor(origin chan int, wait chan struct{}){
go func() {
data, ok := <-origin
if !ok{
close(wait)
return
}
fmt.Println(212,data)
out := make(chan int)
Processor(out, wait)
for num := range origin{
if num%data != 0{
out<-num
}
}
close(out)
}()
}
//簡單求素?cái)?shù)寫法
func main(){
p := make([]int, 0)
for i:=2;i<100;i++{
for v:= range p{
if i%p[v]== 0 && p[v] > 1{
goto flag
}
}
p = append(p, i)
flag:
}
fmt.Println("求素?cái)?shù)__:", p)
}
。。妖碉。涌庭。。欧宜。
# Python語法:
def Prime(num):
a=[1 for i in range(0,num+1)]
for i in range(2, int(num**.5)+1):
if a[i]:
j=i
while j*i<=num:
a[i*j]=0
j+=1
m = [i for i in range(2,num+1) if a[i]]
return m # num=10 -->[2, 3, 5, 7]
10 容量為K的大頂堆
// 大頂堆就是子節(jié)點(diǎn)比父節(jié)點(diǎn)小的樹坐榆,堆化就是父節(jié)點(diǎn)跟子節(jié)點(diǎn)發(fā)生交換
type heap struct{
m []int
len int
}
func main() {
m := []int{0,9,3,6,2,1,7} //第0個(gè)下標(biāo)不放目標(biāo)元素
h := buildHeap(m) //建堆,返回一個(gè)heap結(jié)構(gòu)
h.Push(50)
h.Pop()
fmt.Println(h.m) // [0 9 3 7 2 1 6 7]
}
func buildHeap(m []int) *heap{
n := len(m)-1
for i:=n/2; i>0; i-- {
heapf(m, n, i)
}
return &heap{m,n}
}
func (h *heap)Push(data int) {
h.len++
h.m = append(h.m, data)//向切片尾部插入數(shù)據(jù)(推斷出父節(jié)點(diǎn)下標(biāo)為i/2)
i := h.len
for i/2 >0 && h.m[i/2]<h.m[i] { //自下而上的堆化
h.m[i/2], h.m[i] = h.m[i], h.m[i/2]
i = i/2
}
}
func (h *heap)Pop() int{
if h.len < 1 {
return -1
}
out := h.m[1]
h.m[1] = h.m[h.len] //把最后一個(gè)元素給堆頂
h.len--
//對堆頂節(jié)點(diǎn)進(jìn)行堆化即可
heapf(h.m, h.len, 1)
return out
}
//對下標(biāo)為i的節(jié)點(diǎn)進(jìn)行堆化冗茸, n表示堆的最后一個(gè)節(jié)點(diǎn)下標(biāo)
//2i,2i+1
func heapf(m []int, n,i int) {
for {
maxPos := i
if 2*i<= n && m[2*i] > m[i] {
maxPos = 2*i
}
if 2*i+1 <=n && m[2*i+1] > m[maxPos] {
maxPos = 2*i + 1
}
if maxPos == i { //如果i節(jié)點(diǎn)位置正確席镀,則退出
break
}
m[i],m[maxPos] = m[maxPos],m[i]
i = maxPos
}
}
11 單鏈表反轉(zhuǎn)
type ListNode struct {
data interface{}
Next *ListNode
}
//反轉(zhuǎn)單鏈表
func reverseList(head *ListNode) *ListNode {
var pre *ListNode
for head != nil {
head.Next, pre, head = pre, head, head.Next
}
return pre
}
func CreateNode(node *ListNode, max int) {
for i := 1; i < max; i++ {
node.Next = &ListNode{}
node.Next.data = i
node = node.Next
}
}
//打印鏈表的方法
func PrintNode(info string, node *ListNode) {
fmt.Print(info,"\n")
for cur := node; cur != nil; cur = cur.Next {
fmt.Print(cur.data, " ")
}
}
func main() {
var head = new(ListNode)
CreateNode(head, 10)
PrintNode("前:", head)
node := reverseList(head)
PrintNode("后:", node) //1 2 3 4 5 6 7 8 9---> 9 8 7 6 5 4 3 2 1
}
獲取數(shù)組里最長子串的總和
func main() {
s := []int{-2,1,-1,2,4,1,-5,4}
var maxSum int
for i:=0;i<len(s);i++{
sum:=0
for j:=i;j<len(s);j++{
sum+=s[j]
if sum>maxSum{
maxSum=sum
}
}
}
fmt.Println(maxSum) //7
}
二維數(shù)組順時(shí)針螺旋迭代成一維數(shù)組
func main() {
a:=[][]int{
{1,2,3,4},
{7,8,9,10},
{3,4,5,6},
}
s:=f1(a)
fmt.Println(s) // [1 2 3 4 10 6 5 4 3 7 8 9]
}
func f1(a [][]int)[]int{
dir,row,col := 1,0,0
top,right,bottom,left := 0, len(a[0])-1, len(a)-1,0
s:=make([]int, 0)
for top<=bottom && left <= right{
s = append(s, a[row][col])
switch dir{
case 1:
if col==right{
dir=2
top++
row++
continue
}
col++
case 2:
if row==bottom{
dir=3
right--
col--
continue
}
row++
case 3:
if col==left{
dir=4
bottom--
row--
continue
}
col--
case 4:
if row==top{
dir=1
left++
col++
continue
}
row--
}
}
return s
}
斐波拉切數(shù)列
func main(){
s := "1,1,2,3,5,8,13,21,34,55"
for i:=1;i<len(s);i++{
fmt.Println(f1(i))
}
}
func f1(n int)int{
if n < 2{
return n
}
return f1(n-2)+f1(n-1)
}