classSolution(object): defaddStrings(self, num1, num2): """ :type num1: str :type num2: str :rtype: str """ i = len(num1) - 1 j = len(num2) - 1 num = "" carry = 0 while i >= 0or j >= 0: result = carry if i >= 0: result += int(num1[i]) if j >= 0: result += int(num2[j]) if result >= 10: carry = 1 else: carry = 0 num = str(result % 10) + num i -= 1 j -= 1 if carry == 1: num = "1" + num return num
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example: Given a binary tree
1 2 3 4 5
1 / \ 2 3 / \ 4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
Note: The length of path between two nodes is represented by the number of edges between them.
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original. Example:
1 2
Input: s = "abcdefg", k = 2 Output: "bacdfeg"
Restrictions:
The string consists of lower English letters only.
Length of the given string and k will in the range [1, 10000]
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]]