189. Rotate Array \ 169. Majority Element \ Best time to buy
2017-01-18 17:20
489 查看
Rotate Array
Majority Element
Best time to buy
法二:
法二:
法二:使用 Moore’s voting algorithm。
Moore’s Voting Algorithm 介绍:
http://www.cs.utexas.edu/~moore/best-ideas/mjrty/index.html
法二:
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; } };
相关文章推荐
- Best Time to Buy and Sell Stock
- leetcode解题之121 # Best Time to Buy and Sell Stock
- LeetCode Best Time to Buy and Sell Stock III
- 122. Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock
- 121BestTimetoBuyandSellStock.java
- leetcode之Best Time to Buy and Sell Stock && II && III
- Leetcode: Best Time to Buy and Sell Stock I II III
- [leetcode][DP] Best Time to Buy and Sell Stock
- 309. Best Time to Buy and Sell Stock with Cooldown【M】【56】
- Middle-题目19:309. Best Time to Buy and Sell Stock with Cooldown
- Best Time to Buy and Sell Stock
- Hard-题目21:123. Best Time to Buy and Sell Stock III
- LeetCode122:Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock I II III IV (第四周 动态规划)
- 【LEETCODE】121-Best Time to Buy and Sell Stock
- LeetCode-Best Time to Buy and Sell Stock III
- Best Time To Buy and Sell Stock
- [LeetCode] Best Time to Buy and Sell Stock IV
- LeetCode121 Best Time to Buy and Sell Stock