171. Excel Sheet Column Number\172. Factorial Trailing Zeroes\134. Gas Station\135. Candy

171. Excel Sheet Column Number

题目描述

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 

代码实现

class Solution {
public:
    int titleToNumber(string s) {
        int res = 0, slen = s.length(), mul = 1;
        for(int ind = slen - 1; ind >= 0; ind--) {
            res += (s[ind] - 'A' + 1)*mul; mul *= 26;
        }
        return res;
    }
};

简化一下:

class Solution {
public:
    int titleToNumber(string s) {
        int res = 0, slen = s.length(), mul = 1;
        while(--slen >= 0) {
            res += (s[slen] - 'A' + 1)*mul; mul *= 26;
        }
        return res;
    }
};

172. Factorial Trailing Zeroes

题目描述

Given an integer n, return the number of trailing zeroes in n!.

代码实现

class Solution {
public:
    int trailingZeroes(int n) {
        return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
    }
};

计算5的倍数即可。

class Solution {
public:
    int trailingZeroes(int n) {
     int count = 0;  
     for(int i = 5; n/i >= 1;)  
     {  
            count += n/i;  
            n /= 5;  
      }
      return count;
    }    
};

134. Gas Station

题目描述

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.

代码实现

比较典型的贪心算法。

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int rem = 0, ind = 0, glen = gas.size();

        for(int i = 0, cnt = 0; i < glen; i++, cnt++) {
            int stt = i;
            rem = gas[i];
            while(rem >= cost[i]) {
                rem -= cost[i];
                i++;
                cnt++;
                i = i%glen;
                if(stt == i)
                    return stt;
                rem += gas[i];
            }
            if(cnt > glen) break;
        }
        return -1;
    }
};

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?

代码实现

最开始使用的是暴力破解,超时了。

class Solution {
public:
    int candy(vector<int>& r) {
        int rlen = r.size();
        vector<int> num(rlen, 1);
        bool finish = false;
        while(!finish) {
            int cnt = 0;
            for(int i = 0; i < rlen; i++) {
                if(i - 1 >= 0 && r[i] > r[i - 1] && num[i] <= num[i-1]) num[i]++;
                if(i + 1 < rlen && r[i] > r[i + 1] && num[i] <= num[i+1]) num[i]++;
            }
            for(int j = 0; j < rlen; j++) {
                if(j - 1 >= 0 && r[j] > r[j-1] && num[j] <= num[j-1]) break;
                if(j - 1 >= 0 && r[j] < r[j-1] && num[j] >= num[j-1]) break;
                if(j + 1 < rlen && r[j] > r[j+1] && num[j] <= num[j+1]) break;
                if(j + 1 < rlen && r[j] < r[j+1] && num[j] >= num[j+1]) break;
                if(j == rlen - 1) finish = true;
            }
        }
        for(int i = 0; i < rlen; i++) cout << num[i] << "  ";
        return accumulate(num.begin(), num.end(), 0);
    }
};

使用贪心算法,把从左到右,如果是右边大于左边,右边就是左边加一。接着从右到左,如果左边大于右边,那就是从本身和右边+1中选择更大的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值