02 2017 档案
摘要:We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wron
阅读全文
摘要: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 or
阅读全文
摘要:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality c
阅读全文
摘要:Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexi
阅读全文
摘要:Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 本题和之前的105题比较
阅读全文
摘要:Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 本题通过前序和中序来推断这
阅读全文
摘要:一般实际生活中我们遇到的算法分为四类: 一>判定性问题 二>最优化问题 三>构造性问题 四>计算性问题 而今天所要总结的算法就是着重解决 最优化问题 《算法之道》对三种算法进行了归纳总结,如下表所示: 标准分治 动态规划 贪心算法 适用类型 通用问题 优化问题 优化问题 子问题结构 每个子问题不同
阅读全文
摘要:Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twi
阅读全文
摘要:Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n]
阅读全文
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maxim
阅读全文
摘要:Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library functio
阅读全文
摘要: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:
阅读全文
摘要:Numbers can be regarded as product of its factors. For example, Write a function that takes an integer n and return all possible combinations of its f
阅读全文
摘要: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
阅读全文
摘要:Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telepho
阅读全文
摘要:Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
阅读全文
摘要: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
阅读全文
摘要: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 num
阅读全文
摘要:Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums
阅读全文
摘要:Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may a
阅读全文
摘要:Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 此题的想法是遍历一遍haystack数组,当遍历
阅读全文
摘要:字符串匹配的KMP算法 作者: 阮一峰 日期: 2013年5月 1日 作者: 阮一峰 日期: 2013年5月 1日 字符串匹配是计算机的基本任务之一。 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD"? 许多算法可以完成这
阅读全文
摘要:Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can f
阅读全文
摘要:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Example:
阅读全文
摘要:Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. 这道题收到
阅读全文
摘要:Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form
阅读全文
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique pe
阅读全文
摘要:Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,
阅读全文
摘要:回溯法 回溯法有“通用的解题法”之称。用它可以系统地搜索一个问题的所有解或任一解。回溯法是一种即带有系统性又带有跳跃性的搜索算法。它在问题的解空间树中,按深度优先策略,从根节点出发搜索解空间树。算法搜索至解空间树的任一结点时,先判断该节点是否包含问题的解。如果不包含,则跳过对以该节点为根的子树的搜索
阅读全文
摘要:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possib
阅读全文
摘要:The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a
阅读全文
摘要:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
阅读全文
摘要:Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space
阅读全文
摘要:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any p
阅读全文
摘要: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 fo
阅读全文
摘要: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 inde
阅读全文
摘要:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 这道题和之前的Game of Live有些不一样,区别在于,之前那道题目只有两种状态,即矩阵只有0和1两种,并且
阅读全文
摘要:According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John
阅读全文
摘要:Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adja
阅读全文
摘要: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 "adjac
阅读全文
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 此题和之前的find the duplicate number比较像,代码如下:
阅读全文
摘要:Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 本题可以利用之前的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.
阅读全文
摘要:Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements
阅读全文
摘要:Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note:Your algorithm sh
阅读全文
摘要:Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complex
阅读全文
摘要: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 s
阅读全文
摘要: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,
阅读全文
摘要:Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any
阅读全文
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longes
阅读全文
摘要:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the followi
阅读全文
摘要:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: You s
阅读全文
摘要:Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain lea
阅读全文
摘要:Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C pr
阅读全文
摘要: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-
阅读全文
摘要: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]. Not
阅读全文
摘要:Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note: Exam
阅读全文
摘要:There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the hor
阅读全文
摘要:Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference room
阅读全文
摘要:Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all mee
阅读全文
摘要:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initia
阅读全文
摘要:Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 1 /** 2 *
阅读全文
摘要:Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. For example, given the following ma
阅读全文
摘要:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the hist
阅读全文
摘要:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. For example, given the following
阅读全文
摘要:You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 此题可以用3*3的矩阵来
阅读全文
摘要:Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For exampl
阅读全文
摘要:Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges. For example, given [0,
阅读全文
摘要: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 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 public class Solutio...
阅读全文
摘要:Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Example:(1) Given nums = [1, 5, 1, 1, 6, 4], one possible
阅读全文
摘要:Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For exam
阅读全文
摘要:快速排序是一个知名度极高的排序算法,其对于大数据的优秀排序性能和相同复杂度算法中相对简单的实现使它注定得到比其他算法更多的宠爱。 算法概述/思路 快速排序一般基于递归实现。其思路是这样的: 1.选定一个合适的值(理想情况中值最好,但实现中一般使用数组第一个值),称为“枢轴”(pivot)。 2.基于
阅读全文
摘要:Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4],
阅读全文
摘要: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,
阅读全文
摘要:This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2. Given a list of words and two words word1
阅读全文
摘要:This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly ma
阅读全文
摘要:Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words =
阅读全文
摘要:Sort a linked list in O(n log n) time using constant space complexity. 这道题要求用nlogn的时间复杂度去做,代码如下: 本题是典型的merge sort,只不过输入的数据是链表而不是数组了,那么我们需要每次找到中间的位置,要用
阅读全文
摘要:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 本题目我第一个想到了用小顶堆来做,不是很难,直接上代码: /** * Definition for s
阅读全文
摘要:分治算法 一、基本概念 在计算机科学中,分治法是一种很重要的算法。字面上的解释是“分而治之”,就是把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题……直到最后子问题可以简单的直接求解,原问题的解即子问题的解的合并。这个技巧是很多高效算法的基础,如排序算法(快速排序,归并
阅读全文
摘要: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. 此题考查链表的操
阅读全文
摘要: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 tha
阅读全文
摘要: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 us
阅读全文
摘要:Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return public class Solution { public List<List<Integer
阅读全文
摘要:Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose an arra
阅读全文
摘要:Suppose an array sorted in ascending order 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 a
阅读全文
摘要: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
阅读全文
摘要:Suppose an array sorted in ascending order 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
阅读全文
摘要:Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and
阅读全文