
leetcode
文章平均质量分 60
花好月圆19
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Rotate Array(leetcode)
题目: 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]. Note: Try to原创 2015-03-04 08:49:09 · 260 阅读 · 0 评论 -
Majority Element (leetcode )
题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-emp原创 2015-02-26 16:29:05 · 225 阅读 · 0 评论 -
3sum(leetcode)
题目: 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.原创 2015-03-13 23:16:44 · 261 阅读 · 0 评论 -
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.If the number of nodes is not a multiple of k then left-out nodes in the end should remain原创 2015-06-17 15:51:16 · 253 阅读 · 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.解题思路:# Definition for singly-linked list.# c原创 2015-06-17 16:02:24 · 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 --> 5代码:# Definition原创 2015-06-18 14:20:42 · 222 阅读 · 0 评论 -
Merge k Sorted Lists
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.解题思路:(1)、二分法(accept)此法比较巧妙,当时没有想起用这个方法。复杂度为O(nlgn)# Definition for singly-linked list原创 2015-06-18 10:01:32 · 223 阅读 · 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 i原创 2015-06-19 09:21:02 · 237 阅读 · 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.代码:此题简单,但未一次通过原创 2015-06-18 11:07:46 · 259 阅读 · 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原创 2015-06-18 10:55:41 · 235 阅读 · 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.Gi原创 2015-06-18 11:24:18 · 273 阅读 · 0 评论 -
Add Two Numbers
题目:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it原创 2015-06-02 16:06:55 · 265 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
题目:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.解题思路:具体思路即为,每次查找到中间的节点,将其作为树的根节点,以该点切分,其左侧以及右侧皆采用此方法,最终即构建出来所需的树。边界条件需要多加注意。此题思路原创 2015-06-20 16:27:25 · 326 阅读 · 0 评论 -
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 hav原创 2015-06-02 11:04:32 · 246 阅读 · 0 评论 -
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)).解题思路:原创 2015-06-04 09:37:02 · 263 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is原创 2015-06-04 08:35:19 · 256 阅读 · 0 评论 -
Longest Palindromic Substring
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.解题思路:原创 2015-06-05 10:04:13 · 258 阅读 · 0 评论 -
Reverse Integer
题目: Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321解题思路:此题极为简单,但有一个小陷阱。附链接https://leetcode.com/discuss/28079/one-python-test-case-i原创 2015-06-05 20:15:27 · 232 阅读 · 0 评论 -
Palindrome Number
题目:Determine whether an integer is a palindrome. Do this without extra space.解题思路: 此题有两种解法,一种可以用递归方式,很容易理解。另一张就是迭代方式。由于此题要求不能有额外的空间开销。故使用迭代,但在此说明一下 (1)、 递归方式class Solutio原创 2015-06-06 15:06:39 · 227 阅读 · 0 评论 -
Two Sum
题目:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target原创 2015-05-29 21:29:14 · 256 阅读 · 0 评论 -
Plus One
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list.解题思路:原创 2015-06-09 17:20:12 · 232 阅读 · 0 评论 -
Contains Duplicate II
题目:Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at原创 2015-06-30 10:24:49 · 242 阅读 · 0 评论 -
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 Solution: # @param {inte原创 2015-07-01 11:22:13 · 343 阅读 · 0 评论 -
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原创 2015-06-11 15:51:02 · 279 阅读 · 0 评论 -
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.解题思路:class Solution: # @param {in原创 2015-07-01 10:46:06 · 373 阅读 · 0 评论 -
Subsets
题目:Given a set of distinct integers, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For e原创 2015-06-10 21:25:31 · 237 阅读 · 0 评论 -
Subsets II
题目:Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain原创 2015-06-10 21:46:20 · 246 阅读 · 0 评论 -
Reverse Linked List
题目:Reverse a singly linked list.Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?解题思路:(1)、迭代版分析以上代码,一开始时我是将head=head.next放在一原创 2015-06-12 15:39:55 · 187 阅读 · 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.解题思路:我在解此题时,遇到的最大问题是没能完全原创 2015-06-12 11:08:00 · 246 阅读 · 0 评论 -
Count Primes
题目:Count the number of prime numbers less than a non-negative number, n.解题思路:思路一(Time limit Exceed):class Solution: # @param {integer} n # @return {integer} def countPrimes(se原创 2015-07-02 09:37:58 · 303 阅读 · 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原创 2015-06-14 09:48:09 · 224 阅读 · 0 评论 -
Basic Calculator
题目:Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers原创 2015-08-22 21:00:35 · 227 阅读 · 0 评论 -
Basic Calculator II
题目:Implement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer divisi原创 2015-08-23 20:44:06 · 212 阅读 · 0 评论 -
Implement Queue using Stacks
题目:Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front eleme原创 2015-07-29 14:22:30 · 223 阅读 · 0 评论 -
Delete Node in a Linked List
题目: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原创 2015-07-29 16:33:18 · 257 阅读 · 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?解题思路:可以分为以下几步:1、计算链表的长度L,直接遍历即可。2、若L为0或1,则是回文的链表。3、原创 2015-07-29 16:12:56 · 225 阅读 · 0 评论 -
Length of Last Word
题目:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note:原创 2015-07-29 21:36:14 · 214 阅读 · 0 评论 -
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 ofnums except nums[i].Solve it without division原创 2015-08-26 19:24:16 · 255 阅读 · 0 评论 -
First Missing Positive
题目:Given an unsorted integer array, find the first missing positive integer.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原创 2015-08-26 16:15:11 · 226 阅读 · 0 评论 -
Reverse Bits
题目:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as0原创 2015-07-31 10:39:07 · 218 阅读 · 0 评论