随笔分类 -  LeetCode(Java)

摘要: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... 阅读全文
posted @ 2016-01-26 20:26 Lewisr 阅读(125) 评论(0) 推荐(0)
摘要:Sort Colors:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the i... 阅读全文
posted @ 2016-01-26 20:11 Lewisr 阅读(93) 评论(0) 推荐(0)
摘要: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,... 阅读全文
posted @ 2016-01-26 19:58 Lewisr 阅读(118) 评论(0) 推荐(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... 阅读全文
posted @ 2016-01-26 19:46 Lewisr 阅读(1218) 评论(0) 推荐(0)
摘要:Recorder 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 ... 阅读全文
posted @ 2016-01-18 19:16 Lewisr 阅读(200) 评论(0) 推荐(0)
摘要: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. 题意:对于给定的一个链表,判断其是否有环,如果有环的话,返回环开始的结点。 思路:在I中是判... 阅读全文
posted @ 2016-01-18 19:10 Lewisr 阅读(160) 评论(0) 推荐(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... 阅读全文
posted @ 2016-01-18 19:01 Lewisr 阅读(130) 评论(0) 推荐(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... 阅读全文
posted @ 2016-01-15 21:30 Lewisr 阅读(181) 评论(0) 推荐(0)
摘要:Insertion Sort List:Sort a linked list using insertion sort. 题意:使用插入排序的方式对链表进行排序。 思路:根据插入排序的思路,进行插入操作。 代码: public ListNode insertionSortList(ListNode head) { if(head==null||head.next==null) re... 阅读全文
posted @ 2016-01-15 21:21 Lewisr 阅读(146) 评论(0) 推荐(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? 题意:判断一个链表中是否存在环。 思路:(参考别人的做法)采用“快慢指针”检查链表是否含有环。即让一个指针一次走一步,另一个指针一次走两步,... 阅读全文
posted @ 2016-01-15 21:14 Lewisr 阅读(130) 评论(0) 推荐(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 const... 阅读全文
posted @ 2016-01-15 21:06 Lewisr 阅读(134) 评论(0) 推荐(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 ret... 阅读全文
posted @ 2016-01-15 21:01 Lewisr 阅读(106) 评论(0) 推荐(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->... 阅读全文
posted @ 2016-01-15 20:55 Lewisr 阅读(116) 评论(0) 推荐(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. 题意:合并两个有序的链表,并返回新的链表。 思路:使用双指针,分别指... 阅读全文
posted @ 2016-01-15 20:48 Lewisr 阅读(129) 评论(0) 推荐(0)
摘要:Palindrome Linked List:Given a singly linked list, determine if it is a palindrome. 题意:判断给定的一个链表中数,是否为回文。 思路:参考http://www.bkjia.com/ASPjc/1031678.html中的解法,采用递归的方式进行判断。 代码: private ListNode lst; ... 阅读全文
posted @ 2016-01-14 19:39 Lewisr 阅读(127) 评论(0) 推荐(0)
摘要:Intersection of Two Linked Lists:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the followi... 阅读全文
posted @ 2016-01-14 19:26 Lewisr 阅读(141) 评论(0) 推荐(0)
摘要:Remove Linked List Elements: Remove all elements from a linked list of integers that have value val. Example: Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 –> 5 题意:... 阅读全文
posted @ 2016-01-14 19:16 Lewisr 阅读(108) 评论(0) 推荐(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 n... 阅读全文
posted @ 2016-01-13 19:16 Lewisr 阅读(91) 评论(0) 推荐(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... 阅读全文
posted @ 2016-01-13 18:59 Lewisr 阅读(116) 评论(0) 推荐(0)
摘要:Ugly Number:Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugl... 阅读全文
posted @ 2016-01-13 18:52 Lewisr 阅读(114) 评论(0) 推荐(0)