01 2016 档案

摘要: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 ... 阅读全文
posted @ 2016-01-26 20:33 Lewisr 阅读(127) 评论(0) 推荐(0)
摘要: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 阅读(132) 评论(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 阅读(105) 评论(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 阅读(121) 评论(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 阅读(1225) 评论(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 阅读(202) 评论(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 阅读(164) 评论(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 阅读(135) 评论(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 阅读(186) 评论(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 阅读(151) 评论(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 阅读(137) 评论(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 阅读(139) 评论(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 阅读(111) 评论(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 阅读(121) 评论(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 阅读(131) 评论(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 阅读(132) 评论(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 阅读(144) 评论(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 阅读(113) 评论(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 阅读(96) 评论(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 阅读(123) 评论(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 阅读(119) 评论(0) 推荐(0)
摘要:Power of Two: Given an integer, write a function to determine if it is a power of two. 题意:给定一个整数,判断其是否是2幂次方。 思路:如果一个整数是2的幂次方的话,用二进制可以表示为100…的形式,如8可以表示为100。在二进制的表示形式下只有一个1。判断n的2进制中1的个数。 代码: public bool... 阅读全文
posted @ 2016-01-12 19:40 Lewisr 阅读(123) 评论(0) 推荐(0)
摘要: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 ... 阅读全文
posted @ 2016-01-12 19:33 Lewisr 阅读(198) 评论(0) 推荐(0)
摘要:Add Digits: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the proc... 阅读全文
posted @ 2016-01-11 19:40 Lewisr 阅读(131) 评论(0) 推荐(0)
摘要:Reverse Integer:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return –321 题意:反转整数,不改变正负号。 思路: 逐步对给定的整数进行取余和求整,初始化最初的结果为result=0,然后result = result * 10 + 余数。最后注意判断是否溢出... 阅读全文
posted @ 2016-01-11 19:28 Lewisr 阅读(110) 评论(0) 推荐(0)
摘要:Longest Common Prefix:Write a function to find the longest common prefix string amongst an array of strings. 题意:查找一个字符串数组中的字符串的最长公共子序列。 思路:首先查找字符串数组中,字符串长度最小的字符串的索引,然后在逐位判断。 代码: public String longestC... 阅读全文
posted @ 2016-01-10 10:00 Lewisr 阅读(148) 评论(0) 推荐(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... 阅读全文
posted @ 2016-01-10 09:56 Lewisr 阅读(132) 评论(0) 推荐(0)
摘要:Compare Version Numbers: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 b) return 1; if(a<b) return -1; } return 0; ... 阅读全文
posted @ 2016-01-08 22:38 Lewisr 阅读(183) 评论(0) 推荐(0)
摘要:Valid Parentheses:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets m... 阅读全文
posted @ 2016-01-08 22:29 Lewisr 阅读(149) 评论(0) 推荐(0)
摘要:Valid Palindrome:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car... 阅读全文
posted @ 2016-01-07 22:55 Lewisr 阅读(175) 评论(0) 推荐(0)
摘要:Add Binary:Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题意:本题意思很明了,给定2个String,分别存储了用二进制表示的数,实现二进制的加法。 思路:从右向左逐位相加,注意进位即可。 代码: public S... 阅读全文
posted @ 2016-01-07 22:48 Lewisr 阅读(134) 评论(0) 推荐(0)
摘要: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 ji... 阅读全文
posted @ 2016-01-07 22:36 Lewisr 阅读(878) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2016-01-06 23:11 Lewisr 阅读(126) 评论(0) 推荐(0)
摘要: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 ma... 阅读全文
posted @ 2016-01-06 23:00 Lewisr 阅读(158) 评论(0) 推荐(0)
摘要:Move Zeros: 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 e... 阅读全文
posted @ 2016-01-06 22:50 Lewisr 阅读(127) 评论(0) 推荐(0)
摘要: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 ... 阅读全文
posted @ 2016-01-05 19:22 Lewisr 阅读(151) 评论(0) 推荐(0)
摘要:Remove Element:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed.... 阅读全文
posted @ 2016-01-05 14:10 Lewisr 阅读(438) 评论(0) 推荐(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 d... 阅读全文
posted @ 2016-01-05 13:47 Lewisr 阅读(178) 评论(0) 推荐(0)