leetcode
_我们的存在
这个作者很懒,什么都没留下…
展开
-
LeetCode 073 Set Matrix Zeroes
题目描述Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up: Did you use extra space? A straight forward solution using O(mn) sp原创 2015-10-17 19:38:23 · 656 阅读 · 0 评论 -
LeetCode 074 Search a 2D Matrix
题目说明Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of each ro原创 2015-10-17 20:24:43 · 608 阅读 · 0 评论 -
LeetCode 019 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 linked lis原创 2015-11-04 09:32:30 · 413 阅读 · 0 评论 -
LeetCode 016 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 have exac原创 2015-11-04 08:36:50 · 677 阅读 · 0 评论 -
LeetCode 017 Letter Combinations of a Phone Number
题目描述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 string “2原创 2015-11-04 08:57:57 · 456 阅读 · 0 评论 -
LeetCode 020 Valid Parentheses
题目描述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 valid b原创 2015-11-04 09:43:33 · 386 阅读 · 0 评论 -
LeetCode 014 Longest Common Prefix
题目描述Write a function to find the longest common prefix string amongst an array of strings.代码 public static String longestCommonPrefix(String[] strs) { if (strs == null || strs.length == 0) {原创 2015-11-03 20:42:10 · 446 阅读 · 0 评论 -
LeetCode 066 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.代码public class Solution {原创 2015-10-03 09:54:18 · 896 阅读 · 2 评论 -
LeetCode 067 Add Binary
题目描述Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”.代码 public static String addBinary(String a, String b) { if (a == null || b原创 2015-10-04 22:25:04 · 854 阅读 · 0 评论 -
LeetCode 021 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.代码 public ListNode mergeTwoLists(ListNode l1, ListNod原创 2015-11-05 14:53:14 · 820 阅读 · 0 评论 -
LeetCode 024 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. You ma原创 2015-11-05 15:19:56 · 325 阅读 · 0 评论 -
LeetCode 069 Sqrt(x)
题目描述Implement int sqrt(int x).Compute and return the square root of x.代码 public static int mySqrt(int x) { // 首先对负数和0进行处理 if (x < 0) { return -1; } else if (x == 0原创 2015-10-05 17:07:45 · 495 阅读 · 0 评论 -
LeetCode 068 Text Justification
题目描述Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is原创 2015-10-05 16:57:17 · 448 阅读 · 0 评论 -
LeetCode 022 Generate Parentheses
题目描述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:“((()))”, “(()())”, “(())()”, “()(())”, “()()()”代码原创 2015-11-05 15:02:27 · 443 阅读 · 0 评论 -
LeetCode 070 Climbing Stairs
题目描述You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?分析斐波那契数列的典型应用。0级台阶:0 1级台阶:1 2级原创 2015-10-06 11:33:32 · 329 阅读 · 0 评论 -
LeetCode 072 Edit Distance
题目描述Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a原创 2015-10-07 15:05:14 · 461 阅读 · 0 评论 -
LeetCode 071 Simplify Path
题目描述Given an absolute path for a file (Unix-style), simplify it.For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” click to show corner cases.Corner Cases: Did you consider原创 2015-10-06 15:47:47 · 407 阅读 · 0 评论 -
LeetCode 026 Remove Duplicates from Sorted Array
题目描述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原创 2015-11-06 15:56:40 · 626 阅读 · 0 评论 -
LeetCode 028 Implement strStr()
题目描述Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.代码原创 2015-11-06 16:08:11 · 422 阅读 · 0 评论 -
LeetCode 027 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. It doesn’t matter what you leave beyond the new length.代码原创 2015-11-06 16:01:23 · 617 阅读 · 0 评论 -
LeetCode 029 Divide Two Integers
题目描述Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.代码 public int divide(int dividend, int divisor) { if (divisor == 0) {原创 2015-11-06 16:32:30 · 463 阅读 · 0 评论 -
LeetCode 075 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 integers 0原创 2015-10-23 09:52:37 · 649 阅读 · 0 评论 -
LeetCode 077 Combinations
题目描述Given two integers n and k, return all possible combinations of k numbers out of 1 … n.For example, If n = 4 and k = 2, a solution is: [2,4], [3,4], [2,3], [1,2], [1,3], [1,4原创 2015-10-23 10:47:42 · 517 阅读 · 0 评论 -
LeetCode 080 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 five el原创 2015-10-24 08:25:35 · 550 阅读 · 0 评论 -
LeetCode 034 Search for a Range
题目描述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 in th原创 2015-11-10 08:54:05 · 638 阅读 · 0 评论 -
LeetCode 082 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->原创 2015-10-25 09:18:13 · 448 阅读 · 0 评论 -
LeetCode 081 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 arra原创 2015-10-24 08:38:19 · 629 阅读 · 0 评论 -
LeetCode 083 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.代码/** * Definition for sing原创 2015-10-25 09:39:57 · 427 阅读 · 0 评论 -
LeetCode 088 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 additi原创 2015-10-25 10:00:39 · 532 阅读 · 0 评论 -
LeetCode 089 Gray Code
题目描述The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of g原创 2015-10-25 10:19:44 · 421 阅读 · 0 评论 -
LeetCode 090 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 dupli原创 2015-10-25 10:46:15 · 718 阅读 · 0 评论 -
LeetCode 036 Valid Sudoku
题目描述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 ‘.’.Note: A valid Sudoku board原创 2015-11-10 09:35:07 · 490 阅读 · 0 评论 -
LeetCode 035 Search Insert Position
题目描述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.H原创 2015-11-10 08:59:44 · 638 阅读 · 0 评论 -
LeetCode 078 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 example,原创 2015-10-23 17:18:21 · 680 阅读 · 0 评论 -
LeetCode 079 Word Search
代码Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically ne原创 2015-10-24 08:06:16 · 549 阅读 · 0 评论 -
LeetCode 092 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 followin原创 2015-10-26 09:42:38 · 427 阅读 · 0 评论 -
LeetCode 093 Restore IP Addresses
题目描述Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example: Given “25525511135”,return [“255.255.11.135”, “255.255.111.35”]. (Order does原创 2015-10-26 12:46:22 · 471 阅读 · 0 评论 -
LeetCode 043 Multiply Strings
题目描述Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.分析代码1 public String multiply(String num原创 2015-11-11 09:38:13 · 580 阅读 · 0 评论 -
LeetCode 046 Permutations
题目描述Given a collection of numbers, return all possible permutations.For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].分析代码 public List<原创 2015-11-11 10:00:28 · 891 阅读 · 0 评论 -
LeetCode 047 Permutations II
题目描述Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].分析使原创 2015-11-11 10:16:26 · 505 阅读 · 0 评论