- 博客(63)
- 收藏
- 关注
原创 Insertion Sort List
Sort a linked list using insertion sort.思路:使用插入排序的方式将一个链表进行排序。可以根据给定的节点,先返回插入位置,然后将节点插入即可。代码如下所示:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode
2016-05-31 15:58:12 135
原创 Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to
2016-05-31 15:16:13 137
原创 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?思路:找
2016-05-31 14:26:16 160
原创 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?题目:如何判断一个链表是否是含有环的思路:使用两个指针,遍历整个链表,一个走一步,一个走两步。直到两个指针相等且都不为NULL的话则说明这个链表有环/** *
2016-05-30 16:20:37 208
原创 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
2016-05-30 15:29:44 187
原创 Reverse Linked List
Reverse a singly linked list.思路:题目要求是将一个单链表进行逆置遍历整个链表,对于每个访问的节点,使用头插的方式来重新建立整个链表即可/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * Lis
2016-05-27 16:29:43 144
原创 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
2016-05-27 16:04:03 205
原创 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->1-
2016-05-27 15:31:41 374
原创 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.思路:题目要求移除链表中值重复的节点。可以考虑如下
2016-05-27 14:39:57 170
原创 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.思路:题目要求旋转一个链表,向右移动的步数为指定的k。(1)使用两
2016-05-27 10:54:11 186
原创 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
2016-05-27 10:14:29 153
原创 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-link
2016-05-26 15:45:06 136
原创 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 l
2016-05-26 14:46:57 202
原创 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 as a link
2016-05-26 14:14:46 183
原创 Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fi
2016-05-26 08:42:32 242
原创 Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your
2016-05-25 15:51:00 178
原创 Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm should
2016-05-25 15:40:24 143
原创 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).
2016-05-25 15:33:14 138
原创 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.方法一:在一个数组中,出现次数超过【n/3】的元素可能只有两个。因此,可以考虑使用两个候选的cadid
2016-05-25 14:41:21 144
原创 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"].思路:(1)用两个标记,一个指示范围的起始下标,一个指示结束下标(2)符合范围的情况是nums
2016-05-25 11:27:53 141
原创 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 difference between i and j is at most k.思路:在使用的时
2016-05-24 16:54:30 141
原创 Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is
2016-05-24 16:39:18 146
原创 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers wi
2016-05-24 16:11:37 137
原创 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
2016-05-24 15:18:58 151
原创 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].Note:Try to come up as many solutions as you
2016-05-24 14:58:54 176
原创 Majority Element
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-empty and the majority element al
2016-05-23 16:51:29 146
原创 Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, in
2016-05-23 16:29:53 149
原创 Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot unkno
2016-05-23 15:25:25 154
原创 Find Minimum in Rotated Sorted Array
Suppose a sorted array 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).Find the minimum element.You may assume no duplicate exists in th
2016-05-23 15:07:43 136
原创 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest
2016-05-23 14:36:59 149
原创 Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most two transactions.Note:You may
2016-05-23 14:31:52 126
原创 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on
2016-05-23 10:52:57 154
原创 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),
2016-05-23 10:28:53 124
原创 Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[[2],[3,4],[6,5,7],[4
2016-05-20 16:22:09 154
原创 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].Note:Could you optimize your algorithm to use only O(k) extra space?class Solution
2016-05-20 14:38:15 179
原创 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]]方法一:可以将每一行的数字分成两部分。两边都是1,中间的数字是上一
2016-05-20 10:57:32 125
原创 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 dupl
2016-05-19 16:26:28 140
原创 Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold addit
2016-05-19 15:46:02 105
原创 Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the
2016-05-19 15:21:21 106
原创 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 fi
2016-05-19 15:11:19 117
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人