
leetcode
tterminator
let the code say
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode:Tenth Line 【shell】
一、问题描述How would you print just the 10th line of a file?For example, assume that file.txt has the following content:Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10Your sc原创 2016-04-24 22:55:37 · 616 阅读 · 0 评论 -
leetcode:Single Number II 【Java】
一、问题描述 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it原创 2016-03-05 22:31:03 · 382 阅读 · 0 评论 -
leetcode:Jump Game II 【Java】
一、问题描述 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-05 23:04:56 · 337 阅读 · 0 评论 -
leetcode:Best Time to Buy and Sell Stock 【Java】
一、问题描述 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of原创 2016-03-06 10:23:35 · 324 阅读 · 0 评论 -
leetcode:Best Time to Buy and Sell Stock II 【Java】
一、问题描述 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like原创 2016-03-06 10:35:44 · 337 阅读 · 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 (原创 2016-03-06 11:14:44 · 371 阅读 · 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原创 2016-03-06 11:28:56 · 433 阅读 · 0 评论 -
leetcode:Spiral Matrix II 【Java】
一、问题描述 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-06 14:18:16 · 346 阅读 · 0 评论 -
leetcode:Spiral Matrix 【Java】
一、问题描述 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-06 15:02:44 · 353 阅读 · 0 评论 -
leetcode:Pascal's Triangle 【Java】
一、问题描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 二、问题分析 从上往下原创 2016-03-06 17:57:21 · 368 阅读 · 0 评论 -
leetcode:Pascal's Triangle II 【Java】
一、问题描述 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 use only O(k) extra space? 二、原创 2016-03-06 18:50:26 · 433 阅读 · 0 评论 -
leetcode:Binary Tree Inorder Traversal 【Java】
一、问题描述(二叉树中序遍历) Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. No原创 2016-03-07 11:10:16 · 480 阅读 · 0 评论 -
leetcode:Binary Tree Level Order Traversal 【Java】
一、问题描述 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \原创 2016-03-07 14:20:19 · 378 阅读 · 0 评论 -
leetcode:Binary Tree Level Order Traversal II 【Java】
一、问题描述 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原创 2016-03-07 14:33:32 · 393 阅读 · 0 评论 -
Binary Tree Zigzag Level Order Traversal 【Java】
一、问题描述 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: G原创 2016-03-07 15:12:25 · 381 阅读 · 0 评论 -
leetcode:Balanced Binary Tree 【Java】
一、问题描述 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node原创 2016-03-07 15:38:08 · 320 阅读 · 0 评论 -
leetcode:Minimum Depth of Binary Tree 【Java】
一、问题描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 二、问题分析 利用迭代思想。 三、算法代码原创 2016-03-07 16:00:46 · 326 阅读 · 0 评论 -
leetcode:Rotate Array 【Java】
一、问题描述 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 sol原创 2016-03-07 16:17:29 · 292 阅读 · 0 评论 -
leetcode:Single Number 【Java】
一、问题描述 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it witho原创 2016-03-05 21:37:14 · 380 阅读 · 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 i原创 2016-03-05 21:27:31 · 391 阅读 · 0 评论 -
leetcode:Jump Game 【Java】
一、问题描述 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-05 21:08:48 · 427 阅读 · 0 评论 -
leetcode:Merge Sorted Array 【Java】
从后往前合并。 public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int a_index = m - 1; int b_index = n - 1; int a_cur = m + n - 1; while(原创 2016-03-03 14:58:18 · 629 阅读 · 0 评论 -
leetcode:Maximum Depth of Binary Tree 【Java】
一、问题描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 二、问题分析 利用递归思想。 三、算法代码原创 2016-03-07 15:47:17 · 339 阅读 · 0 评论 -
leetcode---Majority Element
//利用寻找发帖水王解决方法 public class Solution { public int majorityElement(int[] nums) { int cadinate = 0; int ntimes = 0; for(int i = 0; i < nums.length; i++){ if(ntime原创 2015-12-14 14:58:56 · 585 阅读 · 0 评论 -
leetcode:Search Insert Position 【Java 】
public class Solution { public int searchInsert(int[] nums, int target) { int start = 0; int end = nums.length - 1; int middle = 0; while(start <= end){ mi原创 2016-03-03 13:21:54 · 486 阅读 · 0 评论 -
leetcode:Search for a Range 【Java】
利用二分查找算法查找target,再结合计数器查找区间(注意:假设i > j,则i和j之间的元素个数count = i - j + 1)。 public class Solution { public int[] searchRange(int[] nums, int target) { int [] result = new int[]{-1, -1};原创 2016-03-03 14:04:29 · 421 阅读 · 0 评论 -
leetcode:Search a 2D Matrix 【Java】
重要的是把线性下转换成矩阵元素下标 public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int m = matrix.length; int n = matrix[0].length; int start = 0; int end = m * n -原创 2016-03-03 14:29:33 · 339 阅读 · 0 评论 -
leetcode:Merge Two Sorted Lists 【Java】
原链表已经从小到大排好序,合并后产生的链表也要保持从小到大的顺序。 public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head = new ListNode(-1); ListNode cur = head;原创 2016-03-03 16:24:22 · 379 阅读 · 0 评论 -
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 u原创 2016-03-03 19:37:40 · 402 阅读 · 0 评论 -
leetcode:Merge k Sorted Lists 【Java】
一、问题描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 二、解题要点 利用分治法,并结合算法Merge Two Sorted Lists 三、算法代码 public class Solution { public原创 2016-03-03 17:31:49 · 363 阅读 · 0 评论 -
leetcode:Sort Colors 【Java】
一、问题描述 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-03-04 10:04:33 · 444 阅读 · 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 "()[]{}"原创 2016-03-04 10:46:41 · 430 阅读 · 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原创 2016-03-04 14:42:46 · 484 阅读 · 0 评论 -
leetcode:Reverse Integer 【Java】
一、问题描述 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 二、问题分析 1、保存反转结果时,使用long型变量; 2、在计算出反转结果后,需要判断是否溢出 三、算法代码 public class Solution { pub原创 2016-03-05 10:34:29 · 345 阅读 · 0 评论 -
leetcode:Palindrome Number 【Java】
一、问题描述 Determine whether an integer is a palindrome. Do this without extra space. 二、问题分析 1、分别获取该数的最高位和最低位数字,比较是否一致。 2、当为负数时,返回false。 三、算法代码 public class Solution { public boolean isPalindr原创 2016-03-05 11:18:04 · 430 阅读 · 0 评论 -
leetcode:Two 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:原创 2016-03-05 12:20:57 · 341 阅读 · 0 评论 -
leetcode:Pow(x, n) 【Java】
一、问题描述 Implement pow(x, n). 二、问题分析 在实现分治递归时,结束条件加n==1,可显著提高算法运行效率。 三、算法代码 public class Solution { public double myPow(double x, int n) { if(n < 0){ return 1.0 / pow(x, -原创 2016-03-05 17:08:40 · 356 阅读 · 0 评论 -
leetcode:Sqrt(x) 【Java】
一、问题描述 Implement int sqrt(int x). Compute and return the square root of x. 二、问题分析 返回整型的最接近的平方根。 三、算法代码 public class Solution { public int mySqrt(int x) { if(x < 2){原创 2016-03-05 17:23:14 · 400 阅读 · 0 评论 -
leetcode:Path Sum 【Java】
一、问题描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary t原创 2016-03-07 17:30:41 · 334 阅读 · 0 评论 -
leetcode:Sum Root to Leaf Numbers 【Java】
一、问题描述 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Fin原创 2016-03-07 21:36:10 · 322 阅读 · 0 评论