算法设计
风格色
这个作者很懒,什么都没留下…
展开
-
回溯法的算法框架
回溯法子集树代码如下:def constraint(): # 约束函数 return Truedef bound(): # 限界函数 return Truedef backtracing(t, lst, temp): # 回溯法函数 size = len(lst) if t == size: print(temp)原创 2017-01-02 17:08:01 · 1519 阅读 · 0 评论 -
回溯法和分支界限法解决N后问题
节点的数据结构class Node(object): # 节点类 def __init__(self, header, coor, size): self.header = header # 记录前一个节点 self.coor = coor # 当前结点的坐标 元组 self.footer = [None for i in range(s原创 2017-01-06 17:16:23 · 1215 阅读 · 0 评论 -
尾递归
尾递归函数调用自身,称为递归。如果尾调用自身,就称为尾递归。递归非常耗费内存,因为需要同时保存成千上百个调用帧,很容易发生“栈溢出”错误(stack overflow)。但对于尾递归来说,由于只存在一个调用帧,所以永远不会发生“栈溢出”错误。对比package mainimport ( "fmt")// 存在尾递归func fibonacci(n int) int { if n原创 2017-07-11 15:02:12 · 1836 阅读 · 0 评论 -
golang -- slice元素去重
合并两个整型切片,返回没有重复元素的切片,有两种去重策略1. 通过双重循环来过滤重复元素(时间换空间)// 通过两重循环过滤重复元素func RemoveRepByLoop(slc []int) []int { result := []int{} // 存放结果 for i := range slc{ flag := true for j := r原创 2017-08-19 10:59:18 · 45581 阅读 · 2 评论 -
回溯法-排列树 解决N后问题
# coding=utf-8# 约束函数 判断lst中的点是否在同一斜线上, 如果在,返回False# lst是一个列表, 用序号index作为横坐标, 对应的值lst[index]作为纵坐标# 例如 [1, 3]代表点集(0, 1) (1, 3)def constraint(lst): length = len(lst) for i in range(length-1):原创 2017-09-16 11:59:28 · 1913 阅读 · 0 评论 -
golang 排序算法
排序算法的比较待排序list lst = [28, 35, 46, 32, 64, 21]直接插入排序排序思想: 假设第一个数是排序好的序列,不断从后面选取一个,插入到前面排序好的序列.[28 35 | 46 32 64 21][28 35 46 | 32 64 21][28 32 35 46 | 64 21][28 32 35原创 2017-06-14 20:16:45 · 300 阅读 · 0 评论 -
赛马比赛:25匹马,5个赛道,选出跑的快的前几匹
`package mainimport ( “fmt” “math/rand” “sort” )// 赛马 //@题目:有25匹马,5个赛道,选出跑的最快的前几匹。 //@思路:先将25匹马,分成5组,组内排序,倒序。 如[5, 4, 3, 2, 1] // 将5组马,放到5个赛道中,组内跑的最快的在队首。 // 每个赛道跑的最快...原创 2018-07-30 17:02:07 · 4065 阅读 · 1 评论 -
算法: Find Pivot Index
Given an array of integers nums, write a method that returns the “pivot” index of this array.We define the pivot index as the index where the sum of the numbers to the left of the index is equal to t...原创 2018-12-03 13:18:01 · 416 阅读 · 0 评论