[Leetcode] 135, 136, 137

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):  不使用一个数组,而是直接使用整型的每一位来存储。
Code:
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;
    }
};







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值