Loading

上一页 1 ··· 32 33 34 35 36 37 38 39 40 ··· 75 下一页
摘要: 思路 方法:对称遍历 1 class Solution { 2 public: 3 vector<int> constructArr(vector<int>& a) { 4 if(a.empty()) 5 return vector<int>(); 6 7 vector<int> b(a.size( 阅读全文
posted @ 2020-11-16 10:57 拾月凄辰 阅读(102) 评论(0) 推荐(0)
摘要: 思路 方法:使用位运算实现二进制加法 Java实现 1 class Solution { 2 public int add(int a, int b) { 3 int sum, carry; 4 while(b != 0) //运算到没进位为止 5 { 6 sum = a ^ b; // 用异或运算 阅读全文
posted @ 2020-11-15 20:43 拾月凄辰 阅读(113) 评论(0) 推荐(0)
摘要: 思路 方法:使用逻辑&&运算符的短路特性终止递归 1 class Solution { 2 public: 3 int sumNums(int n) { 4 n > 1 && (n += sumNums(n - 1)); 5 return n; 6 } 7 }; 阅读全文
posted @ 2020-11-15 19:41 拾月凄辰 阅读(85) 评论(0) 推荐(0)
摘要: 思路 方法一:暴力法 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int n = (int)prices.size(), ans = 0; 5 for (int i = 0; i < n; ++i){ 6 阅读全文
posted @ 2020-11-15 19:12 拾月凄辰 阅读(108) 评论(0) 推荐(0)
摘要: 思路 这是经典的约瑟夫环问题。 方法一:用链表模拟 用链表模拟整个游戏过程。如果单纯用链表模拟的话,每次需要从1数到m,才能踢除1个数,所以踢除n-1个数一共需要遍历(n-1)*m次,时间复杂度就是O(n*m),这种方法会超时。 方法二:数学递推公式 1 class Solution { 2 pub 阅读全文
posted @ 2020-11-15 12:35 拾月凄辰 阅读(69) 评论(0) 推荐(0)
摘要: 思路 1. 先排序 2. 如果最大值-最小值>=5,返回false 3. 如果有重复数字,返回false 1 class Solution { 2 public: 3 bool isStraight(vector<int>& nums) { 4 //先排序 5 sort(nums.begin(), 阅读全文
posted @ 2020-11-15 11:39 拾月凄辰 阅读(135) 评论(0) 推荐(0)
摘要: 思路 方法:动态规划 用dp[i][j]表示掷完 i 个骰子之后其点数之和为 j 的总次数,这可以由 投掷完 n-1 枚骰子后,对应点数 j-1, j-2, j-3, ... , j-6 出现的次数之和转化过来。 即: 1 class Solution { 2 public: 3 vector<do 阅读全文
posted @ 2020-11-15 11:01 拾月凄辰 阅读(148) 评论(0) 推荐(0)
摘要: 思路 方法:维护一个单调的双端队列 1 class MaxQueue { 2 private: 3 queue<int> A; 4 deque<int> B; 5 public: 6 MaxQueue() { 7 8 } 9 10 int max_value() { 11 if(B.empty()) 阅读全文
posted @ 2020-11-14 14:21 拾月凄辰 阅读(102) 评论(0) 推荐(0)
摘要: 思路 方法一:暴力法 遍历每一个数nums[i],之后在[i, i+k]中顺序寻找最大值。 时间复杂度:O(k*n) 1 class Solution { 2 public: 3 vector<int> maxSlidingWindow(vector<int>& nums, int k) { 4 i 阅读全文
posted @ 2020-11-14 11:41 拾月凄辰 阅读(88) 评论(0) 推荐(0)
摘要: 思路 方法一:库函数rotate() 1 class Solution { 2 public: 3 string reverseLeftWords(string s, int n) { 4 rotate(s.begin(), s.begin()+n, s.end()); 5 return s; 6 阅读全文
posted @ 2020-11-14 11:07 拾月凄辰 阅读(103) 评论(0) 推荐(0)
上一页 1 ··· 32 33 34 35 36 37 38 39 40 ··· 75 下一页