JinFei's Blog

Thinking will not overcome fear but action will.

[LeetCode]LCP 28. 采购方案

Two Sum变形

题目描述 小力将 N 个零件的报价存于数组 nums。小力预算为 target,假定小力仅购买两个零件,要求购买零件的花费不超过预算,请问他有多少种采购方案。 注意:答案需要以 1e9 + 7 (1000000007) 为底取模,如:计算初始结果为:1000000008,请返回 1 Example 1: 输入:nums = [2,5,3,5], target = 6 输出:1...

[LeetCode]931. 下降路径最小和

路径和

题目描述 给你一个 n x n 的 方形 整数数组 matrix ,请你找出并返回通过 matrix 的下降路径 的 最小和 。 下降路径 可以从第一行中的任何元素开始,并从每一行中选择一个元素。在下一行选择的元素和当前行所选元素最多相隔一列(即位于正下方或者沿对角线向左或者向右的第一个元素)。具体来说,位置 (row, col) 的下一个元素应当是 (row + 1, col -...

[LeetCode]797. 所有可能的路径

类似于字符串的全遍历

题目描述 给你一个有 n 个节点的 有向无环图(DAG),请你找出所有从节点 0 到节点 n-1 的路径并输出(不要求按特定顺序) 二维数组的第 i 个数组中的单元都表示有向图中 i 号节点所能到达的下一些节点,空就是没有下一个结点了。 译者注:有向图是有方向的,即规定了 a→b 你就不能从 b→a 。 Example 1: 输入:graph = [[1,2],[3],...

[LeetCode]787. K 站中转内最便宜的航班

找路径使得航班价格最低

题目描述 有 n 个城市通过一些航班连接。给你一个数组 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示该航班都从城市 fromi 开始,以价格 pricei 抵达 toi。 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是找到出一条最多经过 k 站中转的路线,使得从 src 到 dst 的 价格最便宜 ...

[LeetCode]1981. 最小化目标值与所选元素的差

找到与给定的值相差最少的路径

题目描述 给你一个大小为 m x n 的整数矩阵 mat 和一个整数 target 。 从矩阵的 每一行 中选择一个整数,你的目标是 最小化 所有选中元素之 和 与目标值 target 的 绝对差 。 返回 最小的绝对差 。 a 和 b 两数字的 绝对差 是 a - b 的绝对值。 Example 1: 输入:mat = [[1,2,3],[4,5,6],[7,8,9]],...

[LeetCode]1289. 下降路径最小和 II

路径和

题目描述 给你一个整数方阵 arr ,定义「非零偏移下降路径」为:从 arr 数组中的每一行选择一个数字,且按顺序选出来的数字中,相邻数字不在原数组的同一列。 请你返回非零偏移下降路径数字和的最小值。 Example 1: 输入:arr = [[1,2,3],[4,5,6],[7,8,9]] 输出:13 解释: 所有非零偏移下降路径包括: [1,5,9], [1,5,7], ...

[LeetCode]537. Complex Number Multiplication

复数相乘

题目描述 A complex number can be represented as a string on the form “real+imaginaryi” where: real is the real part and is an integer in the range [-100, 100]. imaginary is the imaginary part...

[LeetCode]Two Sum IV - Input is a BST

Two sum变形

题目描述 Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: roo...

[LeetCode]215. Kth Largest Element in an Array

快速排序

题目描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1: Input: [3,2,1,5,6,4] and k = 2 ...

[LeetCode]1980. Find Unique Binary String

查找独一无二的字符串

题目描述 Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may r...