05 2020 档案
摘要:题目: 解答: 本题的简单解法: 1 class Solution { 2 public: 3 vector<int> printNumbers(int n) 4 { 5 vector<int> res; 6 7 if (n == 0) 8 { 9 return res; 10 } 11 12 //
阅读全文
摘要:题目: 解答: 思路: 一次遍历找到最大的数max1和第二大的数max2,然后看看最大的数是不是大于等于第二大的数的两倍。如果是的话那么肯定满足最大数max1大于等于数组中其他数组的两倍了。 1 class Solution { 2 public: 3 int dominantIndex(vecto
阅读全文
摘要:题目: 解答: sumleft + num[i] + sumright = totalsum sumleft = sumright > 2 * sumleft = totalsum - num[i] 1 class Solution { 2 public: 3 4 // sumleft + nums
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 bool isPowerOfThree(int n) 4 { 5 if (n < 1) 6 { 7 return false; 8 } 9 10 while (n % 3 == 0) 11 { 12 n /= 3; 13
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) 4 { 5 int res = 0; 6 while(n != 0) 7 { 8 res += n & 1; 9 n >>= 1; 10 } 11 return
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) 4 { 5 int res = 0; 6 for (int i = 0; i < 32; i++) 7 { 8 res = (res << 1) + (n
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 一般来说,对于一个问题我通常会给出两种以上的解法,但是对于洗牌问题,Fisher-Yates洗牌算法即是通俗解法,同时也是渐进最优的解法。 1 class Solution { 2 public: 3 4 vector<int> nums; 5 vector<int> copy; 6
阅读全文
摘要:题目: 解答: 说实话,我自己是没有想出来这道题要用到动态规划的思想,而且一开始也没有理解这种方法。 具体方法: (1)本题的子问题设为从字符串 s 的前 i 个元素中能否切分出字典 wordDict 里面的单词。因为考虑到空字符串的问题,我们将用于记录子问题信息的vector定义为 len+1 大
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 4 vector<vector<string>> partition(string s) 5 { 6 vector<string> temp; 7 vector<vector<string>> result; 8 9 ge
阅读全文
摘要:题目: 解答: 先序遍历进行处理。 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNod
阅读全文
摘要:题目: 无序数组求中位数。 解答: 利用快排的思想 1、先进行一趟快排,使得div左边的值都比arr[div]小,div右边的值都比arr[div]大,但是这个div的位置是不确定的,可能位于中间,也可能偏左或者偏右。 2、计算出mid所在的下标,如果是奇数则是mid=(size+1)/2,如果是偶
阅读全文
摘要:题目: 解答: 在无重复字符代码的基础上先对字符串进行排序,这样重复字符必然相邻,然后在回溯过程中加一句判断条件去除重复排列。 1 class Solution 2 { 3 public: 4 vector<string> permutation(string S) 5 { 6 vector<str
阅读全文
摘要:题目: 解答: 1 class Solution { 2 vector<string>ans; 3 void backtracking(string &s,int start) 4 { 5 if(start==s.size()) 6 { 7 ans.emplace_back(s); 8 } 9 fo
阅读全文
摘要:题目: 解答: class Solution { public: int convertInteger(int A, int B) { int res = 0; int temp = A ^ B; while (temp!=0) { int lowbit = temp & (-temp); res+
阅读全文
摘要:题目: 解答: 通过中序遍历进行比较。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 利用两个栈s1,s2,其中s1存放排序数据,当执行push()时,若s1.top()<val,将所有小于val的元素放入s2,再将val压入s1中,最后将s2中的元素放入s1中,实现s1的排序push。 1 class SortedStack { 2 public: 3 stack<
阅读全文
摘要:题目: 解答: 第一个栈保存正常push,另一个保存逆序栈,也就是队列顺序。第二个优先于第一个栈。 1 class MyQueue { 2 public: 3 stack<int> que; 4 stack<int> temp; 5 /** Initialize your data structur
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {
阅读全文
摘要:题目: 解答: 常量空间的话,第一可以考虑是不是固定数量的几个变量能搞定;否则可以考虑是不是问题本身已经提供了足够的空间。 这道题目属于后者,就是利用矩阵的第一行和第一列来作为辅助空间使用。不用开辟新的存储空间。方法就是: A. 先确定第一行和第一列是否需要清零; B. 扫描剩下的矩阵元素,如果遇到
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int maxValue(vector<vector<int>>& grid) 4 { 5 int m = grid.size(); 6 int n = grid[0].size(); 7 8 for (int i = 0
阅读全文
摘要:题目: 解答: 思路:滑动窗口。 我们可以使用哈希表记录每个字符的下一个索引,然后尽量向右移动尾指针来拓展窗口,并更新窗口的最大长度。如果尾指针指向的元素重复,则将头指针直接移动到窗口中重复元素的右侧。 1 class Solution { 2 public: 3 int lengthOfLonge
阅读全文
摘要:题目: 解法: /* * 位运算 * * 因为不能使用加减乘除四则运算,所以只能想到使用二进制的位运算实现相加操作。 * 二进制位运算中,异或操作: 1^1=0 0^0=0 1^0=1 0^1=1,可以模拟无进位的加操作; * 与操作:1&1=1 0&1=0 1&0=0 0&0=0,可以模拟进位的情
阅读全文
摘要:题目: 解答: 思路描述: 计算除 A[i] 以外所有元素的乘积,如果我们使用两层遍历就会重复计算很多次乘法, 其实就相当于求 A[i] 左边所有元素的乘积之和和右边所有元素的乘积 如果我们每次只考虑一边的乘积的话,比如left[i]代表i左侧的乘积,那么left[i + 1] = A[i] * l
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<int> constructArr(vector<int>& a) 、 4 { 5 int n = a.size(); 6 vector<int> ret(n, 1); 7 8 int left = 1; 9
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int sumNums(int n) 4 { 5 if(n == 1) 6 { 7 return 1; 8 } 9 n += sumNums(n - 1); 10 return n; 11 } 12 };
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) 4 { 5 int cost = INT_MAX; 6 int profit = 0; 7 for (int price: prices) 8 { 9
阅读全文
摘要:题目: 解答: 1 class Solution { 2 int f(int n, int m) 3 { 4 if (n == 1) 5 { 6 return 0; 7 } 8 int x = f(n - 1, m); 9 return (m + x) % n; 10 } 11 public: 12
阅读全文
摘要:题目: 解答: 思路描述: 这个就可以先排一下序,然后计算出来 0 的个数 num1,以及需要多少张牌才能够连续,比如: 1 2 4 需要一张牌才能连续,如果数目不大于 num1,那么就是顺子。其中注意出现相同的牌也不是顺子! 1 class Solution { 2 public: 3 bool
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 string reverseLeftWords(string s, int n) 4 { 5 reversestr(s, 0, n); 6 reversestr(s, n, s.size()); 7 reversestr(
阅读全文
摘要:题目: 解答: 1 class MaxQueue { 2 queue<int> q; 3 deque<int> d; 4 public: 5 MaxQueue() { 6 } 7 8 int max_value() 9 { 10 if (d.empty()) 11 return -1; 12 ret
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<int> maxSlidingWindow(vector<int>& nums, int k) 4 { 5 if(nums.size() == 0 || k == 1) 6 { 7 return nums;
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int reverse_string(string& s, int start, int end) 4 { 5 for (int i = start; i <= (start + end) / 2; i++) 6 { 7
阅读全文
摘要:题目: 解答: 方法一: 两次反转,先反转每个单词,再反转每个句子。 方法二: 1 class Solution { 2 public: 3 string reverseWords(string s) 4 { 5 if(s.empty()) 6 { 7 return s; 8 } 9 10 int
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<vector<int>> findContinuousSequence(int target) 4 { 5 int i = 1; // 滑动窗口的左边界 6 int j = 1; // 滑动窗口的右边界 7
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) 4 { 5 // vector<int> res; 6 if (nums.size() <= 1) 7 { 8 retur
阅读全文
摘要:题目: 解答: 方法一:哈希 class Solution { public: int singleNumber(std::vector<int> &nums) { if (nums.empty()) { return 0; } // 哈希表存储数和其出现次数 std::unordered_map<
阅读全文
摘要:题目: 解答: 方法一:哈希 1 class Solution { 2 public: 3 vector<int> singleNumber(vector<int>& nums) { 4 map<int, int> count; 5 for (int n : nums) count[n] ++; 6
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 令0~n的数与nums中的数异或,运算中除了缺失值只出现一次外,其他数都出现两次等同于与自身异或。 1 class Solution { 2 public: 3 int missingNumber(vector<int>& nums) 4 { 5 int n = nums.size(
阅读全文
摘要:题目: 解答: 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(
阅读全文
摘要:题目: 解答: 方法一:双指针法 (1)创建两个指针 pA 和 pB,分别初始化为链表 A 和 B 的头结点。然后让它们向后逐结点遍历。 (2)当 pA到达链表的尾部时,将它重定位到链表 B 的头结点 (你没看错,就是链表 B); 类似的,当 pB 到达链表的尾部时,将它重定位到链表 A 的头结点。
阅读全文
摘要:题目: 解答: 方法一:优先级队列 1 class Solution { 2 public int nthUglyNumber(int n) { 3 PriorityQueue<Long> pq = new PriorityQueue<>(); 4 Set<Long> s = new HashSet
阅读全文
摘要:题目: 解答: 方法一:哈希,遍历。 1 class Solution { 2 public: 3 char firstUniqChar(string s) 4 { 5 if (s == "") 6 { 7 return ' '; 8 } 9 map<char, int> str; 10 11 fo
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int maxSubArray(vector<int>& nums) 4 { 5 int sum = nums[0]; 6 int b = 0; 7 8 for (int i = 0; i < nums.size(); i
阅读全文
摘要:题目: 解答: 方法一:C++超时 1 class MedianFinder { 2 vector<double> store; 3 4 public: 5 // Adds a number into the data structure. 6 void addNum(int num) 7 { 8
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<int> getLeastNumbers(vector<int>& arr, int k) 4 { 5 vector<int> res; 6 priority_queue<int> q; 7 for (int
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int majorityElement(vector<int>& nums) 4 { 5 int x = 0; 6 int votes = 0; 7 for(int num : nums) 8 { 9 if(votes =
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<string> permutation(string str) 4 { 5 vector<string> result; 6 if(str.empty()) 7 { 8 return result; 9 }
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 // 中序遍历即可。只需要记录一个pre指针即可。 2 3 4 class Solution { 5 public: 6 TreeNode* Convert(TreeNode* pRootOfTree) 7 { 8 if(pRootOfTree == nullptr) 9 { 1
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: BST的后序序列的合法序列是,对于一个序列S,最后一个元素是x (也就是根),如果去掉最后一个元素的序列为T,那么T满足:T可以分成两段,前一段(左子树)小于x,后一段(右子树)大于x,且这两段(子树)都是合法的后序序列。 1 class Solution { 2 public: 3
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 bool validateStackSequences(vector<int>& pushed, vector<int>& popped) 4 { 5 stack<int> st; 6 int n = popped.siz
阅读全文
摘要:题目: 解答: 1 class MinStack { 2 stack<int> s; 3 stack<int> s2; 4 public: 5 /** initialize your data structure here. */ 6 MinStack() { 7 8 } 9 10 void pus
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<int> spiralOrder(vector<vector<int>>& matrix) 4 { 5 vector<int> result; 6 if (matrix.empty()) 7 { 8 retu
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 1 class Solution { 2 2 bool isSubtree(TreeNode* pRootA, TreeNode* pRootB) 3 3 { 4 4 if (pRootB == NULL) 5 5 { 6 6 return true; 7 7 } 8 8 if
阅读全文
摘要:题目: 解答: 方法一:递归。 1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : 6 val(x), next(NULL) { 7 } 8 };*/ 9 class Solution {
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {
阅读全文
摘要:题目: 解答: 方法一: 1 class Solution { 2 public: 3 vector<int> exchange(vector<int>& nums) 4 { 5 int left = 0; 6 int right = nums.size() - 1; 7 while (left <
阅读全文
摘要:题目: 解答: 1)先去除字符串首尾的空格2)然后根据e划分指数和底数3)判断指数和底数是否合法即可 1 class Solution { 2 public: 3 bool isNumber(string s) { 4 //1、从首尾寻找s中不为空格首尾位置,也就是去除首尾空格 5 int i=s.
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 bool isMatch(string s, string p) 4 { 5 s=" "+s;//防止该案例:""\n"c*" 6 p=" "+p; 7 8 int m = s.size(); 9 int n = p.si
阅读全文
摘要:题目: 解答: 快速幂。 1 class Solution { 2 public: 3 double myPow(double x, int n) 4 { 5 double res=1.0; 6 int i=n; 7 while(i) 8 { 9 if(i&1) 10 { 11 res*=x; //
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) 4 { 5 int res = 0; 6 while(n != 0) 7 { 8 res += n & 1; 9 n >>= 1; 10 } 11 return
阅读全文
摘要:题目: 解答: 回溯法。 (1)在board中找到一个位置,使得board[i][j] == word[0],可以作为搜索的入口; (2)由于一个格子不能重复进入,因此需要定义一个visit数组,保证每个格子只进入一次; (3)找到一个可行解即返回true。若该次搜索返回false,那么进行步骤1.
阅读全文
摘要:题目: 解答: (1)如下图所示,寻找旋转数组的最小元素即为寻找 右排序数组 的首个元素 numbers[x] ,称 x 为 旋转点 。 (2)排序数组的查找问题首先考虑使用 二分法 解决,其可将遍历法的 线性级别 时间复杂度降低至 对数级别 。 算法流程: (1)循环二分:设置 i, j 指针分别
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int numWays(int n) 4 { 5 vector<int> v(n + 1, 1); 6 for(int i = 2; i <= n; i++) 7 { 8 v[i] = (v[i - 1] + v[i -
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int fib(int n) 4 { 5 vector<int> v(100 + 1); 6 v.at(0) = 0; 7 v.at(1) = 1; 8 9 for (int i = 2; i <= n; ++i) 10
阅读全文
摘要:题目: 解答: 只使用一个栈 stack1 当作队列,另一个栈 stack2 用来辅助操作。 要想将新加入的元素出现栈底,需要先将 stack1 的元素转移到 stack2,将元素入栈 stack1,最后将 stack2 的元素全部回到 stack1。 1 class CQueue { 2 publ
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 方法一:栈保存,先进后出。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x)
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 string replaceSpace(string s) 4 { 5 string res; 6 for(int i = 0; i < s.size(); ++i) 7 { 8 if(s[i] == ' ') 9 { 1
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) 4 { 5 int i=matrix.size()-1; 6 int j=0; 7 //
阅读全文
摘要:题目: 解答: 方法一:哈希 使用哈希来进行处理,当发现哈希中包含相应的元素时,则表示出现了重复的元素,则返回即可。 1 class Solution { 2 public: 3 int findRepeatNumber(vector<int>& nums) 4 { 5 std::set<int>
阅读全文
摘要:题目: 输入一个非空整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。 解答: BST的后序序列的合法序列是,对于一个序列S,最后一个元素是x (也就是根),如果去掉最后一个元素的序列为T,那么T满足:T可以分成两段,前
阅读全文
摘要:题目: 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 解答: 分析二叉树的下一个节点,一共有以下情况: 1.二叉树为空,则返回空; 2.节点右孩子存在,则设置一个指针从该节点的右孩子出发,一直沿着指向左子结点的
阅读全文
摘要:题目:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)。 解答: 1 class Solution { 2 bool isSubtree(TreeNode* pRootA, TreeNode* pRootB) 3 { 4 if (pRootB == NULL
阅读全文
摘要:题目: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针random指向一个随机节点),请对此链表进行深拷贝,并返回拷贝后的头结点。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空) 解答: 3 4 /* 5 *解题思路: 6 *1、遍历链
阅读全文
摘要:题目: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 解答: 1 // 中序遍历即可。只需要记录一个pre指针即可。 2 3 4 class Solution { 5 public: 6 TreeNode* Convert(Tre
阅读全文
摘要:题目: 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。 解答: 思路: 设置快慢指针,都从链表头出发,快指针每次走两步,慢指针一次走一步,假如有环,一定相遇于环中某点(结论1)。接着让两个指针分别从相遇点和链表头出发,两者都改为每次走一步,最终相遇于环入口(结论2)。以下
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 string minNumber(vector<int>& nums) 4 { 5 // 选取使字符串组合更小的排序规则 6 auto compare = [](string sa, string sb){return s
阅读全文
摘要:题目: 解答: C++实现起来用priority_queue容器,默认从小到大排序,底层实现为最大堆。 1 class Solution { 2 public: 3 vector<int> smallestK(vector<int>& arr, int k) 4 { 5 vector<int> re
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 采用双栈的办法以空间换时间,栈s1作为普通的数据栈,栈s2用来保存每次入栈时的较小值,这样s2栈顶的值总是栈中元素的最小值。 1 class MinStack { 2 public: 3 /** initialize your data structure here. */ 4 Mi
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 异或操作。 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 图解: 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++)
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<string> getValidT9Words(string num, vector<string>& words) 4 { 5 vector<string> svec; 6 string letters[1
阅读全文
摘要:题目: 解答: 当我们加上一个正数的时候,和会增加;当我们加上一个负数的时候,和会减少。如果当前得到的和是个负数,那么这个和接下来的累加中应该抛弃并重新清零,不然的话,这个负数将会减少接下来的和。 1 class Solution { 2 public: 3 int maxSubArray(vect
阅读全文
摘要:题目: 解答: 默认升序(降序也只是改一点代码,不影响) 原理:如果左侧最大值大于中间的最小值,则一定会被中间序列包括;同理,如果右侧最小值大于中间的最大值,则一定会被中间序列包括。 一遍遍历 + 两个指针(两次扫描可一次遍历完成) 1、从前向后扫描数组,判断当前array[i]是否比max小,是则
阅读全文
摘要:题目: 解答: 先排序,然后设定返回值为最大,用双指针求得结果。 1 class Solution { 2 public: 3 int smallestDifference(vector<int>& a, vector<int>& b) 4 { 5 sort(a.begin(),a.end());
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 方法一:会超时间 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for an interval. 3 * struct Interval { 4 * int start; 5 * int end; 6 * Interval() : start(0), end(0) {} 7 * Interval(int
阅读全文
摘要:题目: 解答: (1)如果某一个作为 起跳点 的格子可以跳跃的距离是 3,那么表示后面 3 个格子都可以作为 起跳点。(2)可以对每一个能作为 起跳点 的格子都尝试跳一次,把 能跳到最远的距离 不断更新。(3)如果可以一直跳到最后,就成功了。 1 class Solution { 2 public:
阅读全文
摘要:题目: 解答: 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)
阅读全文
摘要:题目: 解答: 迭代法。 1 class Solution { 2 public: 3 4 vector<int> spiralOrder(vector<vector<int> >& matrix) 5 { 6 vector<int> result; 7 if (matrix.empty()) 8
阅读全文
摘要:题目: 解答: 方法 1 :转置加翻转 最直接的想法是先转置矩阵,然后翻转每一行。这个简单的方法已经能达到最优的时间复杂度O(N^2)。 1 class Solution { 2 public void rotate(int[][] matrix) { 3 int n = matrix.length
阅读全文
摘要:题目: 解答: 方法一: 方法二: 方法三 : 1 class Solution { 2 public: 3 int firstMissingPositive(vector<int>& nums) 4 { 5 for (int i = 0; i < nums.size(); i++) 6 { 7 w
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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] +
阅读全文
摘要:题目: 解答: 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)
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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(
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int minArray(vector<int>& nums) 4 { 5 int left = 0; 6 int right = nums.size() - 1; /* 左闭右闭区间,如果用右开区间则不方便判断右值 */
阅读全文
摘要:题目: 解答: 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 //
阅读全文
摘要:题目: 解答: 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 (
阅读全文
摘要:题目: 解答: 这个题目真是打了各种补丁啊。。。。 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
阅读全文
摘要:题目: 解答: 假如不存在重复元素的话,则二分法很好实现,但此题存在重复元素,所以会比较复杂一点。 举例: 对于 [0, 1, 4, 4, 4] 这个数组,使用二分法拿出位于中间的 idx 为 2 的数字 4,然后我们发现 nums[idx] > idx; 此刻我们除了可以确认 idx2 不是魔术索
阅读全文
摘要:题目: 解答: 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(
阅读全文
摘要:题目: 解答: 方法一: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 方法一:排序。 如果对数字进行排序,则任何重复的数字都将与排序后的数组相邻。 方法二:集合。 如果我们在数组遍历时存储每个元素,我们可以在数组迭代过程中检查每个元素。 方法三:二进制 方法四:二分查找 int findDuplicate(vector<int> &nums) { in
阅读全文
摘要:题目: 解答: 1 // The API isBadVersion is defined for you. 2 // bool isBadVersion(int version); 3 4 class Solution { 5 public: 6 int firstBadVersion(int n)
阅读全文
摘要:题目: 解答: 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 =
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 方法一:暴力法 按照题目要求直接求。把所有可能的子数组求和并更新ans ,直到我们找到最优子数组且和满足 sum≥s 。 (1)初始化ans = INT_MAX; (2)用变量i从左到右遍历数组: A. 用变量j从当前元素到数组尾部遍历: a. 将i到j这些元素求和得到sum b.
阅读全文
摘要:题目: 解答: 方法一:线性扫描 本方法利用了连续的两个元素 nums[j]和 nums[j + 1]不会相等这一事实。于是,我们可以从头开始遍历 nums数组。每当我们遇到数字 nums[i],只需要检查它是否大于下一个元素 nums[i+1] 即可判断 nums[i]是否是峰值。 1 publi
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int findMin(vector<int>& nums) 4 { 5 int left = 0; 6 int right = nums.size() - 1; /* 左闭右闭区间,如果用右开区间则不方便判断右值 */
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int findMin(vector<int>& nums) 4 { 5 int left = 0; 6 int right = nums.size() - 1; /* 左闭右闭区间,如果用右开区间则不方便判断右值 */
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 按杨氏矩阵的方法求解,时间复杂度为O(m+n),其中m为矩阵的行数,n为矩阵的列数。 1 class Solution { 2 public: 3 bool searchMatrix(vector<vector<int>>& matrix, int target) 4 { 5 if(
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 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
阅读全文
摘要:题目: 解答: 方法一: 简单粗暴,先将两个数组合并,两个有序数组的合并也是归并排序中的一部分。然后根据奇数,还是偶数,返回中位数。 1 class Solution { 2 public: 3 double findMedianSortedArrays(vector<int>& nums1, ve
阅读全文
摘要:题目: 解答: 下面的方法基于这样一个事实:任何一个整数都可以表示成一个多项式的形式,例如,5 = 2^2 + 2^0,78=2^5 + 2^3 + 2^2 + 2^1等等,任何数字都可以表示上述形式。 我们使除数divisor乘以2(diveisor<<1),直到它达到或大于被除数的一半(如果我们
阅读全文
摘要:题目: 解答: 从最末位寻找第一个破坏升序的数nums[pos - 1], 然后在遍历过的数里寻找比该数大的最小的一个数。如遍历过的数为[7,6,4,3], nums[pos - 1]为5, 则5需要与6进行交换, 在将[7,5,4,3]改为升序(这里使用reverse)。 遍历过的数从后往前一定是
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<vector<int>> threeSum(vector<int>& nums) 4 { 5 sort(nums.begin(), nums.end()); 6 7 int N = nums.size();
阅读全文
摘要:题目: 解答: 将A的从低位(尾部数字)与K相加,同时K每次都要丢弃个位数字,然后与A的下一个数字相加(此思路参考评论区大佬做的)。 1 class Solution { 2 public: 3 vector<int> addToArrayForm(vector<int>& A, int K) 4
阅读全文
摘要:题目: 解答: lef和rig分别指向左右的数,比较并从最大位开始装。 1 class Solution { 2 public: 3 vector<int> sortedSquares(vector<int>& A) 4 { 5 int len = A.size(); 6 vector<int> a
阅读全文
摘要:题目: 解答: 方法一:线性扫描。 我们从数组的最左侧开始扫描,直到找到第一个不满足 A[i] < A[i + 1] 的 i,那么 i 就是这个数组的最高点。如果 i = 0 或者不存在这样的 i(即整个数组都是单调递增的),那么就返回 false。否则从 i 开始继续扫描,判断接下来的的位置 j
阅读全文
摘要:题目: 解答: 方法一:双指针。 在找到一个偶数位是奇数的前提下,找奇数位上的偶数,找到之后在交换。 1 class Solution { 2 public: 3 vector<int> sortArrayByParityII(vector<int>& A) 4 { 5 int j = 1; 6 f
阅读全文
摘要:题目: 解答: 设置双指针 如果右指针 r 是奇数,指针往右走 否则与左指针 l 交换一次 移动左指针 l 1 class Solution { 2 public: 3 vector<int> sortArrayByParity(vector<int>& A) 4 { 5 int l = 0; 6
阅读全文
摘要:题目: 解答: 如果数组单调则所有相邻两值的差必须都同号。 1 class Solution { 2 public: 3 bool isMonotonic(vector<int>& A) 4 { 5 //两相邻值的差 6 int sub=0; 7 for (int i = 1; i < A.size
阅读全文
摘要:题目: 解答: 思路:滑动窗口。 每个(连续)增加的子序列是不相交的,并且每当 nums[i-1]>=nums[i] 时,每个此类子序列的边界都会出现。当它这样做时,它标志着在 nums[i] 处开始一个新的递增子序列,我们将这样的 i 存储在变量 anchor 中。例如,如果 nums=[7,8,
阅读全文
摘要:题目: 解答: 方法一:累计求和 为了获得长度为 k的子数组的平均值,我们需要知道这 k个元素之和。使用 sum记录数组的累加和,sum[i]存储从第一个元素到第 i个元素之和。该数组只需要计算一次。 在数组 sum中,原数组索引从 i到 i+k的元素之和为 sum[i] - sum[i-k]。按照
阅读全文
摘要:题目: 解答: 方法一:排序。 我们将数组进行升序排序,如果数组中所有的元素都是非负数,那么答案即为最后三个元素的乘积。 如果数组中出现了负数,那么我们还需要考虑乘积中包含负数的情况,显然选择最小的两个负数和最大的一个正数是最优的,即为前两个元素与最后一个元素的乘积。 上述两个结果中的较大值就是答案
阅读全文
摘要:题目: 解答: 我们从左到右扫描数组 flowerbed,如果数组中有一个 0,并且这个 0 的左右两侧都是 0,那么我们就可以在这个位置种花,即将这个位置的 0 修改成 1,并将计数器 count 增加 1。对于数组的第一个和最后一个位置,我们只需要考虑一侧是否为 0。 在扫描结束之后,我们将 c
阅读全文
摘要:题目: 解答: 方法一:暴力破解,找出所有的两个元素的排列。 方法二:排序。 为了理解这种方法,让我们从不同的角度来看待问题。我们需要形成数组元素的配对,使得这种配对中最小的总和最大。因此,我们可以查看选择配对中最小值的操作,比如 (a,b)可能会产生的最大损失 a-b (如果 a > b)。
阅读全文
摘要:题目: 解答: 依次扫描字符串,并用两个计数器记录A和L的数量,其中对于L计数器,因为需要是连续计数,所以如果碰到A和P,则需要重置L计数器。 当A计数器 > 1 时,直接返回false。当L计数器 > 2 时,直接返回false。 都扫描结束,则返回true。 1 class Solution {
阅读全文
摘要:题目: 解答: 首先将数组排序,随后从小到大遍历数组中的元素,对于每一个元素,向其右侧二分查找k+nums[i],因为向其左侧查找得到的nums[i]-k一定是在更早之前被发现的(nums[i]-k)+k。 如果当前元素和其前一个元素相等,那么其找到的结果也必然和前一个元素相等,是重复结果,因此直接
阅读全文
摘要:题目: 解答: 方法一:递归 算法: (1)检查整数N,如果N小于等于1,则返回N; (2)否则,通过递归关系:F(n) = F(n-1) + F(n-2); (3)直到所有计算返回结果得到答案; 1 public class Solution { 2 public int fib(int N) {
阅读全文
摘要:题目: 解答: 方法一:快慢指针 1 class Solution { 2 public: 3 int findMaxConsecutiveOnes(vector<int>& nums) 4 { 5 int slow = 0; 6 int fast = 0; 7 int count = 0; 8 i
阅读全文
摘要:题目: 解答: 方法一:哈希表。 方法二:原地修改。 (1)我们需要知道数组中存在的数字,由于数组的元素取值范围是 [1, N],所以我们可以不使用额外的空间去解决它。 (2)我们可以在输入数组本身以某种方式标记已访问过的数字,然后再找到缺失的数字。 算法: (1)遍历输入数组的每个元素一次。 (2
阅读全文
摘要:题目: 解答: 方法一: 利用set中元素的有序性和唯一性,将元素放入set中,若set的size不小于3输出倒数第三个元素;若set的size小于3,输出最后一个元素。 1 class Solution { 2 public: 3 int thirdMax(vector<int>& nums) 4
阅读全文
摘要:题目: 解答: 方法一: 当我们遇到一个非零元素时,我们需要交换当前指针和慢速指针指向的元素,然后前进两个指针。如果它是零元素,我们只前进当前指针。 1 class Solution { 2 public: 3 void moveZeroes(vector<int>& nums) 4 { 5 int
阅读全文
摘要:题目: 解答: 方法有排序、哈希、数学求和等,这里采用位运算。 由于异或运算(XOR)满足结合律,并且对一个数进行两次完全相同的异或运算会得到原来的数,因此我们可以通过异或运算找到缺失的数字。 1 class Solution { 2 public: 3 int missingNumber(vect
阅读全文
摘要:题目: 解答: 思路:哈希。 (1)维护一个哈希表,里面始终最多包含 k 个元素,当出现重复值时则说明在 k 距离内存在重复元素; (2)每次遍历一个元素则将其加入哈希表中,如果哈希表的大小大于 k,则移除最前面的数字; (3)时间复杂度:O(n)O(n),nn 为数组长度; 1 class Sol
阅读全文
摘要:题目: 解答: 方法一:哈希,判断value值是否大于1 1 class Solution { 2 public: 3 bool containsDuplicate(vector<int>& nums) { 4 5 unordered_map <int,int>mp; 6 for(int i:num
阅读全文
摘要:题目: 解答: 使用反转。 这个方法基于这个事实:当我们旋转数组 k 次, k\%nk%n 个尾部元素会被移动到头部,剩下的元素会被向后移动。 在这个方法中,我们首先将所有元素反转。然后反转前 k 个元素,再反转后面 n-kn−k 个元素,就能得到想要的结果。 假设 n=7且 k=3 。 原始数组
阅读全文
摘要:题目: 解答: 思路: 如果我们把众数记为+1,把其他数记为-1,将它们全部加起来,显然和大于0,从结果本身我们可以看出众数比其他数多。 算法: Boyer-Moore算法的详细步骤如下: 1)维护一个候选众数candidate和它出现的次数count。初始时,candidate可以为任意值,cou
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& numbers, int target) 4 { 5 int low = 0; 6 int high = numbers.size() - 1; 7 8 wh
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) 4 { 5 int profit = 0; 6 for (int i = 1; i < prices.size(); i++) 7 { 8 int tm
阅读全文
摘要:题目: 解答: 我们需要找出给定数组中两个数字之间的最大差值(即,最大利润)。此外,第二个数字(卖出价格)必须大于第一个数字(买入价格)。 形式上,对于每组 i和 j(其中 j >i),我们需要找出 max(prices[j] - prices[i])。 方法一:暴力法 1 class Soluti
阅读全文
摘要:题目: 解答: 总的来说就是利用杨辉三角形后一行与前一行的关系。更新过程为:从倒数第二个元素开始往前更新 它等于原来这个位置的数 + 前一个位置的数行[i] = 行[i] + 行[i-1] 1 class Solution { 2 public: 3 vector<int> getRow(int r
阅读全文
摘要:题目: 解答: 杨辉三角即该位置的值为左上角与右上角的和,注释很清楚了(note for self数组初始化方法)。 1 class Solution { 2 public: 3 vector<vector<int>> generate(int numRows) 4 { 5 vector<vecto
阅读全文

浙公网安备 33010602011771号