classSolution(object): deffindTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ dic_t = {} for item in t: dic_t[item] = dic_t.get(item,0) + 1 for item in s: dic_t[item] = dic_t[item] - 1 for item in t: if dic_t[item] == 1: return item
这题同样可以使用 XOR,或者只是将字符转换为对于对应的 ASCII 数值然后相减
XOR
1 2 3
classSolution(object): deffindTheDifference(self, s, t): returnchr(reduce(operator.xor, map(ord, s + t)))
相减
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution(object): deffindTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ ans = 0 for c in t: ans = ans + ord(c) for c in s: ans = ans - ord(c) returnchr(ans)
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher’s h-index.
According to the definition of h-index on Wikipedia: “A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each.”
For example, given citations = [3, 0, 6, 1, 5], which means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, his h-index is 3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Solution
O(n)算法其实不难,主要还得做到空间复杂度为O(1).
O(n)的算法
这题的标签是 hash table,很明显常规解法就是使用这个:
遍历列表然后用字典统计数字–数字出现的个数
遍历字典返回 value 值为 1 的数字
当然很明显这种做法,空间复杂度超过 O(1) 了
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution(object): defsingleNumber(self, nums): """ :type nums: List[int] :rtype: int """ dic_nums = {} for num in nums: dic_nums[num] = dic_nums.get(num,0) + 1 for key in dic_nums: if dic_nums[key] == 1: return key
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]]
classSolution(object): defnumberOfBoomerangs(self, points): """ :type points: List[List[int]] :rtype: int """ res = 0 for x1,y1 in points: dis_map = {} for x2,y2 in points: dis_map[(x1-x2)**2 + (y1-y2)**2] = 1 + dis_map.get((x1-x2)**2 + (y1-y2)**2,0) for dis in dis_map: #1 res += dis_map[dis] * (dis_map[dis] - 1) return res
classSolution(object): defnumberOfBoomerangs(self, points): """ :type points: List[List[int]] :rtype: int """ count = 0 for point1 in points: m = {} for point2 in points: dx = point1[0] - point2[0] dy = point1[1] - point2[1] d = dx*dx + dy*dy if d in m: count += m[d]*2 m[d] +=1 else: m[d] = 1 return count
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn’t have “lakes” (water inside that isn’t connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don’t exceed 100. Determine the perimeter of the island.
Example:
1 2 3 4 5 6 7
[[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]]
Answer: 16 Explanation: The perimeter is the 16 yellow stripes in the image below:
Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.
Example 1:
1 2 3 4 5 6
Input: candies = [1,1,2,2,3,3] Output: 3 Explanation: There are three different kinds of candies (1, 2 and 3), and two candies for each kind. Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too. The sister has three different kinds of candies.
Example 2:
1 2 3 4
Input: candies = [1,1,2,3] Output: 2 Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1]. The sister has two different kinds of candies, the brother has only one kind of candies.
Note:
The length of the given array is in range [2, 10,000], and will be even.
The number in given array is in range [-100,000, 100,000].
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.
You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.
Example 1:
1 2 3 4 5
Input: ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"] Output: ["Shogun"] Explanation: The only restaurant they both like is "Shogun".
Example 2:
1 2 3 4 5
Input: ["Shogun", "Tapioca Express", "Burger King", "KFC"] ["KFC", "Shogun", "Burger King"] Output: ["Shogun"] Explanation: The restaurant they both like and have the least index sum is "Shogun" with index sum 1 (0+1).
Note: The length of both lists will be in the range of [1, 1000]. The length of strings in both lists will be in the range of [1, 30]. The index is starting from 0 to the list length minus 1. No duplicates in both lists.