待办:
1. set容器用法
2. 位运算
题目:217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice
in the array, and it should return false if every element is distinct.Example 1:
Input: [1,2,3,1] Output: true Example 2:
Input: [1,2,3,4] Output: false
#include<set>
#include<algorithm>
using namespace std;
bool Solution217::containsDuplicate(vector<int>& nums)
{
//way 1 low
//set<int> nums_plus;
//for (int i : nums)
//{
// if (nums_plus.find(i) != nums_plus.end())
// return true;
// else
// nums_plus.insert(i);
//}
//return false;
if (nums.size() < 2)
return false;
sort(nums.begin(), nums.end());
for (int i = 1; i < nums.size(); i++)
{
if (nums[i] == nums[i - 1])
return true;
}
return false;
//way 3 low
//return nums.size() != set<int>{nums.begin(), nums.end()}.size();
}
收获:
1. 此题给了三种解法,先用sort排序再进行比较是最快的方法
2. 接触了set容器,以及相应的find
方法,如果没有找到,迭代器最后到达num_plus.end()
3. 初始化set的方法,set<int> {iterator begin,iterator end}
4. 关于set更多的总结以后再写
题目:169. Majority Element
>
Given an array of size n, find the majority element. The majority
element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element
always exist in the array.Example 1:
Input: [3,2,3] Output: 3 Example 2:
Input: [2,2,1,1,1,2,2] Output: 2
题目:122. Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a
given stock on day i.Design an algorithm to find the maximum profit. You may complete as
many transactions as you like (i.e., buy one and sell one share of the
stock multiple times).Note: You may not engage in multiple transactions at the same time
(i.e., you must sell the stock before you buy again).Example 1:
Input: [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (price = 1)
and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
int Solution169::majorityElement(vector<int>& nums)
{
int majority = nums[0];
int num_major = 1;
for (int i = 1; i < nums.size(); i++)
{
if (majority == nums[i])
{
num_major++;
}
else if(num_major > 0 && majority != nums[i])
{
--num_major;
cout << num_major << endl;
}
else
{
num_major = 1;
majority = nums[i];
}
}
return majority;
}
题目:118. Pascal’s Triangle
Given a non-negative integer numRows, generate the first numRows of
Pascal’s triangle.In Pascal’s triangle, each number is the sum of the
two numbers directly above it.
Example:
Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
vector<vector<int>> Solution118::generate(int numRows)
{
vector<vector<int>> pascal;
if (numRows == 0)
return vector<vector<int>> {};
if (numRows == 1)
{
pascal.push_back(vector<int> {1});
}
else if (numRows == 2)
{
pascal.push_back(vector<int> {1});
pascal.push_back(vector<int> {1, 1});
}
else
{
pascal.push_back(vector<int> {1});
pascal.push_back(vector<int> {1, 1});
for (int i = 3; i <= numRows; i++)
{
vector<int> pascal_row;
for (int j = 0; j < i; j++)
{
if (j == 0 || j == i - 1)
pascal_row.push_back(1);
else
pascal_row.push_back(pascal[i - 2][j - 1] + pascal[i - 2][j]);
}
pascal.push_back(pascal_row);
}
}
return pascal;
}
题目:268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, …,
n, find the one that is missing from the array.Example 1:
Input: [3,0,1] Output: 2
int Solution268::missingNumber(vector<int>& nums)
{
//way 1
//int n = nums.size() + 1;
//vector<int> nums_plus(n);
//for (int i = 0; i < n - 1; i++)
//{
// nums_plus[nums[i]] = 1;
//}
//for (int i = 0; i < n; i++)
//{
// if (nums_plus[i] == 0) {
// return i;
// }
//}
//way2
//int n = nums.size();
//int sum = (n + 1) * n / 2;
//for (int i : nums)
//{
// sum -= i;
//}
//return sum;
//way3
int result = nums.size();
for (int i = 0; i<nums.size(); ++i)
{
result ^= i ^ nums[i];
}
return result;
}
这道题涉及了异或操作,但不是很理解,希望以后可以有更深入的了解