摘要: 题目: 解答: 1 class Solution { 2 public: 3 string minNumber(vector<int>& nums) 4 { 5 // 选取使字符串组合更小的排序规则 6 auto compare = [](string sa, string sb){return s 阅读全文
posted @ 2020-05-05 21:54 梦醒潇湘 阅读(175) 评论(0) 推荐(0)
摘要: 题目: 解答: C++实现起来用priority_queue容器,默认从小到大排序,底层实现为最大堆。 1 class Solution { 2 public: 3 vector<int> smallestK(vector<int>& arr, int k) 4 { 5 vector<int> re 阅读全文
posted @ 2020-05-05 21:51 梦醒潇湘 阅读(161) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Trie { 2 private: 3 bool isEnd; 4 Trie* next[26]; 5 public: 6 Trie() 7 { 8 isEnd = false; 9 memset(next, 0, sizeof(next)); 10 } 11 12 阅读全文
posted @ 2020-05-05 21:44 梦醒潇湘 阅读(198) 评论(0) 推荐(0)
摘要: 题目: 解答: 采用双栈的办法以空间换时间,栈s1作为普通的数据栈,栈s2用来保存每次入栈时的较小值,这样s2栈顶的值总是栈中元素的最小值。 1 class MinStack { 2 public: 3 /** initialize your data structure here. */ 4 Mi 阅读全文
posted @ 2020-05-05 21:40 梦醒潇湘 阅读(131) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int maxProduct(vector<int>& nums) 4 { 5 int max = INT_MIN; 6 int imax = 1; 7 int imin = 1; 8 9 for (int i = 0;i 阅读全文
posted @ 2020-05-05 21:36 梦醒潇湘 阅读(198) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 // 总的思想就是 哈希双向链表 2 struct Node 3 { 4 int key; 5 int value; 6 Node* pre; 7 Node* next; 8 // 构造函数初始化 9 Node(int key, int value) : key(key), va 阅读全文
posted @ 2020-05-05 21:31 梦醒潇湘 阅读(153) 评论(0) 推荐(0)
摘要: 题目: 解答: 异或操作。 1 class Solution { 2 public: 3 int singleNumber(vector<int>& nums) 4 { 5 int ans = 0; 6 for (int i = 0; i < nums.size(); i++) 7 { 8 ans 阅读全文
posted @ 2020-05-05 21:24 梦醒潇湘 阅读(146) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int minDistance(string word1, string word2) 4 { 5 vector<vector<int>> dp(word1.size() + 1, vector<int>(word2.si 阅读全文
posted @ 2020-05-05 21:16 梦醒潇湘 阅读(160) 评论(0) 推荐(0)
摘要: 题目: 解答: d[1] = 1; d[2] = 2; 再根据公式d[i] = d[i-1] + d[i-2]; 1 class Solution { 2 public: 3 int climbStairs(int n) 4 { 5 if (n<=2) 6 { 7 return n; 8 } 9 1 阅读全文
posted @ 2020-05-05 21:09 梦醒潇湘 阅读(160) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 vector<string> Permutation(string str) 4 { 5 vector<string> result; 6 if(str.empty()) return result; 7 8 Permut 阅读全文
posted @ 2020-05-05 21:07 梦醒潇湘 阅读(164) 评论(0) 推荐(0)
摘要: 题目: 解答: 图解: 1 class Solution { 2 public: 3 int trap(vector<int>& height) 4 { 5 int ans = 0; 6 stack<int> st; 7 for (int i = 0; i < height.size(); i++) 阅读全文
posted @ 2020-05-05 21:04 梦醒潇湘 阅读(154) 评论(0) 推荐(0)
只有注册用户登录后才能阅读该文。 阅读全文
posted @ 2020-05-05 20:54 梦醒潇湘 阅读(0) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 vector<string> getValidT9Words(string num, vector<string>& words) 4 { 5 vector<string> svec; 6 string letters[1 阅读全文
posted @ 2020-05-05 20:29 梦醒潇湘 阅读(189) 评论(0) 推荐(0)
摘要: 题目: 解答: 当我们加上一个正数的时候,和会增加;当我们加上一个负数的时候,和会减少。如果当前得到的和是个负数,那么这个和接下来的累加中应该抛弃并重新清零,不然的话,这个负数将会减少接下来的和。 1 class Solution { 2 public: 3 int maxSubArray(vect 阅读全文
posted @ 2020-05-05 19:27 梦醒潇湘 阅读(201) 评论(0) 推荐(0)
摘要: 题目: 解答: 默认升序(降序也只是改一点代码,不影响) 原理:如果左侧最大值大于中间的最小值,则一定会被中间序列包括;同理,如果右侧最小值大于中间的最大值,则一定会被中间序列包括。 一遍遍历 + 两个指针(两次扫描可一次遍历完成) 1、从前向后扫描数组,判断当前array[i]是否比max小,是则 阅读全文
posted @ 2020-05-05 19:23 梦醒潇湘 阅读(274) 评论(0) 推荐(0)
摘要: 题目: 解答: 先排序,然后设定返回值为最大,用双指针求得结果。 1 class Solution { 2 public: 3 int smallestDifference(vector<int>& a, vector<int>& b) 4 { 5 sort(a.begin(),a.end()); 阅读全文
posted @ 2020-05-05 19:11 梦醒潇湘 阅读(211) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 void merge(vector<int>& A, int m, vector<int>& B, int n) 4 { 5 int len1 = m - 1; 6 int len2 = n - 1; 7 int len 阅读全文
posted @ 2020-05-05 18:55 梦醒潇湘 阅读(189) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 vector<vector<int>> res; 4 5 vector<vector<int>> subsets(vector<int>& nums) 6 { 7 // 记录走过的路径 8 vector<int> trac 阅读全文
posted @ 2020-05-05 18:45 梦醒潇湘 阅读(167) 评论(0) 推荐(0)
摘要: 题目: 解答: 方法一:会超时间 1 class Solution { 2 public: 3 void rotate(vector<vector<int>>& matrix) 4 { 5 // 思路是: 转置 + 反转每一行 6 7 int len = matrix.size(); 8 9 // 阅读全文
posted @ 2020-05-05 18:40 梦醒潇湘 阅读(186) 评论(0) 推荐(0)
摘要: 题目: 解答: 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 阅读全文
posted @ 2020-05-05 18:04 梦醒潇湘 阅读(155) 评论(0) 推荐(0)
摘要: 题目: 解答: (1)首先计算数组 A 中所有数字总和 sum; (2)遍历数组 A 查找和为 sum / 3的子数组个数; (3)如果找到了三个这样的子数组则返回 true (4)找不到三个就返回 false; 1 class Solution { 2 public: 3 bool canThre 阅读全文
posted @ 2020-05-05 17:52 梦醒潇湘 阅读(273) 评论(0) 推荐(0)
摘要: 题目: 解答: 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 阅读全文
posted @ 2020-05-05 17:48 梦醒潇湘 阅读(140) 评论(0) 推荐(0)
摘要: 题目: 解答: 动态规划。 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 阅读全文
posted @ 2020-05-05 17:30 梦醒潇湘 阅读(205) 评论(0) 推荐(0)
摘要: 题目: 解答: 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 = 阅读全文
posted @ 2020-05-05 17:26 梦醒潇湘 阅读(190) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 先将数每一位拆成数组2,若数组为非升序序列,则直接返回原数即可3,否则,就找到数组中第一次出现升序的位置,从该位置往后找到最后一个最大值max_val及其下标max_ind4,从数组头开始找第一个比max_val小的数的下标i,交换i与max_ind位置的数即可 1 class 阅读全文
posted @ 2020-05-05 17:24 梦醒潇湘 阅读(180) 评论(0) 推荐(0)
摘要: 题目: 解答: 单调栈 正向遍历,单调递增栈,找出自始至终没有出栈的最大索引 l 反向遍历,单调递减栈,找出自始至终没有出栈的最小索引 r 中间就是需要排序的最小子数组 1 class Solution { 2 public: 3 int findUnsortedSubarray(vector<in 阅读全文
posted @ 2020-05-05 17:19 梦醒潇湘 阅读(146) 评论(0) 推荐(0)
摘要: 题目: 解答: 可以在考虑不同的 endend 的同时直接找到总和,而不是考虑所有 startstart 和 endend 然后找到对应的每个子数组的总和。 我们可以选择一个特定的 start,同时迭代 end,我们可以将对应于 end 的元素添加到到目前为止形成的总和中。当 sum 等于所需的 k 阅读全文
posted @ 2020-05-05 17:14 梦醒潇湘 阅读(235) 评论(0) 推荐(0)
摘要: 题目: 解答: 利用题目中所给信息 1 ≤ a[i] ≤ n ,将出现过的数字作为数组的index(访问元素时需要减一),如果出现一次的,将该index下的数取相反数,记录此时的状态,如果值为index的数字再出现一次(此时index索引的值为负数),那么这个数字就出现了两次。 比如 数组 [2,2 阅读全文
posted @ 2020-05-05 17:10 梦醒潇湘 阅读(281) 评论(0) 推荐(0)
摘要: 题目: 解答: 此题的正确解法是利用到了一个一维数组和一个 HashMap,其中数组用来保存数字,HashMap 用来建立每个数字和其在数组中的位置之间的映射。 插入操作——先看这个数字是否已经在 HashMap 中存在,如果存在的话直接返回 false,不存在的话,将其插入到数组的末尾,然后建立数 阅读全文
posted @ 2020-05-05 17:08 梦醒潇湘 阅读(233) 评论(0) 推荐(0)
摘要: 题目: 解答: 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> 阅读全文
posted @ 2020-05-05 17:03 梦醒潇湘 阅读(187) 评论(0) 推荐(0)
摘要: 题目: 解答: 就很简单的遍历一遍...中间判断数字是否连续。 1 class Solution { 2 public: 3 vector<string> summaryRanges(vector<int>& nums) 4 { 5 vector<string> ans; 6 for(int i = 阅读全文
posted @ 2020-05-05 16:56 梦醒潇湘 阅读(151) 评论(0) 推荐(0)
摘要: 题目: 解答: 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 阅读全文
posted @ 2020-05-05 16:24 梦醒潇湘 阅读(180) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 vector<vector<int>> res; 4 5 vector<vector<int>> subsetsWithDup(vector<int>& nums) 6 { 7 // 为啥排序啊?? 8 sort(nums 阅读全文
posted @ 2020-05-05 16:15 梦醒潇湘 阅读(126) 评论(0) 推荐(0)
摘要: 题目: 解答: 回溯方法: 1 class Solution { 2 public: 3 vector<vector<int>> res; 4 5 vector<vector<int>> subsets(vector<int>& nums) 6 { 7 // 记录走过的路径 8 vector<int 阅读全文
posted @ 2020-05-05 16:05 梦醒潇湘 阅读(187) 评论(0) 推荐(0)
摘要: 题目: 解答: (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 阅读全文
posted @ 2020-05-05 15:56 梦醒潇湘 阅读(156) 评论(0) 推荐(0)
摘要: 题目: 解答: A. 先确定第一行和第一列是否需要清零; B. 扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0; C. 根据第一行和第一列的信息,已经可以将剩下的矩阵元素赋值为结果所需的值了; D. 根据1中确定的状态,处理第一行和第一列; 1 class Solutio 阅读全文
posted @ 2020-05-05 15:53 梦醒潇湘 阅读(212) 评论(0) 推荐(0)
摘要: 题目: 解答: 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 阅读全文
posted @ 2020-05-05 15:48 梦醒潇湘 阅读(198) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) 4 { 5 int m = obstacleGrid.size(); 6 int n = ob 阅读全文
posted @ 2020-05-05 15:44 梦醒潇湘 阅读(108) 评论(0) 推荐(0)
摘要: 题目: 解答: 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 梦醒潇湘 阅读(139) 评论(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 梦醒潇湘 阅读(252) 评论(0) 推荐(0)
摘要: 题目: 解答: (1)如果某一个作为 起跳点 的格子可以跳跃的距离是 3,那么表示后面 3 个格子都可以作为 起跳点。(2)可以对每一个能作为 起跳点 的格子都尝试跳一次,把 能跳到最远的距离 不断更新。(3)如果可以一直跳到最后,就成功了。 1 class Solution { 2 public: 阅读全文
posted @ 2020-05-05 15:27 梦醒潇湘 阅读(201) 评论(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 梦醒潇湘 阅读(174) 评论(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 梦醒潇湘 阅读(166) 评论(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 梦醒潇湘 阅读(179) 评论(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 梦醒潇湘 阅读(155) 评论(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 梦醒潇湘 阅读(161) 评论(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 梦醒潇湘 阅读(175) 评论(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 梦醒潇湘 阅读(155) 评论(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 梦醒潇湘 阅读(155) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int missingNumber(vector<int>& nums) 4 { 5 int i = 0; 6 int j = nums.size() - 1; 7 8 while(i <= j) 9 { 10 int m 阅读全文
posted @ 2020-05-05 14:34 梦醒潇湘 阅读(283) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int search(vector<int>& nums, int target) 4 { 5 int left = left_bound(nums, target); 6 int right = right_bound( 阅读全文
posted @ 2020-05-05 14:28 梦醒潇湘 阅读(299) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int minArray(vector<int>& nums) 4 { 5 int left = 0; 6 int right = nums.size() - 1; /* 左闭右闭区间,如果用右开区间则不方便判断右值 */ 阅读全文
posted @ 2020-05-05 14:24 梦醒潇湘 阅读(158) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 bool searchMatrix(vector<vector<int>>& matrix, int target) 4 { 5 int i = matrix.size() - 1; 6 int j = 0; 7 8 // 阅读全文
posted @ 2020-05-05 14:21 梦醒潇湘 阅读(253) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int findString(vector<string>& words, string s) 4 { 5 int left = 0; 6 int right = words.size() - 1; 7 8 while ( 阅读全文
posted @ 2020-05-05 14:18 梦醒潇湘 阅读(214) 评论(0) 推荐(0)
摘要: 题目: 解答: 这个题目真是打了各种补丁啊。。。。 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 whil 阅读全文
posted @ 2020-05-05 14:08 梦醒潇湘 阅读(240) 评论(0) 推荐(0)
摘要: 题目: 解答: 假如不存在重复元素的话,则二分法很好实现,但此题存在重复元素,所以会比较复杂一点。 举例: 对于 [0, 1, 4, 4, 4] 这个数组,使用二分法拿出位于中间的 idx 为 2 的数字 4,然后我们发现 nums[idx] > idx; 此刻我们除了可以确认 idx2 不是魔术索 阅读全文
posted @ 2020-05-05 14:00 梦醒潇湘 阅读(251) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int smallestDivisor(vector<int>& nums, int threshold) 4 { 5 int l = 1, r = *max_element(nums.begin(), nums.end( 阅读全文
posted @ 2020-05-05 13:54 梦醒潇湘 阅读(178) 评论(0) 推荐(0)
摘要: 题目: 解答: 方法一: 1 class Solution { 2 public: 3 vector<int> findClosestElements(vector<int>& arr, int k, int x) 4 { 5 int size = arr.size(); 6 7 int left 阅读全文
posted @ 2020-05-05 13:44 梦醒潇湘 阅读(323) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 /** 2 * Forward declaration of guess API. 3 * @param num your guess 4 * @return -1 if num is lower than the guess number 5 * 1 if num is hig 阅读全文
posted @ 2020-05-05 13:34 梦醒潇湘 阅读(204) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int lengthOfLIS(vector<int>& nums) 4 { 5 int n = (int)nums.size(); 6 7 if (n == 0) 8 { 9 return 0; 10 } 11 vect 阅读全文
posted @ 2020-05-05 13:29 梦醒潇湘 阅读(249) 评论(0) 推荐(0)
摘要: 题目: 解答: 方法一:排序。 如果对数字进行排序,则任何重复的数字都将与排序后的数组相邻。 方法二:集合。 如果我们在数组遍历时存储每个元素,我们可以在数组迭代过程中检查每个元素。 方法三:二进制 方法四:二分查找 int findDuplicate(vector<int> &nums) { in 阅读全文
posted @ 2020-05-05 13:26 梦醒潇湘 阅读(617) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 // The API isBadVersion is defined for you. 2 // bool isBadVersion(int version); 3 4 class Solution { 5 public: 6 int firstBadVersion(int n) 阅读全文
posted @ 2020-05-05 13:17 梦醒潇湘 阅读(147) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int hIndex(vector<int>& citations) 4 { 5 int n = citations.size(); 6 7 int pivot; 8 int left = 0; 9 int right = 阅读全文
posted @ 2020-05-05 13:13 梦醒潇湘 阅读(169) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 bool searchMatrix(vector<vector<int>>& matrix, int target) 4 { 5 int i = matrix.size()-1; 6 int j=0; 7 while(i 阅读全文
posted @ 2020-05-05 13:06 梦醒潇湘 阅读(157) 评论(0) 推荐(0)
摘要: 题目: 解答: 方法一:暴力法 按照题目要求直接求。把所有可能的子数组求和并更新ans ,直到我们找到最优子数组且和满足 sum≥s 。 (1)初始化ans = INT_MAX; (2)用变量i从左到右遍历数组: A. 用变量j从当前元素到数组尾部遍历: a. 将i到j这些元素求和得到sum b. 阅读全文
posted @ 2020-05-05 13:01 梦醒潇湘 阅读(227) 评论(0) 推荐(0)
摘要: 题目: 解答: 方法一:线性扫描 本方法利用了连续的两个元素 nums[j]和 nums[j + 1]不会相等这一事实。于是,我们可以从头开始遍历 nums数组。每当我们遇到数字 nums[i],只需要检查它是否大于下一个元素 nums[i+1] 即可判断 nums[i]是否是峰值。 1 publi 阅读全文
posted @ 2020-05-05 12:50 梦醒潇湘 阅读(360) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int findMin(vector<int>& nums) 4 { 5 int left = 0; 6 int right = nums.size() - 1; /* 左闭右闭区间,如果用右开区间则不方便判断右值 */ 阅读全文
posted @ 2020-05-05 12:44 梦醒潇湘 阅读(140) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 int findMin(vector<int>& nums) 4 { 5 int left = 0; 6 int right = nums.size() - 1; /* 左闭右闭区间,如果用右开区间则不方便判断右值 */ 阅读全文
posted @ 2020-05-05 12:40 梦醒潇湘 阅读(205) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 bool search(vector<int>& nums, int target) 4 { 5 int l = 0; 6 int r = nums.size() -1; 7 8 while (l <= r) 9 { 10 阅读全文
posted @ 2020-05-05 12:35 梦醒潇湘 阅读(153) 评论(0) 推荐(0)
摘要: 题目: 解答: 按杨氏矩阵的方法求解,时间复杂度为O(m+n),其中m为矩阵的行数,n为矩阵的列数。 1 class Solution { 2 public: 3 bool searchMatrix(vector<vector<int>>& matrix, int target) 4 { 5 if( 阅读全文
posted @ 2020-05-05 12:18 梦醒潇湘 阅读(299) 评论(0) 推荐(0)
摘要: 题目: 解答: 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 阅读全文
posted @ 2020-05-05 12:04 梦醒潇湘 阅读(196) 评论(0) 推荐(0)
摘要: 题目: 解答: 1 class Solution { 2 public: 3 vector<int> searchRange(vector<int>& nums, int target) 4 { 5 vector<int> range(2, -1); 6 7 range[0] = left_boun 阅读全文
posted @ 2020-05-05 11:59 梦醒潇湘 阅读(256) 评论(0) 推荐(0)
摘要: 题目: 解答: 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 阅读全文
posted @ 2020-05-05 11:38 梦醒潇湘 阅读(157) 评论(0) 推荐(0)
摘要: 题目: 解答: 方法一: 简单粗暴,先将两个数组合并,两个有序数组的合并也是归并排序中的一部分。然后根据奇数,还是偶数,返回中位数。 1 class Solution { 2 public: 3 double findMedianSortedArrays(vector<int>& nums1, ve 阅读全文
posted @ 2020-05-05 11:30 梦醒潇湘 阅读(423) 评论(0) 推荐(0)
摘要: 题目: 解答: 下面的方法基于这样一个事实:任何一个整数都可以表示成一个多项式的形式,例如,5 = 2^2 + 2^0,78=2^5 + 2^3 + 2^2 + 2^1等等,任何数字都可以表示上述形式。 我们使除数divisor乘以2(diveisor<<1),直到它达到或大于被除数的一半(如果我们 阅读全文
posted @ 2020-05-05 11:24 梦醒潇湘 阅读(311) 评论(0) 推荐(0)