- 博客(52)
- 收藏
- 关注
原创 leetcode中01背包问题思路梳理
leetcode中01背包问题思路梳理416. 分割等和子集思路空间优化474. 一和零待续。。494. 目标和待续。。416. 分割等和子集给你一个 只包含正整数 的 非空 数组 nums 。请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。思路乍一看与背包问题无关,但其实可以转化成01背包问题:给定N个物品,每个物品的重量为nums[i],和一个容量为sum/2的背包,问是否存在一种方法,可以恰好装满背包。定义dp[i][j]:用前i个数字,是否可能恰好凑成和为i。b
2021-07-08 13:31:54
843
原创 leetcode中完全背包问题思路梳理——零钱兑换
322. 零钱兑换给定不同面额的硬币 coins 和一个总金额 amount。编写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合能组成总金额,返回 -1。你可以认为每种硬币的数量是无限的。思路定义dp[i][j]:用前i种面额的硬币,凑成总金额j,所需要的最少硬币个数。basecase:...
2021-07-07 17:42:53
219
原创 经典背包问题思路梳理
01背包n件物品,第i件物品重量w[i],价值c[i],背包容量为V,如何选择物品,使背包内物品总价值最大?【每种物品只有一件】思路定义dp[i][j]:使用前i种物品,背包容量为V,能装下的最大价值。base case:dp[0][…] = 0,dp[…][0] = 0状态转移:装第i件物品:dp[i][j] = dp[i-1][j];不装第i件物品:dp[i][j] = dp[i-1][j-w[i]]+c[i];综上,dp[i][j] = max(dp[i-1][j], dp[i-
2021-07-06 22:03:07
195
原创 golang map转string
child_enums := map[string]string{ "id": "3", "key": "key_temp" } child_json, _ := json.Marshal(child_enums) child_string := string(child_json) fmt.Printf("print mString:%s",child_string)
2021-02-05 15:50:18
1963
原创 golang中map数据结构可以包含不同数据类型value的方法
如果这样写:results := make(map[string]string)则生成的map,value必须都是string类型。如果想要多种value类型的map,可以这样定义:results := make(map[string]interface {})用interface{}来定义value类型,就可以存储int/string/map/…多种类型的value。不过这种interface{}在使用时也要转换一下格式。例如,得到的结果如下:results := [ {...},
2021-02-05 15:40:32
3473
原创 go可选参数的使用——mongodb场景
query := bson.D{ {"domain", url}, } task := bson.M{ "$set": ReqMap, } updateOptions := make([]*options.UpdateOptions,0,1) //切片,注意这里的长度只有1,只能插入1个元素 // for _, opt := range opts{ //由于上面初始化时长度只有1,这里就不用循环了,因为只插入1个元素 tempBool := true uOpts := opt.
2021-01-27 11:28:22
382
转载 数据埋点是什么?设置埋点的意义是什么?
作者:大头鱼链接:https://zhuanlan.zhihu.com/p/25195217来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。所谓埋点就是在应用中特定的流程收集一些信息,用来跟踪应用使用的状况,后续用来进一步优化产品或是提供运营的数据支撑,包括访问数(Visits),访客数(Visitor),停留时长(Time On Site),页面浏览数(Page Views)和跳出率(Bounce Rate)。这样的信息收集可以大致分为两种:页面统计(track t
2020-11-18 16:22:30
791
转载 【Linux】常用必会命令
1. tailtail 命令可用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件。tail -f filename 会把 filename 文件里的最尾部的内容显示在屏幕上,并且不断刷新,只要 filename 更新就可以看到最新的文件内容。命令格式:tail [参数] [文件]2. awkAWK 是一种处理文本文件的语言,是一个强大的文本分析工具。之所以叫 AWK 是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Brian Kern
2020-11-05 16:45:08
111
转载 安装thrift遇到Error: libcrypto required
问题:linux安装thrift,执行到./configure时,遇到问题:Error: libcrypto required网上找到答案:“在debian 8上,sudo apt-get install libssl1.0-dev解决了这个问题。”“sudo apt-get install libssl1.0-dev”
2020-11-05 15:42:11
2442
转载 【github】mac终端如何退出git:(master)
@TOC今天在终端误操作,在主目录下执行git init命令,结果杯具了, 总是出现这个提示。各种搜索解决方案,终于退出了。方法如下:删掉.git目录:rm -rf ~/.git
2020-11-05 11:11:18
2175
1
原创 【leetcode】1143. 最长公共子序列(动态规划)
【leetcode】1143. 最长公共子序列题目描述解题思路1. DP基础方法代码2. DP优化(一维数组)代码题目描述给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。例如,“ace” 是 “abcde” 的子序列,但 “aec” 不是 “abcde” 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列
2020-10-13 17:01:27
245
原创 【leetcode】64. 最小路径和(动态规划)
【leetcode】64. 最小路径和题目描述解题思路1. 可以修改原数组的方法代码2. 不能修改原数组,空间复杂度O(n)代码题目描述给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。说明:每次只能向下或者向右移动一步。示例:输入:[[1,3,1],[1,5,1],[4,2,1]]输出: 7解释: 因为路径 1→3→1→1→1 的总和最小。解题思路整体思路就是动态规划,只不过根据是否能修改原数组,选择二维数组(原数组),或者
2020-10-12 22:05:16
207
原创 【leetcode】300. 最长上升子序列(动态规划)
【leetcode】300. 最长上升子序列题目描述解题思路1. 动态规划,时间复杂度O(n2)代码2. 贪心+二分查找,时间复杂度O(nlogn)代码题目描述给定一个无序的整数数组,找到其中最长上升子序列的长度。示例:输入: [10,9,2,5,3,7,101,18]输出: 4解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。说明:可能会有多种最长上升子序列的组合,你只需要输出对应的长度即可。你算法的时间复杂度应该为 O(n2) 。进阶: 你能将算法的时间复杂度降低到
2020-10-12 20:25:11
124
原创 【剑指offer】40. 最小的k个数
剑指 Offer 40. 最小的k个数题目描述解题思路1. 最大堆2. 快速排序(最高效)3. 冒泡排序这道题分成两道,一道是不考虑答案取模,一道考虑取模。题目描述输入整数数组 arr ,找出其中最小的 k 个数。例如,输入4、5、1、6、2、7、3、8这8个数字,则最小的4个数字是1、2、3、4。示例 1:输入:arr = [3,2,1], k = 2输出:[1,2] 或者 [2,1]示例 2:输入:arr = [0,1,2,1], k = 1输出:[0]限制:0 <= k &
2020-09-23 15:06:38
118
原创 【剑指offer】14. 剪绳子
剑指 Offer 14. 剪绳子题目描述1. 题目一2. 题目二解题思路(题目一)1. 动态规划2. 贪婪算法解题思路(题目二)这道题分成两道,一道是不考虑答案取模,一道考虑取模。题目描述1. 题目一给你一根长度为 n 的绳子,请把绳子剪成整数长度的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0],k[1]…k[m-1] 。请问 k[0] * k[1] * … * k[m-1] 可能的最大乘积是多少?例如,当绳子的长度是8时,我们把它剪成长度分别为2、3、3的三
2020-09-22 15:45:25
138
原创 【剑指offer】11. 旋转数组的最小数字
剑指 Offer 11. 旋转数组的最小数字题目描述解题思路代码题目描述把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一个旋转,该数组的最小值为1。示例 1:输入:[3,4,5,1,2]输出:1示例 2:输入:[2,2,2,0,1]输出:0解题思路类似二分查找的思想。令left=0,right=numbers.length-1,每次计算mid =
2020-09-22 10:48:58
97
原创 【剑指offer】7. 重建二叉树
剑指 Offer 07. 重建二叉树题目描述解题思路1. 递归2. 迭代代码题目描述输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如,给出前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7]返回如下的二叉树: 3 / \ 9 20 / \ 15 7限制:0 <= 节点个数 <= 5000解题思路根据二叉树的前序
2020-09-21 21:59:50
117
原创 【剑指offer】3. 数组中重复的数字
剑指 Offer 03. 数组中重复的数字题目描述解题思路1. 暴力搜索2. 可修改原数组的算法3. 不可修改原数组的算法(1)辅助空间(2)二分查找题目描述找出数组中重复的数字。在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。示例 1:输入:[2, 3, 1, 0, 2, 5, 3]输出:2 或 3限制:2 <= n <= 100000解题
2020-09-21 19:24:26
112
原创 [leetcode] 117. Populating Next Right Pointers in Each Node II
117. Populating Next Right Pointers in Each Node IIDescriptionAnalysisCodeDescriptionGiven a binary treestruct Node {int val;Node *left;Node *right;Node *next;}Populate each next pointer to p...
2019-08-01 17:24:52
118
原创 [leetcode] 116. Populating Next Right Pointers in Each Node
116. Populating Next Right Pointers in Each NodeDescriptionAnalysisCodeDescriptionYou are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The b...
2019-08-01 17:12:40
138
原创 [leetcode]108 &109. Convert Sorted Array/List to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree & 109. Convert Sorted List to Binary Search TreeDescriptionAnalysisCodeDescriptionGiven an array where elements are sorted in ascending order, con...
2019-08-01 13:16:15
96
原创 [leetcode] 137. Single Number II
137. Single Number IIDescriptionAnalysisCodeDescriptionGiven a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:...
2019-07-30 17:38:15
90
原创 [leetcode] 96. Unique Binary Search Trees
96. Unique Binary Search TreesDescriptionAnalysisCodeDescriptionGiven n, how many structurally unique BST’s (binary search trees) that store values 1 … n?Example:Input: 3Output: 5Explanation:Gi...
2019-03-14 21:01:53
288
原创 [leetcode] 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder TraversalDescriptionAnalysis & CodeDescriptionGiven a binary tree, return the inorder traversal of its nodes’ values.Example:Input: [1,null,2,3]12/3Output: [1,3,2...
2019-03-09 17:25:11
163
原创 [leetcode] 141. Linked List Cycle
141. Linked List CycleDescriptionAnalysisCodeDescriptionGiven a linked list, determine if it has a cycle in it.To represent a cycle in the given linked list, we use an integer pos which represents ...
2019-03-08 11:18:52
157
翻译 [leetcode] 48. Rotate Image
48. Rotate ImageDescriptionAnalysisCodeDescriptionYou are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, whi...
2019-03-07 17:14:28
157
原创 [leetcode] 75. Sort Colors
75. Sort ColorsDescriptionAnalysisCodeDescriptionGiven an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the o...
2019-03-07 11:51:28
153
原创 [leetcode] 80. Remove Duplicates from Sorted Array II
80. Remove Duplicates from Sorted Array IIDescriptionAnalysisCodeDescriptionGiven a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new l...
2019-03-07 10:25:04
154
原创 [leetcode] 61. Rotate List
61. Rotate ListDescriptionAnalysisCodeDescriptionGiven a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2O...
2019-03-07 09:54:00
165
原创 [leetcode] 92. Reverse Linked List II
92. Reverse Linked List IIDecriptionAnalysisCodeDecriptionReverse a linked list from position m to n. Do it in one-pass.Note: 1 ≤ m ≤ n ≤ length of list.Example:Input: 1->2->3->4->5-&...
2019-03-06 14:25:55
163
原创 [leetcode] 82.Remove Duplicates from Sorted List II
82. Remove Duplicates from Sorted List IIDescriptionAnalysisCodeDescriptionGiven a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original l...
2019-03-06 10:41:28
126
原创 [leetcode] 18. 4Sum
18. 4SumDescriptionAnalysisCodeDescriptionGiven an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadrup...
2019-03-05 22:12:18
136
原创 [leetcode] 16. 3Sum Closest
16. 3Sum ClosestDescriptionAnalysisCodeDescriptionGiven an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the t...
2019-03-05 21:19:23
149
原创 [leetcode]15. 3Sum
15. 3SumDescriptionAnalysisCodeDescriptionGiven an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of ...
2019-03-05 19:57:19
127
原创 [Leetcode] 36. Valid Sudoku
[Leetcode] 36. Valid SudokuDescriptionAnalysisCodeDescriptionDetermine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must cont...
2019-03-05 11:20:49
153
原创 [leetcode] 11. Container With Most Water
[leetcode] 11. Container With Most WaterDescriptionAnalysisCodeDescriptionGiven n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are draw...
2018-10-19 15:32:39
143
原创 [leetcode]1. Two Sum
[leetcode]1. Two SumDescription思路实现代码DescriptionGiven an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have ex...
2018-10-19 15:03:51
114
原创 [leetcode] 88. Merge Sorted Array
DescriptionGiven two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n respectively.You may a...
2018-10-18 22:05:42
129
原创 [leetcode] 66. Plus One
66. Plus OneDescriptionGiven a non-empty array of digits representing a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of th...
2018-10-18 16:30:12
184
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人