随笔分类 -  leetcode-数组

摘要:题目: 解答: 思路: 一次遍历找到最大的数max1和第二大的数max2,然后看看最大的数是不是大于等于第二大的数的两倍。如果是的话那么肯定满足最大数max1大于等于数组中其他数组的两倍了。 1 class Solution { 2 public: 3 int dominantIndex(vecto 阅读全文
posted @ 2020-05-20 11:21 梦醒潇湘 阅读(192) 评论(0) 推荐(0)
摘要:题目: 解答: sumleft + num[i] + sumright = totalsum sumleft = sumright > 2 * sumleft = totalsum - num[i] 1 class Solution { 2 public: 3 4 // sumleft + nums 阅读全文
posted @ 2020-05-20 11:19 梦醒潇湘 阅读(445) 评论(0) 推荐(0)
摘要:题目: 解答: 1 class Solution { 2 public: 3 bool increasingTriplet(vector<int>& nums) 4 { 5 int len = nums.size(); 6 7 if (len < 3) 8 { 9 return false; 10 阅读全文
posted @ 2020-05-16 13:35 梦醒潇湘 阅读(222) 评论(0) 推荐(0)
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<int> intersect(vector<int>& nums1, vector<int>& nums2) 4 { 5 if (nums1.size() > nums2.size()) 6 { 7 retu 阅读全文
posted @ 2020-05-16 13:30 梦醒潇湘 阅读(232) 评论(0) 推荐(0)
摘要:题目: 解答: 一般来说,对于一个问题我通常会给出两种以上的解法,但是对于洗牌问题,Fisher-Yates洗牌算法即是通俗解法,同时也是渐进最优的解法。 1 class Solution { 2 public: 3 4 vector<int> nums; 5 vector<int> copy; 6 阅读全文
posted @ 2020-05-16 13:18 梦醒潇湘 阅读(212) 评论(0) 推荐(0)
摘要:题目: 无序数组求中位数。 解答: 利用快排的思想 1、先进行一趟快排,使得div左边的值都比arr[div]小,div右边的值都比arr[div]大,但是这个div的位置是不确定的,可能位于中间,也可能偏左或者偏右。 2、计算出mid所在的下标,如果是奇数则是mid=(size+1)/2,如果是偶 阅读全文
posted @ 2020-05-11 11:36 梦醒潇湘 阅读(1233) 评论(0) 推荐(0)
摘要:题目: 解答: 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 梦醒潇湘 阅读(161) 评论(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 梦醒潇湘 阅读(149) 评论(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 梦醒潇湘 阅读(187) 评论(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 梦醒潇湘 阅读(148) 评论(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 梦醒潇湘 阅读(136) 评论(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 梦醒潇湘 阅读(147) 评论(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 梦醒潇湘 阅读(152) 评论(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 梦醒潇湘 阅读(145) 评论(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 梦醒潇湘 阅读(174) 评论(0) 推荐(0)
摘要:题目: 解答: 当我们加上一个正数的时候,和会增加;当我们加上一个负数的时候,和会减少。如果当前得到的和是个负数,那么这个和接下来的累加中应该抛弃并重新清零,不然的话,这个负数将会减少接下来的和。 1 class Solution { 2 public: 3 int maxSubArray(vect 阅读全文
posted @ 2020-05-05 19:27 梦醒潇湘 阅读(190) 评论(0) 推荐(0)
摘要:题目: 解答: 默认升序(降序也只是改一点代码,不影响) 原理:如果左侧最大值大于中间的最小值,则一定会被中间序列包括;同理,如果右侧最小值大于中间的最大值,则一定会被中间序列包括。 一遍遍历 + 两个指针(两次扫描可一次遍历完成) 1、从前向后扫描数组,判断当前array[i]是否比max小,是则 阅读全文
posted @ 2020-05-05 19:23 梦醒潇湘 阅读(260) 评论(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 梦醒潇湘 阅读(196) 评论(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 梦醒潇湘 阅读(177) 评论(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 梦醒潇湘 阅读(162) 评论(0) 推荐(0)