class Solution(object): def convertToBase7(self, num): """ :type num: int :rtype: str """ if num == 0: return "0" symbol = "" if num < 0: num = 0 - num symbol = "-"
result = "" while num > 0: result += str(num % 7) num = num / 7
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right. For example, the above binary watch reads “3:25”. Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent. Example:
The hour must not contain a leading zero, for example “01:00” is not valid, it should be “1:00”.
The minute must be consist of two digits and may contain a leading zero, for example “10:2” is not valid, it should be “10:02”.
难度
Easy
方法
遍历所有可能的时间,如果其中1的个数等于n,则将该时间加入返回的结果列表中
python代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution(object): defreadBinaryWatch(self, num): """ :type num: int :rtype: List[str] """ result = [] for i inrange(12): for j inrange(60): ifbin(i).count("1") + bin(j).count("1") == num: result.append("%d:%02d" % (i, j)) return result
Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k(the order of the tuple matters).
Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
Example:
1 2 3 4 5 6 7 8
Input: [[0,0],[1,0],[2,0]]
Output: 2
Explanation: The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: “Gold Medal”, “Silver Medal” and “Bronze Medal”.
Example 1:
1 2 3 4 5
Input: [5, 4, 3, 2, 1] Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"] Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". For the left two athletes, you just need to output their relative ranks according to their scores.
Note:
N is a positive integer and won’t exceed 10,000.
All the scores of athletes are guaranteed to be unique.
class Solution(object): def findRelativeRanks(self, nums): """ :type nums: List[int] :rtype: List[str] """ numsStrs = [] for i in range(len(nums)): numsStrs.append(str(nums[i])+'-'+str(i))
numsStrs = sorted(numsStrs, lambda x,y: cmp(int(x.split("-")[0]), int(y.split("-")[0])))[::-1] ranks = [''] * len(nums) for i in range(len(numsStrs)): index = int(numsStrs[i].split("-")[1]) if i == 0: ranks[index] = "Gold Medal" elif i == 1: ranks[index] = "Silver Medal" elif i == 2: ranks[index] = "Bronze Medal" else: ranks[index] = str(i+1) return ranks
Given a binary tree, return the tilt of the whole tree.
The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0.
The tilt of the whole tree is defined as the sum of all nodes’ tilt.
Example:
1 2 3 4 5 6 7 8 9 10
Input: 1 / \ 2 3 Output: 1 Explanation: Tilt of node 2 : 0 Tilt of node 3 : 0 Tilt of node 1 : |2-3| = 1 Tilt of binary tree : 0 + 0 + 1 = 1
Note:
The sum of node values in any subtree won’t exceed the range of 32-bit integer.
All the tilt values won’t exceed the range of 32-bit integer.
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note: You may assume that both strings contain only lowercase letters.
i = 0 j = 0 while i < len(ransomNote) and j < len(magazine): if ransomNote[i] == magazine[j]: j += 1 i += 1 else: j += 1 if i == len(ransomNote): returnTrue returnFalse