您的位置:首页 > 大数据 > 人工智能

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

2017-02-20 22:24 357 查看
Excel Sheet Column Number
题目描述

代码实现

Factorial Trailing Zeroes
题目描述

代码实现

Gas Station
题目描述

代码实现

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中选择更大的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 171 172 134 candy