随笔分类 -  leetcode-数组

摘要:题目: 解答: 1 class Solution { 2 public: 3 int uniquePaths(int m, int n) 4 { 5 vector<vector<int>> dp(m,vector<int>(n)); 6 7 for (int i = 0; i < n; i++) 8 阅读全文
posted @ 2020-05-05 15:39 梦醒潇湘 阅读(134) 评论(0) 推荐(0)
摘要:题目: 解答: 1 /** 2 * Definition for an interval. 3 * struct Interval { 4 * int start; 5 * int end; 6 * Interval() : start(0), end(0) {} 7 * Interval(int 阅读全文
posted @ 2020-05-05 15:32 梦醒潇湘 阅读(240) 评论(0) 推荐(0)
摘要:题目: 解答: (1)如果某一个作为 起跳点 的格子可以跳跃的距离是 3,那么表示后面 3 个格子都可以作为 起跳点。(2)可以对每一个能作为 起跳点 的格子都尝试跳一次,把 能跳到最远的距离 不断更新。(3)如果可以一直跳到最后,就成功了。 1 class Solution { 2 public: 阅读全文
posted @ 2020-05-05 15:27 梦醒潇湘 阅读(183) 评论(0) 推荐(0)
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<vector<int>> generateMatrix(int n) 4 { 5 vector< vector<int> > matrix(n, vector<int>(n)); 6 if (n == 0) 阅读全文
posted @ 2020-05-05 15:24 梦醒潇湘 阅读(163) 评论(0) 推荐(0)
摘要:题目: 解答: 迭代法。 1 class Solution { 2 public: 3 4 vector<int> spiralOrder(vector<vector<int> >& matrix) 5 { 6 vector<int> result; 7 if (matrix.empty()) 8 阅读全文
posted @ 2020-05-05 15:23 梦醒潇湘 阅读(157) 评论(0) 推荐(0)
摘要:题目: 解答: 方法 1 :转置加翻转 最直接的想法是先转置矩阵,然后翻转每一行。这个简单的方法已经能达到最优的时间复杂度O(N^2)。 1 class Solution { 2 public void rotate(int[][] matrix) { 3 int n = matrix.length 阅读全文
posted @ 2020-05-05 15:14 梦醒潇湘 阅读(162) 评论(0) 推荐(0)
摘要:题目: 解答: 方法一: 方法二: 方法三 : 1 class Solution { 2 public: 3 int firstMissingPositive(vector<int>& nums) 4 { 5 for (int i = 0; i < nums.size(); i++) 6 { 7 w 阅读全文
posted @ 2020-05-05 15:02 梦醒潇湘 阅读(144) 评论(0) 推荐(0)
摘要:题目: 解答: 1 class Solution { 2 private: 3 vector<int> candidates; 4 vector<vector<int>> res; 5 vector<int> path; 6 public: 7 void DFS(int start, int tar 阅读全文
posted @ 2020-05-05 14:58 梦醒潇湘 阅读(151) 评论(0) 推荐(0)
摘要:题目: 解答: https://leetcode-cn.com/problems/combination-sum/solution/hui-su-suan-fa-jian-zhi-python-dai-ma-java-dai-m-2/ 1 class Solution { 2 private: 3 阅读全文
posted @ 2020-05-05 14:55 梦醒潇湘 阅读(166) 评论(0) 推荐(0)
摘要:题目: 解答: 1 class Solution { 2 public: 3 int threeSumClosest(vector<int>& nums, int target) 4 { 5 sort(nums.begin(),nums.end()); 6 7 int ans = nums[0] + 阅读全文
posted @ 2020-05-05 14:51 梦醒潇湘 阅读(143) 评论(0) 推荐(0)
摘要:题目: 解答: 1 class Solution { 2 public: 3 int maxArea(vector<int>& height) 4 { 5 int i = 0; 6 int j = height.size() - 1; 7 int res = 0; 8 9 while(i < j) 阅读全文
posted @ 2020-05-05 14:42 梦醒潇湘 阅读(149) 评论(0) 推荐(0)
摘要:题目: 解答: 从最末位寻找第一个破坏升序的数nums[pos - 1], 然后在遍历过的数里寻找比该数大的最小的一个数。如遍历过的数为[7,6,4,3], nums[pos - 1]为5, 则5需要与6进行交换, 在将[7,5,4,3]改为升序(这里使用reverse)。 遍历过的数从后往前一定是 阅读全文
posted @ 2020-05-04 19:31 梦醒潇湘 阅读(193) 评论(0) 推荐(0)
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<vector<int>> threeSum(vector<int>& nums) 4 { 5 sort(nums.begin(), nums.end()); 6 7 int N = nums.size(); 阅读全文
posted @ 2020-05-04 19:25 梦醒潇湘 阅读(195) 评论(0) 推荐(0)
摘要:题目: 解答: 将A的从低位(尾部数字)与K相加,同时K每次都要丢弃个位数字,然后与A的下一个数字相加(此思路参考评论区大佬做的)。 1 class Solution { 2 public: 3 vector<int> addToArrayForm(vector<int>& A, int K) 4 阅读全文
posted @ 2020-05-04 18:52 梦醒潇湘 阅读(241) 评论(0) 推荐(0)
摘要:题目: 解答: lef和rig分别指向左右的数,比较并从最大位开始装。 1 class Solution { 2 public: 3 vector<int> sortedSquares(vector<int>& A) 4 { 5 int len = A.size(); 6 vector<int> a 阅读全文
posted @ 2020-05-04 18:49 梦醒潇湘 阅读(147) 评论(0) 推荐(0)
摘要:题目: 解答: 方法一:线性扫描。 我们从数组的最左侧开始扫描,直到找到第一个不满足 A[i] < A[i + 1] 的 i,那么 i 就是这个数组的最高点。如果 i = 0 或者不存在这样的 i(即整个数组都是单调递增的),那么就返回 false。否则从 i 开始继续扫描,判断接下来的的位置 j 阅读全文
posted @ 2020-05-04 18:46 梦醒潇湘 阅读(235) 评论(0) 推荐(0)
摘要:题目: 解答: 方法一:双指针。 在找到一个偶数位是奇数的前提下,找奇数位上的偶数,找到之后在交换。 1 class Solution { 2 public: 3 vector<int> sortArrayByParityII(vector<int>& A) 4 { 5 int j = 1; 6 f 阅读全文
posted @ 2020-05-04 18:44 梦醒潇湘 阅读(184) 评论(0) 推荐(0)
摘要:题目: 解答: 设置双指针 如果右指针 r 是奇数,指针往右走 否则与左指针 l 交换一次 移动左指针 l 1 class Solution { 2 public: 3 vector<int> sortArrayByParity(vector<int>& A) 4 { 5 int l = 0; 6 阅读全文
posted @ 2020-05-04 18:41 梦醒潇湘 阅读(356) 评论(0) 推荐(0)
摘要:题目: 解答: 如果数组单调则所有相邻两值的差必须都同号。 1 class Solution { 2 public: 3 bool isMonotonic(vector<int>& A) 4 { 5 //两相邻值的差 6 int sub=0; 7 for (int i = 1; i < A.size 阅读全文
posted @ 2020-05-04 18:39 梦醒潇湘 阅读(217) 评论(0) 推荐(0)
摘要:题目: 解答: 思路:滑动窗口。 每个(连续)增加的子序列是不相交的,并且每当 nums[i-1]>=nums[i] 时,每个此类子序列的边界都会出现。当它这样做时,它标志着在 nums[i] 处开始一个新的递增子序列,我们将这样的 i 存储在变量 anchor 中。例如,如果 nums=[7,8, 阅读全文
posted @ 2020-05-04 18:27 梦醒潇湘 阅读(209) 评论(0) 推荐(0)