![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode Solution
木子天一
这个作者很懒,什么都没留下…
展开
-
201. Bitwise AND of Numbers Range
题意是给定一个闭区间[m, n],其中0 <= m <= n <= 2147483647,返回区间内所有数按位与的结果。 比如,给定区间[5, 7],应该返回4。易错点:题目含义其实是求区间内的数有多少位不同,仔细研究不难发现,任意两个相连的数的最后一位必相异,只要相异结果必为0,则这一位的结果也就是0。所以只要每次比较最后一位即可。Solution1class Solution1: #原创 2016-11-07 17:12:30 · 298 阅读 · 0 评论 -
191.Number of 1 Bits
191.Number of 1 Bits题意是求无符号整数二值化结果的和,比如32位整数11二值化为00000000000000000000000000001011,结果返回3.易错点:无Solution1class Solution(object): def hammingWeight(self, n): """ :type n: int :原创 2016-11-06 15:49:47 · 266 阅读 · 0 评论 -
190.Reverse Bits
190.Reverse Bits目的是要对32位无符号数的二进制数进行翻转,比如43261596 (二进制为 00000010100101000001111010011100),应该返回964176192 (二进制为00111001011110000010100101000000)。易错点:利用python进行进制转化的函数,bin,int,hex在对位数较少的数进行转化时并不会自动补零,所以需要手原创 2016-11-06 15:36:40 · 482 阅读 · 0 评论 -
231. Power of Two
题意是判断一个整数是不是2的n次方易错点:想法很简单,判断这个数的二进制数中是不是只有一个1。class Solution: # @param {integer} n # @return {boolean} def isPowerOfTwo(self, n) return n > 0 and (n & (n - 1)) == 0这个n & (n - 1)的求法原创 2016-11-07 19:47:20 · 388 阅读 · 0 评论 -
318. Maximum Product of Word Lengths
题意是给定一个字符数组,求出最大的两个字符串长度的积,同时,这两个字符串不能拥有相同的字母(同一个字符串允许有相同的字母),如果没有这样的两个字符串存在,则返回0。Example 1:Given [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”] Return 16 The two words can be “abcw”, “xtfn”.Example原创 2016-11-08 14:36:16 · 274 阅读 · 0 评论 -
393. UTF-8 Validation
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n-bits are原创 2016-11-30 19:50:18 · 227 阅读 · 0 评论 -
15. 3Sum
题意:给定一个包含n个数的数组S,找出所有S中符合a+b+c=0的不同的三元组(a,b,c),注意解中不包含相同的三元组。For example, given array S = [-1, 0, 1, 2, -1, -4],A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]思路:既然是数组的题,很显然想到用三个指针分别指向这三个数,首先当然先原创 2016-12-12 15:14:51 · 192 阅读 · 0 评论 -
342. Power of Four
题意为判断一个有符号数是不是4的n次方,是之前231的延伸231.Power of Two易错点:和之前的231很相似,直接判断二进制中是否只有一个1,且1位于奇数位上。class Solution(object): def isPowerOfFour(self, num): """ :type num: int :rtype: bool原创 2016-11-23 17:41:18 · 159 阅读 · 0 评论 -
16. 3Sum Closest
题意:给定一个含有n个数的数组S,找出其中的三个数即它们的和与给定的目标数最接近,并返回这三个数的和,假定每个输入只有一个解。For example, given array S = {-1 2 1 -4}, and target = 1.The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).思路:解法和15.3Sum 类似,只是原创 2016-12-13 12:41:09 · 180 阅读 · 0 评论 -
18. 4Sum
题意:给定一个含有n个整数的数组S,其中是否存在四个元素a,b,c,d使a + b + c + d = target,找出所有不同的(a,b,c,d)。For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2],原创 2016-12-13 13:43:07 · 175 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
题意: 给定一个排好序的数组,移除其中重复的元素,并返回新的数组长度,在O(1)的空间复杂度内完成。For example, Given input array nums = [1,1,2],Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.原创 2016-12-13 14:16:35 · 246 阅读 · 0 评论 -
27. Remove Element
题意:Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The or原创 2016-12-13 15:48:32 · 165 阅读 · 0 评论 -
421. Maximum XOR of Two Numbers in an Array
题意:给定一个非空的正整数数组,找出其中异或结果最大的两个值(注:要在O(n)时间内完成)。Example:Input: [3, 10, 5, 25, 2, 8]Output: 28Explanation: The maximum result is 5 ^ 25 = 28.思考:这题刚开始看到,首先想的是异或的性质,比如:异或相当于无进位的加法,但是这题不能这么做,之后想了很多方法,但是要在O(n原创 2016-12-08 13:20:14 · 245 阅读 · 0 评论 -
371. Sum of Two Integers
不使用’+’,’-‘号来计算整数加法,比如给定a=1,b=2,返回c=3.思路: 对于两个一位数而言,可以使用异或来代替加法,使用与来表示进位,所以C++可以很简单的写出这个代码:int Add(int x, int y){ while (y != 0) { int carry = x & y; x = x ^ y; y =原创 2016-11-26 16:16:44 · 182 阅读 · 0 评论 -
389. Find the Difference
题意: 给定两个字符串s ,t ,其中只包含小写字符,t由s打乱之后在任意位置随机添加一个字母组成。找出添加到t上的哪个字母。思路:之前最早的想法使用python集合的方法,求差集就可以了,但是有两个问题,第一是求出的那个字母是个集合类型,没法输出,就像’set([‘a’])’,最后用pop的方法解决了,但是出现了第二个问题,对于s = ‘aa’, t = ‘a’时,s和t的集合类型都是a,这就尴尬原创 2016-11-27 11:23:06 · 219 阅读 · 0 评论 -
41. First Missing Positive
题意: 给定一个未排序的整数数组,找到第一个丢失的正整数。For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space. 思路:开始的时候,题意理解错了,以为是找出一段数字中丢失的数,那个难做啊。。。后来仔细看了原创 2016-12-15 13:33:42 · 211 阅读 · 0 评论 -
48. Rotate Image
题意:将N×N的矩阵顺时针旋转90度。思路:这题直接从旋转之后索引的变换开始考虑,自顶向下,从左到右,其实就是将对应四边的四个点顺时针旋转向右移一位,就是(x,y),(y,n-x),(n-x,n-y),(n-y,x) = (n-y,x),(x,y)(y,n-x),(n-x,n-y) ,再确定下循环次数就可以了。方法一:class Solution(object): def rotate(se原创 2016-12-16 12:27:11 · 189 阅读 · 0 评论 -
54. Spiral Matrix
题意:给定一个m行,n列的矩阵,按螺旋顺序返回所有元素的值。For example, Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]You should return [1,2,3,6,9,8,7,4,5]. 思路:碰到这类矩阵的题,还是要从索引的范围和规律入手,我一开始是想把矩阵转化为ndar原创 2016-12-16 14:21:57 · 275 阅读 · 0 评论 -
31. Next Permutation
题意:就是生成比这个排列大的下一个排列,如果排列已经最大了,就直接生成最小的下一个排列。Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 →原创 2016-12-14 13:45:12 · 199 阅读 · 0 评论 -
59. Spiral Matrix II
题意:给定一个整数n,生成一个把1到n^2按螺旋顺序填充的方阵。For example, Given n = 3, You should return the following matrix: [[ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]思路:这种题目主要还是思考索引的变换规律,和之前54. Spiral Matrix 思路是一样的。class So原创 2016-12-17 13:02:32 · 213 阅读 · 0 评论 -
73. Set Matrix Zeroes
题意: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up:Did you use extra space? A straight forward solution using O(mn) space is probably a bad原创 2016-12-17 15:37:09 · 169 阅读 · 0 评论 -
209. Minimum Size Subarray Sum
题意:Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead.For example, given the array [2,3,1,2原创 2016-12-21 13:09:38 · 147 阅读 · 0 评论 -
215. Kth Largest Element in an Array
题意:Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example, Given [3,2,1,5,6,4] and k = 2, return 5.Not原创 2016-12-21 15:33:16 · 276 阅读 · 0 评论 -
228. Summary Ranges
题意: Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”]. 思路:这题我想的还是使用双指针来做,但是要考虑的东西有点多,所以代码会显得有些繁琐。class Solut原创 2016-12-21 22:34:23 · 213 阅读 · 0 评论 -
229. Majority Element II
题意:Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.思路:就用字典当做哈希表就可以解了,这里注意collections.defaultdict(int),原创 2016-12-22 10:55:51 · 229 阅读 · 0 评论 -
80. Remove Duplicates from Sorted Array II
题意: Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five el原创 2016-12-17 21:23:49 · 207 阅读 · 0 评论 -
415. Add Strings
题意:Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digit原创 2016-12-31 14:28:35 · 185 阅读 · 0 评论 -
238. Product of Array Except Self
题意: Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).F原创 2016-12-23 14:13:41 · 200 阅读 · 0 评论 -
240.Search a 2D Matrix II
题意:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right.Integers in each原创 2016-12-23 14:48:11 · 193 阅读 · 0 评论 -
289. Game of Life
题意: According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”Given a board with m by原创 2016-12-23 16:03:01 · 217 阅读 · 0 评论 -
334. Increasing Triplet Subsequence
题意: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.Formally the function should:Return true if there exists i, j, ksuch that arr[i] < arr[j] <原创 2016-12-23 16:54:37 · 217 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
题意:Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Could原创 2016-12-23 19:46:18 · 206 阅读 · 0 评论 -
442. Find All Duplicates in an Array
题意:Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements that appear twice in this array.Could you do it without extra sp原创 2016-12-23 20:03:20 · 184 阅读 · 0 评论 -
414. Third Maximum Number
题意:Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Example 1:Input: [3, 2, 1]Out原创 2016-12-24 11:48:17 · 185 阅读 · 0 评论 -
412. Fizz Buzz
题意:Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. Fo原创 2016-12-24 12:11:32 · 203 阅读 · 0 评论 -
419. Battleships in a Board
题意:Given an 2D board, count how many different battleships are in it. The battleships are represented with ‘X’s, empty slots are represented with ‘.’s. You may assume the following rules:You receive a原创 2016-12-24 13:26:21 · 246 阅读 · 0 评论 -
396. Rotate Function
题意: Given an array of integers A and let n to be its length.Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a “rotation function” F on A as follow:F(k) = 0 *原创 2016-12-24 12:43:21 · 255 阅读 · 0 评论 -
118. Pascal's Triangle
题意:Given numRows, generate the first numRows of Pascal’s triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]思路:这个是开始学c++的时候遇到过的第一题吧,先放一个正常的代原创 2016-12-18 13:06:21 · 184 阅读 · 0 评论 -
119. Pascal's Triangle II
题意:Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1]. 思路:和之前那个118.Pascal’s Triangle 的思路是一样的。第一种还是前后补0相加的方法:class Solution(object): def getRow原创 2016-12-18 13:33:49 · 186 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
题意:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), d原创 2016-12-18 15:52:24 · 186 阅读 · 0 评论