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
题目描述
代码实现
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
简化一下:
计算5的倍数即可。
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.
使用贪心算法,把从左到右,如果是右边大于左边,右边就是左边加一。接着从右到左,如果左边大于右边,那就是从本身和右边+1中选择更大的。
题目描述
代码实现
Factorial Trailing Zeroes
题目描述
代码实现
Gas Station
题目描述
代码实现
Candy
题目描述
代码实现
171. Excel Sheet Column Number
题目描述
Related to question Excel Sheet Column TitleGiven 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中选择更大的。
相关文章推荐
- William J. Thompson, «Computing for Scientists and Engineers: A Workbook of Analysis, Numerics, and Applications»
- Jacquie Barker, «Beginning Java Objects: From Concepts To Code, Second Edition»
- Ian Sinclair, John Dunton, «Practical Electronics Handbook, Sixth Edition»
- reactos操作系统实现(171)
- NYOJ-171 第三届河南省ACM竞赛 聪明的KK (简单的DP)
- Codeforces Round #171 (Div. 2)
- CF 171 (div2)C
- nyoj 171 聪明的kk
- nyoj18 The Triangle & hdu2084 & nyoj171 聪明的KK 数塔(动态规划初步)
- NYOJ171 聪明的kk
- nyoj171
- NYOJ-171 聪明的kk
- 聪明的kk (南洋理工—171)
- nyoj-171 聪明的kk
- Leetcode_171_Excel Sheet Column Number
- leetCode#171 Excel Sheet Column Number
- Leetcode#171 Excel Sheet Column Number
- LeetCode 135. Candy (O(n)时间 O(n)和O(1)空间的两种实现)
- [leetcode]C语言:171 Excel Sheet Column Number
- 135. Candy