diff --git "a/Recursion/\345\256\266/.keep" "b/Recursion/\345\256\266/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/Recursion/\345\256\266/\345\256\266_46.md" "b/Recursion/\345\256\266/\345\256\266_46.md" new file mode 100644 index 0000000000000000000000000000000000000000..a5a25d7b1cc06e9289181e26d70ff3f2c47b666c --- /dev/null +++ "b/Recursion/\345\256\266/\345\256\266_46.md" @@ -0,0 +1,32 @@ +``` +/** + * 作者:家 + * 思路:回溯,循环+递归,用一个Map记录访问过的元素即可 + * 时间复杂度:O(n^2)?不是很确定,用了Map可以保证获取重复元素时为O(1) + * 空间复杂度:O(n),递归最多到n层,加上n长度的Map + */ + func permute(nums []int) [][]int { + l := len(nums) + if l == 0 { + return [][]int{} + } + var dfs func([]int) + have := map[int]bool{} + re := [][]int{} + dfs = func(r []int) { + if len(r) == l { + re = append(re,append([]int{},r...)) + return + } + for i:=0;i x{ + return mid + }else if now > x { + return rec(x,l,mid-1) + } + return rec(x,mid+1,r) +} + + +``` \ No newline at end of file diff --git "a/Recursion/\345\256\266/\345\256\266_77.md" "b/Recursion/\345\256\266/\345\256\266_77.md" new file mode 100644 index 0000000000000000000000000000000000000000..e2ba412a6c1a42295bd393e221a55fbe3f70df3d --- /dev/null +++ "b/Recursion/\345\256\266/\345\256\266_77.md" @@ -0,0 +1,23 @@ +``` +/** + * 作者:家 + * 思路:回溯,循环+递归标配 + * 时间复杂度:O(n*k)?不是很确定,循环n次(递减的等差数列)*递归k次 + * 空间复杂度:O(k),递归最多到k层 + */ +func combine(n int, k int) [][]int { + re := [][]int{} + var dfs func(int,[]int) + dfs = func(index int,r []int) { + if len(r) == k { + re = append(re,append([]int{},r...)) + return + } + for i:=index;i<=n;i++ { + dfs(i+1,append(r,i)) + } + } + dfs(1,[]int{}) + return re +} +``` \ No newline at end of file