
数据结构与算法
文章平均质量分 51
丁真的小马
这个作者很懒,什么都没留下…
展开
-
LeetCode:动态规划-回文/公共子序列/公共字串
动态规划字符串原创 2022-07-25 15:04:52 · 769 阅读 · 0 评论 -
LeetCode:前缀和
一、和为K的子数组560:给你一个整数数组 nums 和一个整数 k ,请你统计并返回该数组中和为 k 的连续子数组的个数。class Solution { public int subarraySum(int[] nums, int k) { //key为前缀和,value为此前缀和的个数 Map<Integer,Integer> map = new HashMap<>(); map.put(0,1);原创 2022-01-15 21:49:12 · 313 阅读 · 0 评论 -
LeetCode:字符串总结
一、字符串转换整数8:请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)class Solution { public int myAtoi(String s) { char[] chars = s.toCharArray(); int length = chars.length; int index = 0; while(index&原创 2022-01-14 22:37:18 · 335 阅读 · 0 评论 -
LeetCode:图
207:你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。 在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则 必须 先学习课程 bi 。 例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1 。 请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false 。class S.原创 2022-01-14 22:33:58 · 278 阅读 · 0 评论 -
LeetCode:优先队列
179:给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。class Solution { public String largestNumber(int[] nums) { PriorityQueue<String> queue = new PriorityQueue<>((x,y)-> (y+x).compareTo(x+y)); for(int num:nums){原创 2022-01-14 22:32:47 · 311 阅读 · 0 评论 -
LeetCode:矩阵最长递增路径
329:给定一个m x n 整数矩阵matrix ,找出其中 最长递增路径 的长度。对于每个单元格,你可以往上,下,左,右四个方向移动。 你 不能 在 对角线 方向上移动或移动到 边界外(即不允许环绕)。class Solution { private int[][] memo; private int[][] matrix; public int longestIncreasingPath(int[][] matrix) { int m = matri..原创 2022-01-14 22:30:33 · 208 阅读 · 0 评论 -
LeetCode:有序矩阵搜索
一、搜索二维矩阵II240:编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性: 每行的元素从左到右升序排列。 每列的元素从上到下升序排列。class Solution { public boolean searchMatrix(int[][] matrix, int target) { int m = matrix.length; int n = matrix[0].length;原创 2022-01-14 22:02:48 · 270 阅读 · 0 评论 -
LeetCode:数组总结
一、三数之和15:给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> result = new LinkedList<>(原创 2022-01-07 21:59:42 · 412 阅读 · 0 评论 -
LeetCode:二叉树
一、二叉树锯齿形遍历103:给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { int depth = 0; Deque<TreeNode> queue = new LinkedList<>原创 2022-01-07 00:08:04 · 505 阅读 · 0 评论 -
LeetCode:动态规划 - 子序列问题
无原创 2022-08-09 23:08:00 · 129 阅读 · 0 评论 -
LeetCode:动态规划-打家劫舍
一、打家劫舍198:你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。class Solution { public int rob(int[] nums) { int length =nums.length; if原创 2022-01-07 14:24:13 · 403 阅读 · 0 评论 -
LeetCode:动态规划-背包问题
一、0/1背包1.1 方法总结有N件物品和⼀个最多能被重量为W 的背包。第 i 件物品的重量是weight[i],得到的价值是value[i]。每件物品只能⽤⼀次,求解将哪些物品装⼊背包里物品价值总和最大。dp[ j]:为容量为 j的背包所背的最大价值 dp[ j ] = max ( dp[ j ], dp[ j - weight[ i ] ] + value[ i ] )注意遍历顺序:1、先遍历物品再遍历背包容量2、倒序遍历背包容量,防止重复选取一个物品void test..原创 2022-01-04 00:10:26 · 927 阅读 · 0 评论 -
LeetCode:动态规划 - 题目总结
32:给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。class Solution { public int longestValidParentheses(String s) { int length = s.length(); if(length<2) return 0; //以s[i]个字符结尾的最长有效括号 int[] dp = new int.原创 2022-01-12 20:24:14 · 79 阅读 · 0 评论 -
LeetCode:动态规划-买卖股票
一、买卖股票121:你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润贪心:class Solution { public int maxProfit(int[] prices) { int length = prices.length; int min = prices[0]; int cur = 0; int max = 0; for原创 2022-01-06 21:58:05 · 325 阅读 · 0 评论 -
LeetCode:排序
一、快速排序lass Solution { public int[] sortArray(int[] nums) { quckSort(nums,0,nums.length-1); return nums; } public void quckSort(int[] nums,int left,int right){ if(left<right){ int mid = partition(nums,le原创 2022-01-06 21:12:10 · 292 阅读 · 0 评论 -
LeetCode:滑动窗口、双指针、单调栈
一、滑动窗口1.1 无重复字符的最长字串3:给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度。class Solution { public int lengthOfLongestSubstring(String s) { if(s.length()==0) return 0; HashMap<Character,Integer> map = new HashMap<>(); ....原创 2022-01-06 21:07:11 · 148 阅读 · 0 评论 -
LeetCode:回溯总结
一、组合:同一个元素不能重复使用1.1 组合77:给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。class Solution { LinkedList<List<Integer>> result = new LinkedList<>(); LinkedList<Integer> path = new LinkedList<>(); public List<List<原创 2022-01-03 21:42:49 · 182 阅读 · 0 评论 -
LeetCode:链表总结
一、反转链表1.1 从前往后遍历public ListNode reverseList(ListNode head) { ListNode cur = head; ListNode next = null; ListNode pre = null; while(cur!=null){ //cur不为空才求它的next next = cur.next; cur.next = pre; pre = cur;原创 2022-01-03 19:45:35 · 655 阅读 · 3 评论 -
算法:动态规划
1、确定dp数组以及下标的含义2、确定递推公式3、dp数组如何初始化4、确定遍历顺序5、举例推导dp数组原创 2021-12-03 14:13:45 · 186 阅读 · 0 评论