Leetcode
文章平均质量分 53
糊糊姨
这个作者很懒,什么都没留下…
展开
-
leetcode--547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, the原创 2017-09-18 19:42:41 · 476 阅读 · 0 评论 -
leetcode--16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exact原创 2017-09-28 21:41:10 · 231 阅读 · 0 评论 -
leetcode--17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.利用back原创 2017-09-29 14:51:58 · 229 阅读 · 0 评论 -
leetcode--18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.这道题目最主要的关注点:原创 2017-09-30 16:19:36 · 233 阅读 · 0 评论 -
leetcode--19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example,Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, th原创 2017-09-30 22:30:02 · 192 阅读 · 0 评论 -
leetcode--22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()原创 2017-10-01 08:35:24 · 310 阅读 · 0 评论 -
leetcode--24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2017-10-01 09:48:06 · 306 阅读 · 0 评论 -
leetcode--33. Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in原创 2017-10-11 21:12:45 · 242 阅读 · 0 评论 -
leetcode--34. Search for a Range
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the targ原创 2017-10-12 16:21:32 · 234 阅读 · 0 评论 -
LeetCode总结,二分法一般性总结
一,学习别人的总结与讲解本部分的参考见末尾,本部分文字是在其基础上的二度总结(节约时间和精力)。1,典型的二分法算法:当数据量很大适宜采用该方法。采用二分法查找时,数据需是排好序的。 基本思想:假设数据是按升序排序的,对于给定值key,从序列的中间位置k开始比较,如果当前位置arr[k]值等于key,则查找成功;若key小于当前位置值arr[k],则在数转载 2017-10-12 16:42:08 · 341 阅读 · 0 评论 -
leetcode--39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen原创 2017-10-15 18:19:03 · 247 阅读 · 0 评论 -
leetcode--8. String to Integer (atoi)
将string转化为int,要求包括:The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional原创 2017-09-28 17:03:13 · 219 阅读 · 0 评论 -
leetcode--1、15、167 2Sum、3sum
L1:无序数组找出相加为target的两个数特征:无序、无重复、每个元素只能用一次、有且只有一组答案Tag:O(n) hashmap解:Step1:hashmap中,key存储number,value存储index。Step2:在hashmap的key中寻找 target-number。只需常数时间。Step3:一边建表一边比较。如果找到原创 2017-09-23 16:52:48 · 260 阅读 · 0 评论 -
leetcode--BinarySearch
L35:常规二分查找 L278:常规二分查找 L374:常规二分查找 L367:perfect number判断一个数n是否是某个整数的平方方1:N-1-3-5-7-9-…… == 0 ? True:false 方2:X=nWhile x*x>n 则更新x=(x+n/x)/2原创 2017-09-23 16:55:34 · 319 阅读 · 0 评论 -
leetcode--PascalTriangle
L118:建立一个杨辉三角 L119:将杨慧三角的第n行取出原创 2017-09-23 16:57:03 · 196 阅读 · 0 评论 -
leetcode--66.Plus One
L66:将一个整数用一个数组表示,将这个整数+1。Tips:问题点在于是否进位。若进行到某一下标i,nums[i]vectorplusOne(vector& digits) { int len = digits.size(); for(int i = len-1; i>=0; i--){ if(digits[原创 2017-09-23 17:04:12 · 264 阅读 · 0 评论 -
leetcode--414.Third Maximam
L414:找出一组数中,第三大的数。Tips:将max1,max2,max3均设为-1.如果max1为-1 或 新来的数一直等于max1,则不做任何变化。如果max2为-1 或 新来的数一直等于max2,则更新max1和max2.如果max3为-1 或 新来的数大于max3,更新max1,max2,max3.原创 2017-09-23 17:05:37 · 292 阅读 · 0 评论 -
leetcode--Remove Elements
Two Pointers L26:RemoveDuplicatesFromSortedArray有序数组,将重复的数删除(放在数组尾端),并返回新数组长度。Tips:For循环,用i遍历数组。只有nums[i]!=nums[i-1]的时候,进行交换操作,其他时候无操作。 L27:Remove Element将某一个原创 2017-09-23 17:07:47 · 301 阅读 · 0 评论 -
leetcode--Contain Duplicate
L217:ContainDuplicates如果数组含有相同的数,返回trueHashtable L219:数组含有相同数,且两个下标的绝对值不大于k坑:1、如果检测出下标大于k,记得更新下标。例如[1,0,1,1]。更新下标的操作可以与建表合并。hashmap[nums[i]] = i; //这个操作既可以更新表,又可以原创 2017-09-23 17:09:11 · 268 阅读 · 0 评论 -
leetcode--532.K-DiffPairsInAnArray
L532:在一个数组中,找到绝对值为k的两个数,统计共有多少对这样的pairs。Tips:HashTable 坑:1、empty check2、k是否大于03、{1,3}和{3,1}是同一种组合 解决3:如果k==0,那么当相同的数过来的时候,需要二次统计,例如[1,1,1,1],但是仅进行1次。如果k!=0,原创 2017-09-23 17:10:15 · 239 阅读 · 0 评论 -
leetcode--189.Rotate
L189:将一个数组里的数,向右移动 k位。(shift)。要求多种方法。 坑:k可能大于len,所以需要取余。 方案1:将数组复制,依次填入新的位置。新的位置为(i+k)%n 方案2:将数组reverse。将数组第0-k-1位reverse将数租第k-len-1位reverse 方案3:原创 2017-09-23 17:12:15 · 271 阅读 · 0 评论 -
leetcode--169.Major Element
L169:找出出现次数大于数组长度一半的数。 方一:Moore voting利用其余数组加起来都没有这个数字多的特性。记录一个max,记录一个target,凡是不等于target,将max-1。如果max==0,更换target和max。 方二:Hashtable,最方便。 方三:分治算法。Divide原创 2017-09-23 17:13:16 · 357 阅读 · 0 评论 -
leetcode--40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combina原创 2017-10-15 18:52:54 · 264 阅读 · 0 评论