上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 38 下一页
摘要: 题目: 解答: 假如不存在重复元素的话,则二分法很好实现,但此题存在重复元素,所以会比较复杂一点。 举例: 对于 [0, 1, 4, 4, 4] 这个数组,使用二分法拿出位于中间的 idx 为 2 的数字 4,然后我们发现 nums[idx] > idx; 此刻我们除了可以确认 idx2 不是魔术索 阅读全文
posted @ 2020-05-05 14:00 梦醒潇湘 阅读(232) 评论(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 梦醒潇湘 阅读(154) 评论(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 梦醒潇湘 阅读(305) 评论(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 梦醒潇湘 阅读(188) 评论(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 梦醒潇湘 阅读(245) 评论(0) 推荐(0)
摘要: 题目: 解答: 方法一:排序。 如果对数字进行排序,则任何重复的数字都将与排序后的数组相邻。 方法二:集合。 如果我们在数组遍历时存储每个元素,我们可以在数组迭代过程中检查每个元素。 方法三:二进制 方法四:二分查找 int findDuplicate(vector<int> &nums) { in 阅读全文
posted @ 2020-05-05 13:26 梦醒潇湘 阅读(600) 评论(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 梦醒潇湘 阅读(134) 评论(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 梦醒潇湘 阅读(159) 评论(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 梦醒潇湘 阅读(142) 评论(0) 推荐(0)
摘要: 题目: 解答: 方法一:暴力法 按照题目要求直接求。把所有可能的子数组求和并更新ans ,直到我们找到最优子数组且和满足 sum≥s 。 (1)初始化ans = INT_MAX; (2)用变量i从左到右遍历数组: A. 用变量j从当前元素到数组尾部遍历: a. 将i到j这些元素求和得到sum b. 阅读全文
posted @ 2020-05-05 13:01 梦醒潇湘 阅读(217) 评论(0) 推荐(0)
上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 38 下一页