随笔分类 - leetcode-数组
摘要:题目: 解答: 方法一:会超时间 1 class Solution { 2 public: 3 void rotate(vector<vector<int>>& matrix) 4 { 5 // 思路是: 转置 + 反转每一行 6 7 int len = matrix.size(); 8 9 //
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 bool isUnique(string astr) 4 { 5 int mark = 0; 6 for (int i = 0; i < astr.size(); i++) 7 { 8 int move_bit = ast
阅读全文
摘要:题目: 解答: (1)首先计算数组 A 中所有数字总和 sum; (2)遍历数组 A 查找和为 sum / 3的子数组个数; (3)如果找到了三个这样的子数组则返回 true (4)找不到三个就返回 false; 1 class Solution { 2 public: 3 bool canThre
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<vector<int>> transpose(vector<vector<int>>& A) 4 { 5 int ro = A.size(); 6 int co = A[0].size(); 7 8 vect
阅读全文
摘要:题目: 解答: 动态规划。 1 class Solution { 2 public: 3 int findLength(vector<int>& A, vector<int>& B) 4 { 5 int ans = 0; 6 vector<vector<int>> memo(A.size() + 1
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int numSubarrayProductLessThanK(vector<int>& nums, int k) 4 { 5 if (k <= 1) 6 { 7 return 0; 8 } 9 10 int prod =
阅读全文
摘要:题目: 解答: 1 先将数每一位拆成数组2,若数组为非升序序列,则直接返回原数即可3,否则,就找到数组中第一次出现升序的位置,从该位置往后找到最后一个最大值max_val及其下标max_ind4,从数组头开始找第一个比max_val小的数的下标i,交换i与max_ind位置的数即可 1 class
阅读全文
摘要:题目: 解答: 单调栈 正向遍历,单调递增栈,找出自始至终没有出栈的最大索引 l 反向遍历,单调递减栈,找出自始至终没有出栈的最小索引 r 中间就是需要排序的最小子数组 1 class Solution { 2 public: 3 int findUnsortedSubarray(vector<in
阅读全文
摘要:题目: 解答: 可以在考虑不同的 endend 的同时直接找到总和,而不是考虑所有 startstart 和 endend 然后找到对应的每个子数组的总和。 我们可以选择一个特定的 start,同时迭代 end,我们可以将对应于 end 的元素添加到到目前为止形成的总和中。当 sum 等于所需的 k
阅读全文
摘要:题目: 解答: 利用题目中所给信息 1 ≤ a[i] ≤ n ,将出现过的数字作为数组的index(访问元素时需要减一),如果出现一次的,将该index下的数取相反数,记录此时的状态,如果值为index的数字再出现一次(此时index索引的值为负数),那么这个数字就出现了两次。 比如 数组 [2,2
阅读全文
摘要:题目: 解答: 此题的正确解法是利用到了一个一维数组和一个 HashMap,其中数组用来保存数字,HashMap 用来建立每个数字和其在数组中的位置之间的映射。 插入操作——先看这个数字是否已经在 HashMap 中存在,如果存在的话直接返回 false,不存在的话,将其插入到数组的末尾,然后建立数
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<int> productExceptSelf(vector<int>& nums) 4 { 5 int n = nums.size(); 6 7 //把向量output初始化为1 8 vector<int>
阅读全文
摘要:题目: 解答: 就很简单的遍历一遍...中间判断数字是否连续。 1 class Solution { 2 public: 3 vector<string> summaryRanges(vector<int>& nums) 4 { 5 vector<string> ans; 6 for(int i =
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) 4 { 5 if (nums.size() <= 2) 6 { 7 return nums.size(); 8 } 9 10 // Initi
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<vector<int>> res; 4 5 vector<vector<int>> subsetsWithDup(vector<int>& nums) 6 { 7 // 为啥排序啊?? 8 sort(nums
阅读全文
摘要:题目: 解答: 回溯方法: 1 class Solution { 2 public: 3 vector<vector<int>> res; 4 5 vector<vector<int>> subsets(vector<int>& nums) 6 { 7 // 记录走过的路径 8 vector<int
阅读全文
摘要:题目: 解答: (1)计数排序,时间复杂度为O(n),空间复杂度为O(n)。(2)三个指针的方式,时间复杂度为O(n),空间复杂度为O(1)。 用 i 记录0应该放的位置,j 记录2应该放的位置。 cur从0到j扫描, 遇到0,放在 i 位置,i 后移 遇到2,放在 j 位置,j 前移 遇到1,cu
阅读全文
摘要:题目: 解答: A. 先确定第一行和第一列是否需要清零; B. 扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0; C. 根据第一行和第一列的信息,已经可以将剩下的矩阵元素赋值为结果所需的值了; D. 根据1中确定的状态,处理第一行和第一列; 1 class Solutio
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int minPathSum(vector<vector<int>>& grid) 4 { 5 for(int i = 0; i < grid.size(); i++) 6 { 7 for(int j = 0; j < g
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) 4 { 5 int m = obstacleGrid.size(); 6 int n = ob
阅读全文

浙公网安备 33010602011771号