
编程练习题
文章平均质量分 57
蓝色街灯_BlueLight
编程是一种美德,是促使一个人不断向上发展的一种原动力。
展开
-
LeetCode : First Missing Positive [java]
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 constant原创 2016-03-29 00:09:12 · 321 阅读 · 0 评论 -
LeetCode : Combination Sum II [java]
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combina原创 2016-03-21 10:57:56 · 334 阅读 · 0 评论 -
LeetCode : Count and Say [java]
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as原创 2016-03-11 22:09:51 · 340 阅读 · 0 评论 -
LeetCode : Sudoku Solver [java]
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku原创 2016-03-11 21:23:54 · 320 阅读 · 0 评论 -
LeetCode : Valid Sudoku [java]
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille原创 2016-03-11 20:21:55 · 297 阅读 · 0 评论 -
LeetCode : Search Insert Position [java]
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2016-03-10 23:46:17 · 341 阅读 · 0 评论 -
LeetCode : Search for a Range [java]
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found原创 2016-03-10 23:26:46 · 246 阅读 · 0 评论 -
LeetCode : Search in Rotated Sorted Array [java]
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).You are given a target value to search. If found in the array retur原创 2016-03-10 23:11:13 · 232 阅读 · 0 评论 -
LeetCode : Longest Valid Parentheses [java]
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",原创 2016-03-10 22:09:29 · 440 阅读 · 0 评论 -
LeetCode : Next Permutation [java]
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2016-03-10 19:35:47 · 350 阅读 · 0 评论 -
LeetCode : Substring with Concatenation of All Words [java]
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and w原创 2016-03-09 21:08:57 · 305 阅读 · 0 评论 -
LeetCode : Divide Two Integers [java]
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.思路:注意使用long进行计算,防止int溢出,然后再判断溢出时重置。public class Solution { public int divide(原创 2016-03-09 15:10:06 · 321 阅读 · 0 评论 -
LeetCode : Implement strStr() [java]
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.思路:简单遍历查找即可。public class Solution { public int strStr(String ha原创 2016-03-09 00:35:14 · 278 阅读 · 0 评论 -
LeetCode : Remove Element [java]
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. It doesn't matter what you leave beyond the new length.思原创 2016-03-09 00:06:53 · 256 阅读 · 0 评论 -
LeetCode : Remove Duplicates from Sorted Array [java]
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2016-03-08 20:10:40 · 245 阅读 · 0 评论 -
LeetCode : Reverse Nodes in k-Group [java]
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 as it is原创 2016-03-08 19:33:16 · 348 阅读 · 0 评论 -
LeetCode : Swap Nodes in Pairs [java]
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-03-08 00:43:08 · 427 阅读 · 0 评论 -
LeetCode : Merge k Sorted Lists [java]
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.思路:归并法,主要利用切分简化问题。/** * Definition for singly-linked list. * public class ListNode { * in原创 2016-03-07 23:48:43 · 385 阅读 · 0 评论 -
LeetCode : Generate Parentheses [java]
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()原创 2016-03-07 20:49:57 · 265 阅读 · 0 评论 -
LeetCode : Merge Two Sorted Lists [java]
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. *原创 2016-03-07 20:11:42 · 206 阅读 · 0 评论 -
LeetCode : Valid Parentheses [java]
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va原创 2016-03-07 19:55:32 · 202 阅读 · 0 评论 -
LeetCode : Remove Nth Node From End of List [java]
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原创 2016-03-07 18:43:08 · 321 阅读 · 0 评论 -
LeetCode : 4Sum [java]
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:Element原创 2016-03-07 10:42:10 · 284 阅读 · 0 评论 -
LeetCode : Letter Combinations of a Phone Number [java]
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit st原创 2016-03-06 13:49:06 · 287 阅读 · 0 评论 -
LeetCode : 3Sum Closest [java]
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 have exact原创 2016-03-06 13:18:59 · 285 阅读 · 0 评论 -
LeetCode : 3Sum [java]
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.Note:Elements in a triplet (a,b,c原创 2016-03-06 13:03:45 · 240 阅读 · 0 评论 -
LeetCode : Longest Common Prefix [java]
Write a function to find the longest common prefix string amongst an array of strings.思路:每两个串确定一个最长公共前缀,然后再拿和这个公共前缀和第三个串确定新的最长公共前缀,以此类推。public class Solution { public String longestCommonPrefix(S原创 2016-03-05 22:04:47 · 229 阅读 · 0 评论 -
LeetCode : Roman to Integer [java]
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.思路:记住这种算法,后边的比前边的大,加上后边数-2*前边的数,否则,直接加上后边的数。public class Solution { public int ro原创 2016-03-05 21:33:01 · 323 阅读 · 0 评论 -
LeetCode : Integer to Roman [java]
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.思路:查表法,见程序。public class Solution { public String intToRoman(int num) { St原创 2016-03-05 21:07:58 · 297 阅读 · 0 评论 -
LeetCode : Container With Most Water [java]
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, ai) and (i,原创 2016-03-05 20:53:17 · 300 阅读 · 0 评论 -
LeetCode : Regular Expression Matching [java]
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st原创 2016-03-05 17:43:24 · 356 阅读 · 0 评论 -
LeetCode : Palindrome Number [java]
Determine whether an integer is a palindrome. Do this without extra space.思路:依次判断两头数字是否相同,迭代减小数字。public class Solution { public boolean isPalindrome(int x) { if (x < 0) { return false原创 2016-03-05 17:02:21 · 227 阅读 · 0 评论 -
LeetCode : String to Integer (atoi) [java]
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2016-03-05 16:32:31 · 271 阅读 · 0 评论 -
LeetCode : Reverse Integer [java]
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321思路:使用long类型存储结果,从而可以判断是否溢出,溢出直接返回0.public class Solution { public int reverse(int x) { int ma原创 2016-03-05 16:01:35 · 254 阅读 · 0 评论 -
LeetCode : ZigZag Conversion [java]
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2016-03-05 15:19:16 · 242 阅读 · 0 评论 -
LeetCode : Longest Palindromic Substring [java]
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.(思路:把字符串字符间的间隙考虑进去原创 2016-03-05 12:34:27 · 302 阅读 · 0 评论 -
LeetCode : Median of Two Sorted Arrays [java]
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)).思路:类似于归并排序,找到一半的排好序的序列,最原创 2016-03-05 11:52:19 · 218 阅读 · 0 评论 -
LeetCode : Longest Substring Without Repeating Characters [java]
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 3. Fo原创 2016-03-05 11:01:37 · 233 阅读 · 0 评论 -
LeetCode : Add Two Numbers [java]
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-03-04 00:47:26 · 262 阅读 · 0 评论 -
LeetCode : Tow Sum [java]
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:Given nums =原创 2016-03-03 23:57:45 · 282 阅读 · 0 评论