189. Rotate Array \ 169. Majority Element \ Best time to buy
来源:互联网 发布:送餐员下载什么软件 编辑:程序博客网 时间:2024/06/05 00:32
- Rotate Array
- Majority Element
- Best time to buy
189. Rotate Array
法一:
class Solution {public: void rotate(vector<int>& nums, int k) { queue<int> tmp; k = k%nums.size(); k = nums.size() - k; for(int i = 0; i < k; i++) { tmp.push(nums[i]); } nums.erase(nums.begin(), nums.begin() + k); while(!tmp.empty()) { int t = tmp.front(); tmp.pop(); nums.push_back(t); } }};
法二:
class Solution { public: void rotate(int nums[], int n, int k) { if ((n == 0) || (k <= 0)) { return; } // Make a copy of nums vector<int> numsCopy(n); for (int i = 0; i < n; i++) { numsCopy[i] = nums[i]; } // Rotate the elements. for (int i = 0; i < n; i++) { nums[(i + k)%n] = numsCopy[i]; } } };
法二:
class Solution { public: void rotate(int nums[], int n, int k) { if ((n == 0) || (k <= 0)) { return; } int cntRotated = 0; int start = 0; int curr = 0; int numToBeRotated = nums[0]; int tmp = 0; // Keep rotating the elements until we have rotated n // different elements. while (cntRotated < n) { do { tmp = nums[(curr + k)%n]; nums[(curr+k)%n] = numToBeRotated; numToBeRotated = tmp; curr = (curr + k)%n; cntRotated++; } while (curr != start); // Stop rotating the elements when we finish one cycle, // i.e., we return to start. // Move to next element to start a new cycle. start++; curr = start; numToBeRotated = nums[curr]; } } };
169. Majority Element
法一:
class Solution {public: int majorityElement(vector<int>& nums) { int nums_len = nums.size(); int result = 0; int max_times = 0; map<int, int> counter; for(int i = 0; i < nums_len; i++) { if(counter.count(nums[i]) > 0) { counter[nums[i]]++; if(counter[nums[i]] > max_times) { result = nums[i]; max_times = counter[nums[i]]; } } else { counter[nums[i]] = 1; if(counter[nums[i]] > max_times) { result = nums[i]; max_times = counter[nums[i]]; } } } return result; }};
法二:使用 Moore’s voting algorithm。
int majorityElement(vector<int> &num) { int majorityIndex = 0; for (int count = 1, i = 1; i < num.size(); i++) { num[majorityIndex] == num[i] ? count++ : count--; if (count == 0) { majorityIndex = i; count = 1; } } return num[majorityIndex];}
Moore’s Voting Algorithm 介绍:
http://www.cs.utexas.edu/~moore/best-ideas/mjrty/index.html
Best time to buy
法一:
class Solution {public: int maxProfit(vector<int>& prices) { int prices_len = prices.size(); int profit_max = 0; for(int i = 0; i < prices_len - 1; i++) { for(int j = i + 1; j < prices_len; j++) { int profit_tmp = prices[j] - prices[i]; profit_max = profit_tmp > profit_max? profit_tmp : profit_max; } } return profit_max; }};
法二:
class Solution {public: int maxProfit(vector<int> &prices) { int maxPro = 0; int minPrice = INT_MAX; for(int i = 0; i < prices.size(); i++){ minPrice = min(minPrice, prices[i]); maxPro = max(maxPro, prices[i] - minPrice); } return maxPro; }};
0 0
- 189. Rotate Array \ 169. Majority Element \ Best time to buy
- [4]169. Majority Element/[4]121. Best Time to Buy and Sell Stock(Java)
- leetcode题解-169. Majority Element && 189. Rotate Array
- [Array] Best Time to Buy and Sell Stock
- LeetCode-Array-121. Best Time to Buy and Sell Stock
- LeetCode刷题【Array】 Best Time to Buy and Sell Stock
- LeetCode-Array-169. Majority Element
- 【leetcode】Array——Best Time to Buy and Sell Stock I/II/III
- leetCode刷题归纳-Array(Best Time to Buy and Sell Stock I&II&III)
- LeetCode刷题【Array】Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- node js 调试
- React基础知识一
- 微信开发之——接口返回码
- runtime源码探究(四)区分isKindOfClass、isSubclassOfClass、isMemberOfClass
- chrome livestyle插件
- 189. Rotate Array \ 169. Majority Element \ Best time to buy
- PRank算法python实现
- 清除浮动的几种方法
- 隐藏文字
- Spring 4 注解新特性
- git笔记 常规使用
- android音量键调节听筒音量的大小
- 定时器、Date
- FIS