题目: 解答: 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> ...
分类:
编程语言 时间:
2020-05-05 18:04:21
阅读次数:
56
题目: 解答: 就很简单的遍历一遍...中间判断数字是否连续。 1 class Solution { 2 public: 3 vector<string> summaryRanges(vector<int>& nums) 4 { 5 vector<string> ans; 6 for(int i = ...
分类:
编程语言 时间:
2020-05-05 18:02:35
阅读次数:
60
题目: 解答: 利用题目中所给信息 1 ≤ a[i] ≤ n ,将出现过的数字作为数组的index(访问元素时需要减一),如果出现一次的,将该index下的数取相反数,记录此时的状态,如果值为index的数字再出现一次(此时index索引的值为负数),那么这个数字就出现了两次。 比如 数组 [2,2 ...
分类:
编程语言 时间:
2020-05-05 17:58:46
阅读次数:
53
题目: 解答: 单调栈 正向遍历,单调递增栈,找出自始至终没有出栈的最大索引 l 反向遍历,单调递减栈,找出自始至终没有出栈的最小索引 r 中间就是需要排序的最小子数组 1 class Solution { 2 public: 3 int findUnsortedSubarray(vector<in ...
分类:
编程语言 时间:
2020-05-05 17:49:42
阅读次数:
56
题目: 解答: 此题的正确解法是利用到了一个一维数组和一个 HashMap,其中数组用来保存数字,HashMap 用来建立每个数字和其在数组中的位置之间的映射。 插入操作——先看这个数字是否已经在 HashMap 中存在,如果存在的话直接返回 false,不存在的话,将其插入到数组的末尾,然后建立数 ...
分类:
编程语言 时间:
2020-05-05 17:38:33
阅读次数:
80
题目: 解答: 方法一: 方法二: 方法三 : 1 class Solution { 2 public: 3 int firstMissingPositive(vector<int>& nums) 4 { 5 for (int i = 0; i < nums.size(); i++) 6 { 7 w ...
分类:
编程语言 时间:
2020-05-05 15:14:03
阅读次数:
65
题目: 解答: 1 class Solution { 2 public: 3 int searchInsert(vector<int> &nums, int target) 4 { 5 int left = 0; 6 int right = nums.size() - 1; 7 8 while(le ...
分类:
其他好文 时间:
2020-05-05 12:40:50
阅读次数:
49
题目: 解答: 按杨氏矩阵的方法求解,时间复杂度为O(m+n),其中m为矩阵的行数,n为矩阵的列数。 1 class Solution { 2 public: 3 bool searchMatrix(vector<vector<int>>& matrix, int target) 4 { 5 if( ...
分类:
其他好文 时间:
2020-05-05 12:37:47
阅读次数:
47
题目: 解答: 1 class Solution { 2 public: 3 int search(vector<int>& nums, int target) 4 { 5 int l = 0; 6 int r = nums.size() -1; 7 8 while (l <= r) 9 { 10 ...
分类:
编程语言 时间:
2020-05-05 12:36:15
阅读次数:
57
题目: 解答: lef和rig分别指向左右的数,比较并从最大位开始装。 1 class Solution { 2 public: 3 vector<int> sortedSquares(vector<int>& A) 4 { 5 int len = A.size(); 6 vector<int> a ...
分类:
编程语言 时间:
2020-05-04 19:50:58
阅读次数:
59