比如有最经典的sliding window模式,Two pointers模式,快慢指针模式,合并intervals模式,cyclic sort模式,in-place翻转链表模式,树上的BFS,树上的DFS,双Heaps模式,subsets模式,二分法变种,Top K模式,多路模式(K-ways),0/1背包,拓扑排序。 这个课程来自于educative,是一个美国的算法面试方面很出色的网课平台。
intn,intw){boolean[][] states =newboolean[n][w+1];// 默认值 falsestates[0][0] =true;// 第一行的数据要特殊处理,可以利用哨兵优化states[0][weight[0]] =true;for(inti=1; i < n; ++i) {// 动态规划状态转移for(intj=0; j <= w; ++j) {// 不把第 i 个物品放入背包if(states...
A rather straight forward solution is a two-pass algorithm using counting sort. First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's. Could you come up with a one-pass algorithm using only con...
对于区间 DP 这一类问题,我们需要计算区间 [1,n] 的答案,通常用一个二维数组dp 表示,其中 dp[x][y] 表示区间 [x,y]。 有些题目,dp[l][r] 由 dp[l][r−1] 与 dp[l+1][r] 推得;也有些题目,我们需要枚举区间 [l,r] 内的中间点,由两个子问题合并得到,也可以说 dp[l][r] 由 dp[l]...
sort(str, (String o1, String o2) -> (o2+o1).compareTo(o1 + o2)); if(str[0].equals("0")) return "0"; String largestNumberStr = new String(); for (String numAsStr : str) { largestNumberStr += numAsStr; } return largestNumberStr; } ...
1、组合 问题描述 给定两个整数n和k,返回1 ... n 中所有可能的 k个数的组合。 示例: 输入: n = 4, k = 2 输出: [ [2,4], [3,4], [2,3], [1,2],
{i:number;p:number;h:number;d:string;}letrobots:robot[]=[];//从左至右(排序后的下标)遍历机器人并判断当前机器人的方向positions.forEach((v,i)=>robots.push({i:i,p:v,h:healths[i],d:directions[i]}));robots.sort((a,b)=>a.p-b.p);for(leti=0;i<robots.length;){/**比较与栈...
排序算法我们将数组进行排序,那排序后的数组的中点一定就是众数。defmajorityElement(nums):#将数组排序nums.sort()#返回排序数组中的中点returnnums[len(nums)//2]data=[1,2,3,2,2,2,5,4,2]print(majorityElement(data))Boyer-Moore投票算法这道题最经典的解法是Boyer-Moore投票算法。Boyer-...
1、把数组中的0移动到数组尾部 283. Move Zeroes(Easy) Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] Note: You must do this in-place...
输入:numCourses = 2, prerequisites = [[1,0]]输出:true解释:总共有 2 门课程。学习课程 1 之前,你需要完成课程 0 。这是可能的。 示例2: 输入:numCourses = 2, prerequisites = [[1,0],[0,1]]输出:false解释:总共有 2 门课程。学习课程 1 之前,你需要先完成课程 0 ;并且学习课程 0 之前...