135. Candy
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
- Each child must have at least one candy.
- Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
Note: 最直接的方法就是直接模拟发糖果的过程,但是这样做会超时,时间复杂度为O(n^2)。
Code:
class Solution {
public:
int candy(vector<int>& ratings) {
if(ratings.size()==0) return 0;
vector<int> candies(ratings.size(),1);
for(int i=1; i<ratings.size(); i++){
if(ratings[i]>ratings[i-1]){
candies[i] = candies[i-1]+1;
}else if(ratings[i]==ratings[i-1]){
candies[i] = 1;
}else{
candies[i]=1;
for(int t=i-1; t>=0; t--){
if(ratings[t]>ratings[t+1] && candies[t]<=candies[t+1])
candies[t] = candies[t+1] + 1;
else
break;
}
}
}
int candyNum = 0;
for(int i=0; i<candies.size(); i++)
candyNum += candies[i];
return candyNum;
}
};
Solution: ratings递增的部分只需要逐项加一即可,其余部分可以在第二次循环中通过逆序扫描转换成递增的情况。
Code:
class Solution {
public:
int candy(vector<int>& ratings) {
if(ratings.size()==0) return 0;
vector<int> candies(ratings.size(),1);
for(int i=1; i<ratings.size(); i++){
if(ratings[i] > ratings[i-1]){
candies[i] = candies[i-1]+1;
}
}
for(int i=ratings.size()-2; i>=0; i--){
if(ratings[i] > ratings[i+1]){
candies[i] = max(candies[i+1]+1,candies[i]);
}
}
int candyNum = 0;
for(int i=0; i<candies.size(); i++)
candyNum += candies[i];
return candyNum;
}
};
136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Solution: 异或运算满足交换律和结合律,同时相同的数异或运算后会相互抵消,因此可以使用异或运算提取出这样的数来。
Code:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ans = 0;
for(int i=0; i<nums.size(); i++){
ans ^= nums[i];
}
return ans;
}
};
137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Solution(1): 对于二进制下每一位,统计1的数量和0的数量,不能被3整除的就说明single number那一位是0或1。
Code:
class Solution {
public:
int singleNumber(vector<int>& nums) {
vector<int> ones(sizeof(int)*8,0);
for(int i=0; i<nums.size(); i++){
int tmpt = nums[i];
for(int t=0; t<ones.size(); t++){
cout<<(tmpt & 1);
ones[t] += (tmpt & 1); //注意:都是位运算,因为int含有符号位,因此需要进行二进制而不能使用加减乘除
tmpt = tmpt >> 1;
ones[t] = ones[t] % 3;
}
cout<<endl;
}
int ans = 0;
for(int i=0; i<ones.size(); i++){
cout<<ones[i];
ans |= ones[i]<<i;
}
return ans;
}
};
Solution(2): 不使用一个数组,而是直接使用整型的每一位来存储。
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ones = 0;
int twos = 0;
int threes = 0;
for(int i=0; i<nums.size(); i++){
twos |= ones & nums[i]; //进位变成2个1
ones ^= nums[i]; //表示该位有1个1
threes = ~(ones & twos); //是否进位成3,是0,否1,用于模拟%3运算
ones = ones & threes; //进位成功归0
twos = twos & threes; //进位成功归0
}
return ones;
}
private:
void outBinary(int n){
for(int t=(sizeof(int)*8)-1; t>=0; t--){
cout<<((n>>t) & 1);
}
cout<<endl;
}
};