Leetcode
messiran10
这个作者很懒,什么都没留下…
展开
-
【Leetcode】之 Binary Tree Level Order Traversal II
一.问题描述Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,n原创 2016-08-25 11:54:57 · 263 阅读 · 0 评论 -
【Leetcode】之Add Binary
一.问题描述Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".二.我的解题思路这道题也比较容易,直接按照规则计算即可。测试通过的程序如下:class Solution {public:原创 2016-05-31 12:22:18 · 342 阅读 · 0 评论 -
【Leetcode】之 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-05-06 13:36:46 · 266 阅读 · 0 评论 -
【Leetcode】之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原创 2016-05-17 11:40:29 · 305 阅读 · 0 评论 -
【Leetcode】之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-05-16 11:53:14 · 347 阅读 · 0 评论 -
【Leetcode】之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-05-13 14:49:52 · 292 阅读 · 0 评论 -
【Leetcode】之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-05-25 11:17:43 · 332 阅读 · 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原创 2016-05-10 11:16:47 · 285 阅读 · 0 评论 -
【Leetcode】之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-05-10 10:47:38 · 256 阅读 · 0 评论 -
【Leetcode】之Maximum Product Subarray
一.问题描述Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest原创 2016-03-14 17:30:18 · 305 阅读 · 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 duplicate exists in the array原创 2016-03-13 15:20:10 · 318 阅读 · 0 评论 -
【Leetcode】之 Reverse Words in a String
一.问题描述Given an input string, reverse the string word by word.For example, Given s = “the sky is blue”, return “blue is sky the”.二.我的解题思路java的String类提供了较为丰富的方法,因此直接使用java的方法就可以较好的解决这个问题,如下:public clas原创 2016-03-21 13:56:38 · 310 阅读 · 0 评论 -
【Leetcode】之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].二原创 2016-02-04 11:02:28 · 394 阅读 · 0 评论 -
【Leetcode】之 Combination Sum II
一.问题描述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 combinat原创 2016-01-14 22:07:40 · 272 阅读 · 0 评论 -
【Leetcode】之Combination Sum
一.问题描述Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numbe原创 2016-01-13 11:07:11 · 322 阅读 · 0 评论 -
【Leetcode】之Count and Say
一.问题描述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 “one原创 2016-01-11 13:22:35 · 454 阅读 · 0 评论 -
【Leetcode】之 Permutations
一.问题描述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,1,3], [2,3,1], [3,1,2], and [3,2,1].二.我的解题思路这原创 2016-01-22 10:53:30 · 354 阅读 · 0 评论 -
【Leetcode】之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-05-19 11:46:48 · 436 阅读 · 0 评论 -
【Leetcode】之sqrt(x)
一.问题描述Implement int sqrt(int x).Compute and return the square root of x.二.我的解题思路给定一个整数x,要求解的是一个int型的m,使得该m满足: m*m 最直观的想法自然就是从1开始遍历1,2,...., m-1 。但是这样的复杂度自然是比较高的。为了降低时间复杂度,很自然地想到使用二分法。原创 2016-06-01 11:14:51 · 236 阅读 · 0 评论 -
【Leetcode】之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?二.我的解题思路这道题也很简单,原创 2016-06-01 11:35:48 · 311 阅读 · 0 评论 -
求二进制中1的个数相同、且大小最接近的两个数
转自:http://blog.csdn.net/wangfengfan1/article/details/47957299 (程序员面试金典题解)public int getNext(int n){/*计算c0和c1*/int c=n;int c0=0;int c1=0;while(((c&1)==0) && (c!=0)){转载 2016-08-05 08:43:12 · 833 阅读 · 0 评论 -
【Leetcode】之Validate Binary Search Tree
一.问题描述Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the n原创 2016-08-04 23:50:36 · 276 阅读 · 0 评论 -
【Leetcode】之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原创 2016-07-21 23:05:39 · 317 阅读 · 0 评论 -
【Leetcode】之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-07-20 23:30:08 · 228 阅读 · 0 评论 -
【Leetcode】之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-07-19 22:40:48 · 238 阅读 · 0 评论 -
【Leetcode】之Unique Binary Search Trees
一.问题描述Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2原创 2016-07-28 23:04:06 · 240 阅读 · 0 评论 -
【Leetcode】之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-06-21 11:53:32 · 220 阅读 · 0 评论 -
【Leetcode】之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.二.我的解题思路现在看来,链表的题原创 2016-07-27 22:56:54 · 246 阅读 · 0 评论 -
【Leetcode】之Subsets
一.问题描述Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example,If nums = [1,2,3], a solution is:[原创 2016-06-20 10:37:43 · 301 阅读 · 0 评论 -
【Leetcode】之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]原创 2016-06-16 11:38:08 · 239 阅读 · 0 评论 -
【Leetcode】之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原创 2016-06-16 11:23:50 · 214 阅读 · 0 评论 -
【Leetcode】之Subset II
一.问题描述Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example,If nums = [1原创 2016-07-23 23:55:10 · 259 阅读 · 0 评论 -
【Leetcode】之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原创 2016-06-15 10:56:37 · 234 阅读 · 0 评论 -
【Leetcode】之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.二.我的解题思路对于这道题,我的思路是用空间换时间。首先创建两个数组,int row_idx[row_num]={0}; int line_idx[line_num]={0],然后遍原创 2016-06-14 10:48:24 · 232 阅读 · 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原创 2016-05-23 11:43:20 · 437 阅读 · 0 评论 -
【Leetcode】之 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-05-09 12:00:08 · 462 阅读 · 0 评论 -
【Leetcode】之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原创 2015-12-04 09:44:16 · 315 阅读 · 0 评论 -
【Leetcode】之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原创 2015-12-03 16:40:11 · 329 阅读 · 0 评论 -
【Leetcode】之Jump Game II
一.问题描述iven 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.Your goal is to原创 2016-01-21 10:53:40 · 367 阅读 · 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 (i, 0). Find t原创 2015-10-29 11:02:03 · 303 阅读 · 0 评论