LeetCode Majority Element, Excel Sheet Column Title
2014-12-23 09:03
387 查看
最近还在赶Python的网络编程日志,反而是无聊编写的LeetCode OJ写完了几个,就放上来水一水吧~
LeetCode Majority Element:题意,给一组数组,返回它的众数
很简单,熟悉一下字典用法。代码如下:
LeetCode Excel Sheet Column Title:题意,给一个数字,保证大于0,然后给出这个数字对应的在Excel上面的列数
第一反应是10进制转换成26进制,但是有一个点需要注意一下,实际上这个转换当中,A-Y是1-25,Z是0。那么按照这个写一版就OK了
代码如下:
LeetCode Majority Element:题意,给一组数组,返回它的众数
很简单,熟悉一下字典用法。代码如下:
# leetCode Major Element class Solution: # @param num, a list of integers # @return an integer def majorityElement(self, num): dic = {} max = 1 ret = num[0] for i in num: if i in dic: num = dic.get(i) num += 1 if num > max: max = num ret = i dic.update({i:num}) else: dic.update({i:1}) return ret
LeetCode Excel Sheet Column Title:题意,给一个数字,保证大于0,然后给出这个数字对应的在Excel上面的列数
第一反应是10进制转换成26进制,但是有一个点需要注意一下,实际上这个转换当中,A-Y是1-25,Z是0。那么按照这个写一版就OK了
代码如下:
# leetCode Excel Sheet Column Title class Solution: # @return a string def convertToTitle(self, num): ans = "" digital = 26 values = { 1:"A", 2:"B", 3:"C", 4:"D", 5:"E", 6:"F", 7:"G", 8:"H", 9:"I", 10:"J", 11:"K", 12:"L", 13:"M", 14:"N", 15:"O", 16:"P", 17:"Q", 18:"R", 19:"S", 20:"T", 21:"U", 22:"V", 23:"W", 24:"X", 25:"Y", 26:"Z"} if num==0: return 'A' while num > 0: temp = num % digital if temp == 0: temp = 26 num -=1 # print(" %d / %d = %d and ans is %s the key is %d " %(num,digital,temp,ans,temp)) ans = values[temp] + ans # pay attention, there is no difference between int and float num = int(num / digital) return ans这里还用了字典,稍微熟悉一下,正好在Python网络编程当中会用到这种功能很强大的操作
相关文章推荐
- 【LeetCode】168. Excel Sheet Column Title
- 【leetcode】Excel Sheet Column Title & Excel Sheet Column Number
- LeetCode Excel Sheet Column Title
- Leetcode: Excel Sheet Column Title
- [LeetCode] Excel Sheet Column Title
- Leetcode: Excel Sheet Column Title
- LeetCode:《Excel Sheet Column Title》
- [LeetCode]Excel Sheet Column Title
- 【Leetcode】Excel sheet Column title
- [LeetCode] Excel Sheet Column Title
- 【leetcode 进制转换】Excel Sheet Column Title
- leetcode 168-Excel Sheet Column Title
- [leetcode]Excel Sheet Column Title
- [leetcode]Excel Sheet Column Title
- 【Leetcode】Excel Sheet Column Title
- [leetcode]Excel Sheet Column Title
- Leetcode: Excel Sheet Column Title
- LeetCode(168) Excel Sheet Column Title
- [LeetCode]168 Excel Sheet Column Title
- [leetcode] Excel Sheet Column Title & Excel Sheet Column Number