1 求出等于目标值的数的下标
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("[索引下标是]____:",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 选择排序
// 选择排序 (不断的跟左右比较,找到最小的下标的值,记录下来交换位置)
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 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数0 0;当 A[i] 为偶数时,i也是偶数
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 计算字符串中不重复的最长子串的长度 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("【最长的不重复的字符串长度为】___:",max) // 3
}
7 求中位数
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 切片数字反转
// 输入:[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("反转后的切片__:",s) //[66 55 22 33 11]
}
9 求素数
func main(){
// 找1000内的素数
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)
}()
}
//简单求素数写法
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("求素数__:", 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的大顶堆
// 大顶堆就是子节点比父节点小的树,堆化就是父节点跟子节点发生交换
type heap struct{
m []int
len int
}
func main() {
m := []int{0,9,3,6,2,1,7} //第0个下标不放目标元素
h := buildHeap(m) //建堆,返回一个heap结构
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)//向切片尾部插入数据(推断出父节点下标为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] //把最后一个元素给堆顶
h.len--
//对堆顶节点进行堆化即可
heapf(h.m, h.len, 1)
return out
}
//对下标为i的节点进行堆化, n表示堆的最后一个节点下标
//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节点位置正确,则退出
break
}
m[i],m[maxPos] = m[maxPos],m[i]
i = maxPos
}
}
11 单链表反转
type ListNode struct {
data interface{}
Next *ListNode
}
//反转单链表
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
}
获取数组里最长子串的总和
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
}
二维数组顺时针螺旋迭代成一维数组
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
}
斐波拉切数列
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)
}