leetcode
文章平均质量分 62
xdugucc
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
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-09-28 11:03:06 · 225 阅读 · 0 评论 -
Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence.Example 1:Input: [1,3,5,4,7]Output: 3Explanation: The longest continuous increasing subseque原创 2017-09-24 14:56:46 · 251 阅读 · 0 评论 -
Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. * public class ListNode { * int val; * L原创 2017-09-24 15:45:24 · 231 阅读 · 0 评论 -
TeemoAttacking
最近刚开始刷leetcode,先从easy开始刷,不小心看到了TeemoAttacking。点进去一看居然还真是LOL里的提莫。干脆就先把这个medium题做了!In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. No原创 2017-09-14 22:16:40 · 215 阅读 · 0 评论 -
K-diff Pairs In An Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers原创 2017-09-14 21:11:17 · 243 阅读 · 0 评论 -
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.思路:这是一个比较简单的操作链表的原创 2017-09-24 20:20:14 · 247 阅读 · 0 评论 -
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 --> 5Credits:思路:原创 2017-09-25 13:24:35 · 248 阅读 · 0 评论 -
Reverse LinkedList
Reverse a singly linked list.思路:问题描述很简单,翻转一个链表。我们搜索链表,将每一个节点cur的前驱pre作为它的后继。然后继续下一个节点,但是我们在寻找下一个节点的时候(cur = cur.next),因为更改了cur的后继,cur.next反而会指向cur的原前驱节点pre。故我们需要用一个临时的指针temp来保存cur的原后继,在cur修改了后原创 2017-09-25 14:42:54 · 382 阅读 · 0 评论 -
Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?思路:题目要求判断一个链表是否是回文链表。并且规定时间复杂度为o(n),空间复杂度为o(1)。既然要判断一个链表是否回文,并且空间复原创 2017-09-25 15:47:18 · 279 阅读 · 0 评论 -
Delete Node In A LinkedList
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value原创 2017-09-25 21:00:06 · 278 阅读 · 0 评论 -
Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:要判断一个链表是否成环,先想象一下成环的样子,一定是0字型或者9字形,而不会是8字型。想象一下,绕城的环就像是一个操场。有两个人在跑步速度不一样,如果原创 2017-09-25 21:38:08 · 252 阅读 · 0 评论 -
Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i原创 2017-09-25 22:50:26 · 232 阅读 · 0 评论 -
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, the原创 2017-09-28 10:18:55 · 257 阅读 · 0 评论 -
Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.思路:这道题的要求不用多说,看上述的例子就能看懂。说几原创 2017-09-28 19:12:44 · 245 阅读 · 0 评论 -
NonDecreasingArray
Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if array[i] holds for every i (1原创 2017-09-24 14:08:40 · 277 阅读 · 0 评论 -
Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1原创 2017-09-28 19:59:54 · 368 阅读 · 0 评论 -
Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2017-09-28 20:41:16 · 258 阅读 · 0 评论 -
Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy the原创 2017-09-28 21:35:53 · 245 阅读 · 0 评论 -
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings思路:题目的要求是求字符串数组strs中所有字符串的最长公共前缀子串。第一步,先把前缀子串pre 设为strs[0].第二步,判断一个字符串中是否含有另一个字符串可以使用indexof()方法,它返回的是子原创 2017-09-28 22:08:28 · 236 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "原创 2017-09-29 10:10:23 · 272 阅读 · 0 评论 -
Longest Palindromic Substring
原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/思路:这题要求一个字符串的最长回文串,注意到回文串有着中心对称的特点,而且有两种对称方式。一种是字符串所含字符串是单数个,比如aba,另外一种偶数情况,比如baab。那么我们要求字符串的最长回文串,如果字符串为空或者只含一个字符那么直接返原创 2017-11-02 19:07:09 · 390 阅读 · 0 评论 -
Container With Most Water
原题链接:https://leetcode.com/problems/container-with-most-water/description/思路:这道题说白了就是求(j-i)*min(height[j],height[i])的最大值,其中i,j在[0,height.length-1]中。如果暴力破解一共要算n^2个值找出最大的,算法的目的就是减少需要计算的值,也就是说剔除掉肯定不原创 2017-11-03 15:08:28 · 402 阅读 · 0 评论 -
3Sum
原题链接:https://leetcode.com/problems/3sum/description/思路:这道题要找出不重复的合为0的三元组,做过了2Sum那道题会很容易想到借助HashMap,但是笔者尝试过,这样的方式很难解决重复的问题。暴力破解的做法是每次挑出一个元素值叫做a,然后从剩余的数组中找出和为-a的两元素,两个步骤的合在一块的时间复杂度为o(n*n^2)=o(n^3)原创 2017-11-04 12:42:50 · 343 阅读 · 0 评论 -
Letter Combinations of a Phone Number
原题链接:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/思路:输入一串0-9的数字,每个数字对应着电话上的1个或多个字符,要求出所有可能的字符组合,以List形式返回。假设输入“23”,先取第一个字符2,对应的字符串是“abc”,那么此时的List内元素是“原创 2017-11-19 13:35:42 · 422 阅读 · 0 评论 -
Valid Parentheses
原题链接:https://leetcode.com/problems/valid-parentheses/description/思路:括号匹配问题,之前在严蔚敏的数据结构那本书里见到过,C语言版在49页。思路是,将输入的字符串中的每一个符号c1和栈顶符号c2比较,看是否匹配(比如"()"就是匹配,")("或者"({"就是不匹配),如果不匹配,将c1入栈,如果匹配了,将c2原创 2017-11-20 21:39:12 · 338 阅读 · 0 评论 -
java中的访问权限控制
访问权限修饰词java中提供了四种访问权限修饰词,分别为private,default,protected,public。这四种修饰词用来修饰成员变量与方法(下面统称为成员),从前往后访问权限越来越宽。现在分别进行介绍1.privateprivate修饰的成员只能被该类本身所访问,其他类都不可访问到private修饰的成员。2.default默认的权限修饰符,默认的(不加修饰符原创 2017-10-12 19:29:18 · 522 阅读 · 0 评论 -
Generate Parentheses
原题链接:https://leetcode.com/problems/generate-parentheses/description/思路:这道题要求出n对括号能够正确排列的所有情况,重点是要找到符合条件排列的特点。我们可以将长度为2n的排列,看成是n个(与n个)一次次从左往右拼接而成。如果,这个排列是合理的,那么它有着如下的特点:1. 在拼接符号的过程中,(的个数一定是小于原创 2017-11-23 09:21:45 · 331 阅读 · 0 评论 -
ContainsDuplicate
问题描述:检测一个数组中是否含有相同元素解决:因为set中不可添加重复元素的特性,我们可以用set来保存遍历过的元素,一旦set添加新元素失败,表明新元素与已有元素重复。/* * Given an array of integers, find if the array contains any duplicates. * Your function should原创 2017-09-14 09:43:27 · 298 阅读 · 0 评论 -
RemoveDuplicates
问题描述:给一个排好序的数组,要把该数组中所有重复的值删除,并返回删除之后的数组长度。解决:使用双指针来处理,其中一个指针用来遍历。另一个指针用来处理元素。在这个题里,当出现重复元素时,遍历指针往后走,元素不重复时,保存。/* * Given a sorted array, remove the duplicates in place such that each element原创 2017-09-13 19:05:05 · 3317 阅读 · 0 评论 -
RemoveElement
问题描述:一个int数组,将指定元素从数组中删除,返回处理之后的数组长度。解决:双指针来处理。/*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原创 2017-09-13 19:24:05 · 468 阅读 · 0 评论 -
Search Insert Position
问题描述:一个排好序的int数组,给定一个值target。如果数组中有target,返回target的下标,如果target不再数组里,返回其插入进数组的下标。解决:对于排好序的数组,使用二分法查找。/* * Given a sorted array and a target value, return the index if the target is found.原创 2017-09-13 19:35:03 · 256 阅读 · 0 评论 -
maximum subarray
问题描述:一个int数组,求出其最大子串。也就是求出其连续元素和的最大值解决:这是刷Leetcode碰到的第一个动态规划的题。在这个题目,里假设nums[i]到nums[j]这一段连续和最大,那么必然任意的i如果出现小于0的情况,那么nums[k+1]到nums[j]的连续和更大。根据上面的分析,当来到元素nums[i]时,用local表示不包含nums[i]前面的最大子串和,原创 2017-09-13 19:38:04 · 237 阅读 · 0 评论 -
Merged Sorted Array
问题描述:两个排好序的数组nums1,与nums2。要将这两个数组一起排序存储到nums1中,nums1中有n个元素,nums2中有m个元素。nums1的长度比大于等于m+n。解决:题目要求使用nums1来存储而不能新建一个空的数组来保存。如果nums1从nums[0]开始从前往后保存新排序的元素,一定会出现新元素将原始元素覆盖的问题。好在nums1的长度足够,从nums1[n]开原创 2017-09-13 19:57:00 · 268 阅读 · 0 评论 -
PascalTriangle
问题描述:输出指定行数的帕斯卡三角形,要使用List>存储。解决:先知道帕斯卡三角形的特点 1.第一行元素为[1]2.每一行的首尾元素都是13.每一行除首尾以外的元素都是其上一行的左右元素之和。第一点和第二点实现起来都不难,为实现第三点,我们定义pre为上一行的引用,cur为当前行引用。根据pre中元素值来计算出cur。当cur计算完成后,pre=cur来继续计原创 2017-09-13 20:07:44 · 534 阅读 · 0 评论 -
BestTimeToBuyAndSell
问题描述:股票在每天的不同时刻有不同的价格。用int数组来保存股票在一天中不同时刻的价格。要求出当天买卖股票的最大利润。如果价格一直递减,利润为0解决:做过了最大子串那题,能够很明显的感觉到这是一道动态规划问题。如果local表示局部最优,下一个local = max(local + prices[i]-prices[i-1],0)。再每次更新全局最优即可。/* * Say yo原创 2017-09-13 20:58:24 · 234 阅读 · 0 评论 -
PascalTriangel II
问题描述:输出帕斯卡三角形的指定行。解决:这一题与上一题类似,上一题需要输出整个三角形,这里只需要输出指定的一行k。要求空间复杂度为o(k),上一题求出cur行是通过计算pre的元素得到的,这题因为空间复杂度的要求,只能用cur这一个引用。如果仍然像上一题的做法来做,会出现元素覆盖问题。比如给第2行res=[1,2,1],依次计算的结果为[1,3,1原创 2017-09-13 20:32:05 · 277 阅读 · 0 评论 -
plus one
问题描述:对于一个多位的十进制数,用数组保存它的每一位。当这个10进制数加1时,返回新的数组。解决:这题麻烦的地方在于处理进位。我们可以用一个临时变量来保存进位的值,当进位为0时立即停止运算直接返回,当进位为1则任然需要计算。举例:9+1 = 10。我需要保存两个变量,一个是进位值carry=1。一个是加1操作后位上的新元素=0。如果遍历完数组任然carry=1,表明这个数是999原创 2017-09-13 19:44:39 · 358 阅读 · 0 评论 -
Rotate Array
问题描述:对数组进行旋转,举个例子k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]。解决:这道题有挺多解法的,我这里写一种我认为比较巧妙的方法。1.对整个数组翻转[1, 2, 3, 4, 5, 6, 7]->[7, 6, 5, 4, 3, 2, 1]2.对前k个元素翻转[5, 6, 7, 4, 3,原创 2017-09-14 09:39:47 · 300 阅读 · 0 评论 -
MajorityElement
问题描述:一个数组中,寻找出现次数大于数组长度一半的元素。假设这个元素一定存在。解决:可以使用map来存储数据,key为元素的值,value为元素出现的次数。这种方式比较容易想到,是可行的。但是有一种更好的算法叫做Moore's voting。它的思想是:数组中每出现一对不同的元素就将这对元素从数组中删除,如果元素e出现次数过半的,那么最后数组中肯定只剩下e这种元素。/原创 2017-09-13 21:27:16 · 319 阅读 · 0 评论 -
BestTimeToBuyAndSell II
问题描述:可进行多次买卖的情况下,求出一天内买卖股票利润的最大值。解决:其实就是相邻两个元素(后一个元素比前一个元素大)的差值之和。比较简单。/* * Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm t原创 2017-09-13 21:19:16 · 219 阅读 · 0 评论
分享