
数据结构与算法分析
文章平均质量分 58
xiakexiaohu
阿里、快手、网易内推,需要私聊~
展开
-
LeetCode.763 Partition Labels(标签分类)
LeetCode 标签分割原创 2022-06-07 11:46:28 · 302 阅读 · 0 评论 -
全组合(SubSets)全排列(Permutation)全组合和(Combination Sum)解题技巧
1.1 全组合(无重复)题目:给定一个数组(不存在重复),输出所有可能的组合,不限定顺序(LeetCode 78)。样例:[1,2,3]=>[[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]技巧:因为是不存在重复的,典型的采用递归即可,即每次取定第一个开始的节点,然后从该节点后面开始放数据和出数据代码:class Solution { public List<List<Integer>> subsets(int[] num原创 2021-08-15 22:36:18 · 2197 阅读 · 1 评论 -
LeetCode.791 Custom Sort String(自定义字符串排序)
1.题目You are given two strings order and s. All the words oforderareuniqueand were sorted in some custom order previously.Permute the characters ofsso that they match the order thatorderwas sorted. More specifically, if a characterxoccurs befo...原创 2021-07-24 22:26:39 · 247 阅读 · 0 评论 -
LeetCode.713 Subarray Product Less Than K(连续子数组乘积比k小的个数)
1.题目Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k. Example 1:Input: nums = [10,5,2,6], k = 100Output: 8Explanation: The 8原创 2021-07-20 12:58:11 · 228 阅读 · 0 评论 -
LeetCode.498 Diagonal Traverse(遍历矩阵)
1.题目Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.Example:Input:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Output: [1,2,4,7,5,3,6,8,9]Explanation:Note:The tot原创 2021-01-04 00:20:14 · 233 阅读 · 0 评论 -
LeetCode.399 Evaluate Division(评估除法)
1.题目You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.You are also g原创 2020-10-10 23:43:06 · 193 阅读 · 0 评论 -
Leetcode.665 Non-decreasing Array(替换一个数成为非降序数组)
1.题目Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2原创 2020-08-09 21:00:50 · 327 阅读 · 0 评论 -
LeetCode.698 Partition to K Equal Sum Subsets(均分成k组和相等)
1.题目Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into k non-empty subsets whose sums are all equal.2.输入/输出样例Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4Output: TrueExplanation: It's possible原创 2020-08-08 09:44:39 · 205 阅读 · 0 评论 -
LeetCode.540 01 Matrix(距离0最近的距离)
1.题目Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.The distance between two adjacent cells is 1.2.输入输出样例子Example 1:Input:[[0,0,0], [0,1,0], [0,0,0]]Output:[[0,0,0], [0,1,0], [0,0,0]]Example 2:Input:[[0原创 2020-07-26 11:28:23 · 310 阅读 · 0 评论 -
LeetCode.738 Monotone Increasing Digits(单调递增的数)
1.题目Given a non-negative integer N, find the largest number that is less than or equal to N with monotone increasing digits.(Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.)2.样原创 2020-06-27 15:56:21 · 190 阅读 · 0 评论 -
寻找数组中最小的k个数(快排和堆排)
转载自:http://blog.youkuaiyun.com/liuguangqiang/article/details/52210239题目描述 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。思路1:利用快排的思想,寻找第k个位置上正确的数,k位置前面的数即是比k位置小的数组,k后面的数即是比k位置元素大的数组。 publ...转载 2018-03-04 19:54:38 · 1100 阅读 · 0 评论 -
算术表达式转成后缀表达式
package datastructure;public class Postfix { /** * 转为后缀表达式: * 1、如果是"("直接压入栈。 * 2、如果是")",依次从栈弹出运算符加到postfix的末尾,直到遇到"("; * 3、如果是运算符,比较扫描到的运算符,和栈顶的运算符。如果扫描到的运算符优先级 * 高转载 2017-11-26 11:38:30 · 1080 阅读 · 0 评论 -
ch04_使用多个映射的例子
题目: 许多单词都和另一个单词相似。例如:通过改变第1个字母,单词wine可以变成dine、fine、line、mine等。通过改变第3个字母,wine可以变成wide、wife、wipe、或wire等。通过改变第4个字母,wine可以变成wind、wing、wink或wins。 假设我们有一个词典,由大约89000个不同长度的不同单词组成。实际上:最可变化的单词是3个字母的、4原创 2017-11-01 21:56:16 · 973 阅读 · 0 评论 -
Ch_01 引论
1.Mod运算(非常耗时) n%10=n-「n/10」*10;//向下取整 2.JDK5中Object泛型不能使用基本类型赋值(int等),必须使用引用类型(Integer等)。 3.泛型类型的实例化 不能创建一个泛型类型的实例。如果T是一个类型变量,则语句 T obj =new T(); //右边是非法的 是非法的。 4.递归四条基本法原创 2017-08-16 21:57:20 · 318 阅读 · 0 评论 -
Ch_02 算法分析(最大子序列问题)
需求:最大子序列问题 给定(可能有负的)整数A1,A2..An,求最大子序列之和。(为了方便起见,如果所有整数均为负数,则最大子序列和为0) 例如:对于输入-2,11,-4,13,-5,-2,答案为20(从A2 到A4)算法分析:算法1:该算法肯定会正确运行,运行时间为O(n*3)(最简单,但是也是运行最慢的算法,后面会讲解原因)class Solution原创 2017-08-28 11:34:45 · 335 阅读 · 0 评论 -
Ch_03 表、栈和队列
1.解释一个数组arr在必要的时候如何被拓展(其初始长度为10)int [] arr=new int[10];....//下面我们决定需要扩大arrint [] newArray=new int[arr.lenth*2];for(int i=0;i<arr.length;i++){ newArray[i]=arr[i];}arr=newArray;2.Iterator接口原创 2017-09-05 15:23:07 · 181 阅读 · 0 评论