![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
yittah
这个作者很懒,什么都没留下…
展开
-
LeetCode 189. Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 意思是旋转数组。要求是在额外空间O(1)中完成。解法1:将需要旋转的数组前面部分往后面移动,同时用对应的数组...原创 2018-02-26 22:49:01 · 110 阅读 · 0 评论 -
LeetCode234. Palindrome Linked List
题目:判断一个单链表是否是回文的。要求是O(n)的时间和O(1)的额外空间。思路:前面有一道题是reverse单链表,我们可以想,如果把链表倒置了,那么就很好比了。然而这里直接用reverse的方法其实是不符合要求的,因为我们会建造一个新的链表,已经用了不止O(1)的空间了。那么可以将原reverse函数稍做修改,无需将前面的结点链接在本结点之后了,同时我们需要另一个指针,这个指针在第一个指针顺利...原创 2018-03-04 16:34:01 · 79 阅读 · 0 评论 -
LeetCode235. Lowest Common Ancestor of a Binary Search Tree
题目:在BST中找某两个结点的最近共同祖先。思路:当遍历到某个结点时,倘若这两个目标结点的值刚好一个在该结点左边一个在该结点的右边,或者这两个目标结点的值存在与当前结点相等的,那么这个结点肯定是他们的共同最近祖先结点。所以做法也很简单,遍历整个二叉树,若两个结点的值都比当前结点值小,就遍历当前结点的左子树,如果都大于当前结点的值,就遍历当前结点的右子树。直到找到符合要求的结点为止。...原创 2018-03-04 17:09:06 · 79 阅读 · 0 评论 -
LeetCode237. Delete Node in a Linked List
再只知道一个结点的情况下,删除单链表中的这个结点。做法:将此结点之后的结点全部前移一个就好了。或者原创 2018-03-04 19:40:44 · 81 阅读 · 0 评论 -
LeetCode231. Power of Two
题目:判断一个整数n是否为2的幂次思路:将n按2倍缩小,判断其对2的余数,如果余数不为0,肯定不是2的倍数,注意0和1需要特殊判断。第二种方法是用按位与,加入一个数是2的倍数,那么这个数的二进制表示一定是最高位为1,其余位全为0,而n-1就是最高位为0,其余位全为1,这样的话n&n-1的结果就为0,以此来判断这个数是否为2的幂次十分高效。...原创 2018-03-04 14:55:24 · 143 阅读 · 0 评论 -
LeetCode232. Implement Queue using Stacks
题目:使用栈来构建一个队列只需要保持栈里的元素的顺序与队列里的顺序一致,就能正常地做各种操作了。只需要在push的时候就做到这一点。两种做法,一是使用两个栈,在push的时候先将栈1中的元素全部压倒栈2中,这时候再把目标元素压入栈1,然后将栈2中的元素全部取回即可。做法2,只使用一个栈,但2需要用递归操作来完成元素的取出于放回,栈本身就是递归的,所以这个也不难理解。...原创 2018-03-04 15:12:09 · 67 阅读 · 0 评论 -
LeetCode242. Valid Anagram
题目:判断两个字符串是否由相同的字符构成。(每个字符出现的次数相同,顺序不一定相同)。做法:ascll编码的字符共有256个,可以建立两个256大小的数组,用来保存对应编号的字符出现的次数,最后只需要比较一遍两个数组中字符出现的次数是否相同即可。还有一种办法是将两个字符串都排序,最后满足要求的两个字符串相等。不过这样的话用时会较长。...原创 2018-03-04 20:06:20 · 80 阅读 · 0 评论 -
LeetCode268. Missing Number
题目大意:数组中保存着从0到n的数字,中间有一个缺失的数字,找出它。使用O(1)的空间,O(N)的时间。做法:如果没有那个缺失的数字,数组中的数字应该是从0到n-1的每个数字,此时数组的和为n*(n-1)/2;当有一个数字缺失时,缺失的那个数字由n来补上,于是数组的和增加了(n-缺失数字的值)。按照这个原理,只需要对数组求和就可以了。...原创 2018-03-04 20:44:00 · 102 阅读 · 0 评论 -
LeetCode19. 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 en...原创 2018-03-24 13:39:10 · 80 阅读 · 0 评论 -
LeetCode22. 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:[ "((()))", "(()())", "(())()", "()(())", "...原创 2018-03-24 15:50:31 · 78 阅读 · 0 评论 -
LeetCode24. 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 ...原创 2018-03-24 16:12:10 · 89 阅读 · 0 评论 -
LeetCode12.Integer To Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.题目还是很简单的,主要在于这些数都在3999之内,也就是说最大的千位上的表示就是MMM,而且每一位之间的表示是并不互相影响的。由于数量很小,完全可以穷举。个位上的只有"",I","II",...原创 2018-03-20 22:24:56 · 87 阅读 · 0 评论 -
LeetCode15 3sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.这个题的主要难度在于消除重复,我们其实很容易找到所有加起来等于0的三个数的集合,但是要控制...原创 2018-03-20 22:32:51 · 100 阅读 · 0 评论 -
LeetCode16. 3sum closet
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 exactly...原创 2018-03-20 22:38:45 · 130 阅读 · 0 评论 -
LeetCode25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of no...原创 2018-03-30 18:04:01 · 388 阅读 · 0 评论 -
LeetCode4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).意思是在O(log(m+n))中,找出两个已经排好序的数组的...原创 2018-03-30 21:38:05 · 100 阅读 · 0 评论 -
LeetCode17. 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.Input:Digit string "23"...原创 2018-03-21 16:07:39 · 97 阅读 · 0 评论 -
LeetCode226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1用递归就可以轻松的实现,把每个结点的左右交换一下就好了。...原创 2018-03-03 16:34:15 · 82 阅读 · 0 评论 -
LeetCode225. Implement Stack using Queues
题目意思是,只能使用数据结构queue来实现一个栈。有两种实现方式,第一种,使用两个队列:栈和队列的不同在于栈是先进后出,而队列是先进先出的。所以栈的top值,是对应队列里的最后一个值,因此每次求top或者需要pop的时候,先把队列里的元素全部移到另一个队列里,只剩最后一个时,就是栈的top,然后再移动回来。如果只使用一个队列实现的话,就需要每次将队列的front压到队列的末尾,然后把front给...原创 2018-03-03 15:32:32 · 133 阅读 · 0 评论 -
LeetCode2.Add Two Numbers
ExampleInput: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8Explanation: 342 + 465 = 807.题目:用两个链表分别表示倒着两个数,返回这两个数的和,也用链表倒着表示。做法:倒着表示的话,相当于两个数从链表头加起,就是从个位开始加起,如果大于10的话就有进位,这时候,保存...原创 2018-03-06 19:28:02 · 106 阅读 · 0 评论 -
LeetCode3. Longest Substring Without Repeating Characters
题目大意:找一个字符串中没有重复字母的最大子串。做法1:取一个变量ans用来保存当前已经查看到的子串的最大长度,用map来保存当前查看到的子串中的字符的位置,如果下一个查看到的字符被判定为曾在map中出现过的话,说明当前查看到的最大子串在上一个字符已经结束,清空map从这个重复的字符第一次出现的位置重新开始计算。做法2:道理和做法1一模一样,只不过这里引入一个start值来substring保存开...原创 2018-03-06 22:53:01 · 105 阅读 · 0 评论 -
LeetCode191. Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000000000...原创 2018-02-28 22:53:55 · 143 阅读 · 0 评论 -
LeetCode5.Longest Palindromic Substring
题目大意:找出一个字符串中包含的最长的回文字符串。做法:首先建立一个二维数组isPd[][]来保存从下标i到下标j之间的字符串是否时回文字符串(设j>i),如果是则为1,否则为0.那么问题来了,如何判断一个字符串是回文字符串呢,分两种情况:如果s[i]=s[j],有1.j-i<3,这时候要么是aa这种字符串,要么是aba这种情况,而这两种情况都是回文字符串;2.当j-i>3时,有...原创 2018-03-09 20:09:09 · 90 阅读 · 0 评论 -
LeetCode6. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I G...原创 2018-03-09 21:35:35 · 76 阅读 · 0 评论 -
LeetCode198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house...原创 2018-03-02 17:11:41 · 108 阅读 · 0 评论 -
LeetCode190. Reverse Bits
题目要求是将一个整数的二进制表示法倒过来,形成一个新的数,求这个数。思路很简单,采用基数除法,每次可以获得这个数的二进制表示的某一位上的数,而且获得顺序是从低位到高位的,而我们需要将这个二进制数倒过来的话,其实也就是从低位逐渐读到高位,所以可以直接获得这个新的二进制数,并逐步计算成新的整数。...原创 2018-03-02 17:19:06 · 99 阅读 · 0 评论 -
LeetCode11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two ...原创 2018-03-10 16:24:43 · 76 阅读 · 0 评论 -
LeetCode202. Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of ...原创 2018-03-02 18:24:54 · 76 阅读 · 0 评论 -
LeetCode203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5移除链表中...原创 2018-03-02 18:50:03 · 92 阅读 · 0 评论 -
LeetCode204. Count Primes
Count the number of prime numbers less than a non-negative number, n.亲测,如果用朴素的判断素数的办法加上计数的话,是会超时的,所以这里使用的是欧拉筛法,能在O(N)中解决问题。欧拉筛法,就是筛除某些数,这些数是已经找出的素数的倍数,因为这些素数的倍数不可能会是素数了。具体做法是,开两个数组,一个prim数组用来保存已经找到的素数...原创 2018-03-02 21:47:09 · 135 阅读 · 0 评论 -
LeetCode205. Isomorphic Strings
题目大意是判断两个字符串是不是同一个类型的字符串,两个字符串只要格式相同就视为一种字符串,比如egg和add都是abb格式的,就是一种字符串。基本思路就是,要比较两个字符串,其实主要在于比较重复的字符所在的位置是否一样,所以做法是,建立两个位置数组,用来保存每个字符在对应字符串中所出现的位置,如果在字符串中遇到了第一个重复的字符,而它上一次出现的位置与另一个字符串对应字符上一次所出现的位置不同,说...原创 2018-03-03 13:21:59 · 66 阅读 · 0 评论 -
LeetCode206. Reverse Linked
题目要求是将一个单链表倒置。可以采用递归的方式,即,从链表最后一个元素起,逐渐将其前面的数链接到其后面。比如说1->2->3,当查看到1的时候,只要2及之后的链表已经完成reverse,就可以直接设置2->1,并且返回本结点的值到递归的上一层。注意:1.需要提前保存最后一个结点,便于最后的返回。2.完成倒装后要设置head->next=NULL,以避免无休止的计算。...原创 2018-03-03 14:30:47 · 108 阅读 · 0 评论 -
LeetCode217. Contains Duplicate
题目意思是判断这个数组中是否有重复的数字。只要记录每个数字的出现就好了,每次查看一个新的数字时,率先判断map中是否已经出现过这个数字,如果已经出现则返回true。...原创 2018-03-03 14:54:24 · 79 阅读 · 0 评论 -
LeetCode219. Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.思...原创 2018-03-03 15:02:38 · 78 阅读 · 0 评论 -
LeetCode18. 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.做法1:基于3sum的做法,嵌个套找每个数对应的...原创 2018-03-21 22:39:49 · 79 阅读 · 0 评论