算法
文章平均质量分 70
xiaoliucool1314
这个作者很懒,什么都没留下…
展开
-
[Leetcode] Rotate Array
问题描述: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].Note:Try to come up as many solu原创 2015-04-04 15:19:24 · 405 阅读 · 0 评论 -
【九度OJ--1002】grading
题目描述: Grading hundreds of thousands of Graduate Entrance Exams is a hard work. It is even harder to design a process to make the results as fair as possible. One way is to assign each exam prob原创 2015-04-14 17:12:28 · 478 阅读 · 0 评论 -
【leetcode】Populating Next Right Pointers in Each Node
一、问题描述Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next原创 2015-11-05 18:46:55 · 291 阅读 · 0 评论 -
【leetcode】Missing Number
1、问题描述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, 3] return 2.Note:Your alg原创 2015-11-05 18:57:03 · 300 阅读 · 0 评论 -
【leetcode】Maximum Subarray
一、问题描述Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [原创 2015-11-10 17:44:55 · 276 阅读 · 0 评论 -
【leetcode】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 no duplicat原创 2015-11-13 18:13:41 · 277 阅读 · 0 评论 -
【leetcode】Minimum Path Sum
一、问题描述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.Note: You can only move either down原创 2015-11-23 11:40:32 · 257 阅读 · 0 评论 -
【leetcode】Spiral Matrix II
一、问题描述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 following matrix:[ [ 1, 2, 3 ], [ 8原创 2015-11-23 11:35:57 · 308 阅读 · 0 评论 -
【leetcode】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, ai) and (原创 2015-11-24 11:29:36 · 278 阅读 · 0 评论 -
【leetcode】Convert Sorted Array to Binary Search Tree
一、问题描述Given an array where elements are sorted in ascending order, convert it to a height balanced BST.二、问题分析先复习下什么是二叉搜索树(引自Wikipedia):二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree原创 2015-11-13 17:43:21 · 274 阅读 · 0 评论 -
【leetcode】Kth Smallest Element in a BST
一、问题描述Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.二、问题分析结合BST原创 2015-11-18 17:35:38 · 306 阅读 · 0 评论 -
【leetcode】Game of Life
一、问题描述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 Horton Conway in 1970."Given a b原创 2015-11-27 11:21:14 · 429 阅读 · 0 评论 -
【leetcode】Peeking Iterator
一、问题描述Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element t原创 2015-12-07 11:34:35 · 373 阅读 · 0 评论 -
【leetcode】【53】Maximum Subarray
一、问题描述Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [原创 2016-03-01 11:21:30 · 674 阅读 · 0 评论 -
【leetcode】【54】Spiral Matrix
一、问题描述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:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8原创 2016-03-01 11:31:29 · 290 阅读 · 0 评论 -
【leetcode】【55】Jump Game
一、问题描述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 maximum jump length at that position.原创 2016-03-01 14:24:36 · 410 阅读 · 0 评论 -
【leetcode】【58】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.Not原创 2016-03-01 14:41:01 · 258 阅读 · 0 评论 -
【leetcode】【59】Spiral Matrix II
一、问题描述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 following matrix:[ [ 1, 2, 3 ], [ 8原创 2016-03-02 08:55:48 · 225 阅读 · 0 评论 -
【leetcode】【60】Permutation Sequence
一、问题描述The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132"原创 2016-03-02 09:21:16 · 1071 阅读 · 0 评论 -
【leetcode】【61】Rotate List
一、问题描述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->NULL.二、问题分析从给的例子中很明显理解题意,倒原创 2016-03-02 09:37:46 · 242 阅读 · 0 评论 -
【leetcode】【62】Unique Paths
一、问题描述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 point in time. The robot is trying t原创 2015-11-13 18:19:21 · 324 阅读 · 0 评论 -
【leetcode】【63】Unique Paths II
一、问题描述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 is marked as 1 and 0 respective原创 2016-03-02 09:46:55 · 251 阅读 · 0 评论 -
【leetcode】【64】Minimum Path Sum
一、问题描述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.Note: You can only move either down原创 2016-03-02 09:52:56 · 224 阅读 · 0 评论 -
【leetcode】【66】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.二、问题分析数字原创 2016-03-02 10:00:46 · 263 阅读 · 0 评论 -
【leetcode】【67】Add Binary
一、问题描述Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100"二、问题分析无非还是字符串的遍历问题(从后往前)。需要注意的就是进位。最后的结果可以有多重存储方式,比如字符数组,String ,Stri原创 2016-03-02 10:14:45 · 239 阅读 · 0 评论 -
【leetcode】【69】Sqrt(x)
一、问题描述Implement int sqrt(int x).Compute and return the square root of x.二、问题分析求一个正数的平方根,最笨的方法就是从0到x挨个试。显然会超时,那么重新考虑一下,从0-x这不是一个有序的数组吗?因此可以采用二分查找法。需要注意的就是从0-x求平方的过程可能会溢出,因此平方值用long来存储即可。原创 2016-03-02 10:23:36 · 265 阅读 · 0 评论 -
【leetcode】【70】Climbing Stairs
一、问题描述二、问题分析每一步可以爬一格或者两个楼梯,可以发现,递推式是f(n)=f(n-1)+f(n-2),熟悉的可以一眼发现,这是斐波那契数列的递归表达式。当然我们在处理的时候可以采用迭代的方式优化。三、Java AC代码public int climbStairs(int n) { int pre = 0; int cur = 1; for原创 2016-03-02 10:40:03 · 226 阅读 · 0 评论 -
【leetcode】【71】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 Case原创 2016-03-02 11:14:32 · 420 阅读 · 0 评论 -
【leetcode】【73】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 soluti原创 2015-11-27 11:39:05 · 406 阅读 · 0 评论 -
【leetcode】【74】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 int原创 2015-11-26 14:32:05 · 311 阅读 · 0 评论 -
【leetcode】【75】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 t原创 2015-11-13 18:27:31 · 345 阅读 · 0 评论 -
【leetcode】【77】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]原创 2015-11-26 14:45:38 · 36793 阅读 · 0 评论 -
【leetcode】【78】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.原创 2016-03-03 09:32:50 · 319 阅读 · 0 评论 -
【leetcode】【79】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 o原创 2016-03-03 09:43:57 · 274 阅读 · 0 评论 -
【leetcode】【80】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原创 2016-03-03 10:18:48 · 192 阅读 · 0 评论 -
【leetcode】【81】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 targ原创 2016-03-03 10:48:38 · 212 阅读 · 0 评论 -
【leetcode】【83】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.二、问题分析链表的遍历原创 2016-03-03 10:53:43 · 217 阅读 · 0 评论 -
【leetcode】【86】Partition List
一、问题描述Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the node原创 2016-03-03 11:16:44 · 1075 阅读 · 0 评论 -
【leetcode】【88】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) t原创 2016-03-03 11:20:53 · 258 阅读 · 0 评论 -
【leetcode】【89】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原创 2015-11-17 16:43:15 · 317 阅读 · 0 评论