LeetCode
文章平均质量分 77
likecool21
这个作者很懒,什么都没留下…
展开
-
LeetCode刷题笔录 Median Of Two Sorted Arrays
先说临界情况 A为空或者B为空 直接在非空数组中找第k大的数即可。O(1) 找最小的数,k==0的情况,也简单,比较两个数组最开头的元素,谁小就是谁 然后就是比较复杂的情况,假设寻找目标target是下标为k的数。 那么意味着在排好的数组中,在目标数之前,一共有k个比目标更小的数。 将k分成两份,一份在A的前端,一份在B的前端。这里其实将k怎么分配是一个可以讨论的问题,转载 2013-09-01 18:06:39 · 1939 阅读 · 0 评论 -
LeetCode刷题笔录 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo原创 2013-09-01 22:48:12 · 16498 阅读 · 1 评论 -
LeetCode 刷题笔录 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2013-10-02 15:04:45 · 1713 阅读 · 0 评论 -
Leetcode刷题笔录 Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 想法是把这个输入的String数组的每个String元素按照其字符的ASCII码的和进行分类,每一个和对应一个ArrayList,这个数组包含所有原创 2014-02-14 16:14:51 · 741 阅读 · 0 评论 -
LeetCode刷题笔录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:原创 2014-04-30 12:34:42 · 1208 阅读 · 0 评论 -
LeetCode刷题笔录Best Time to But and Sell Stock II
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 (ie, buy on原创 2014-05-02 12:10:38 · 662 阅读 · 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. 先写了一个用extra space的方法,原创 2014-05-01 08:36:16 · 621 阅读 · 0 评论 -
LeetCode刷题笔录Best Time to Buy and Sell Stock
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 the stock),原创 2014-05-02 08:08:07 · 551 阅读 · 0 评论 -
LeetCode刷题笔录Single Number II
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 without usi原创 2014-05-03 09:58:12 · 633 阅读 · 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.原创 2014-04-17 13:12:12 · 637 阅读 · 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?原创 2014-05-03 23:37:15 · 714 阅读 · 0 评论 -
LeetCode刷题笔录 Pascal's Triangle II
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? 这题比较简单原创 2014-04-02 14:02:13 · 959 阅读 · 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 node's key.Th原创 2014-04-09 15:15:58 · 6777 阅读 · 2 评论 -
LeetCode刷题笔录 Binary Tree Level Order Traversal
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 / \ 9 20原创 2014-04-22 09:21:03 · 2585 阅读 · 0 评论 -
LeetCode刷题笔录 Pow(x,n)
Implement pow(x, n). z原创 2014-04-24 13:24:20 · 690 阅读 · 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 [4,−1,2,1] ha原创 2014-05-09 06:21:52 · 687 阅读 · 0 评论 -
LeetCode刷题笔录 Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to原创 2014-04-10 14:05:32 · 670 阅读 · 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. 这题xiang原创 2014-04-25 12:34:59 · 578 阅读 · 0 评论 -
LeetCode刷题笔录 Jump Game II
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. Your goal i原创 2014-04-11 13:39:09 · 1269 阅读 · 0 评论 -
LeetCode刷题笔录 Single Number
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 without using ext原创 2014-04-27 06:42:42 · 577 阅读 · 0 评论 -
LeetCode刷题笔录 Permutaions
public class Solution { public ArrayList> permute(int[] num) { ArrayList> result = new ArrayList>(); //start from an empty list result.add(new ArrayList()); for (转载 2014-04-12 02:42:04 · 762 阅读 · 0 评论 -
LeetCode刷题笔录Unique Binary Search Trees
这里有个讲解(印度人口音真难懂)原创 2014-04-27 13:33:30 · 938 阅读 · 0 评论 -
LeetCode刷题笔录 Same Tree
public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null) return true; else if(p == null || q == null) return f原创 2014-04-27 12:46:10 · 700 阅读 · 0 评论 -
LeetCode刷题笔录Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 标准CTCI原题,快慢两个指针。 public class Solution { public boolean hasCycle(ListNode h原创 2014-04-27 13:35:53 · 687 阅读 · 0 评论 -
LeetCode刷题笔录 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive soluti原创 2014-04-29 02:56:36 · 580 阅读 · 0 评论 -
LeetCode刷题笔录 Binary Tree Inorder Traversal
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]. Note: Recursive solutio原创 2014-04-29 13:02:49 · 746 阅读 · 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], [1,4], ]原创 2014-07-15 12:20:18 · 687 阅读 · 0 评论 -
LeetCode刷题笔录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原创 2014-07-16 04:54:43 · 548 阅读 · 0 评论 -
LeetCode刷题笔录Sum Root to Leaf Numbers
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. Find the tota原创 2014-07-16 05:23:59 · 784 阅读 · 0 评论 -
LeetCode刷题笔录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 va原创 2014-07-18 03:59:38 · 627 阅读 · 0 评论 -
LeetCode刷题笔录Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f原创 2014-06-25 13:09:24 · 769 阅读 · 0 评论 -
LeetCode刷题笔录 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原创 2014-06-26 00:52:55 · 491 阅读 · 0 评论 -
LeetCode刷题笔录 Pascal's Triangle
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] ] public class Solut原创 2014-06-26 11:42:22 · 712 阅读 · 0 评论 -
LeetCode刷题笔录Search 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). You are given a target value to search. If found in the array retur原创 2014-07-18 02:26:12 · 513 阅读 · 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 respectively in the原创 2014-07-21 05:08:44 · 571 阅读 · 0 评论 -
LeetCode刷题笔录Longest Consecutive Sequence
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 longest consecutive elements sequence is [1, 2, 3原创 2014-07-21 03:37:00 · 593 阅读 · 0 评论 -
LeetCode刷题笔录Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1原创 2014-07-21 05:54:37 · 643 阅读 · 0 评论 -
LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这题可以kan'chu'lai原创 2014-05-29 06:01:16 · 783 阅读 · 0 评论 -
LeetCode刷题笔录 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. 这题不能像merge原创 2014-05-29 02:22:10 · 744 阅读 · 0 评论 -
LeetCode刷题笔录Balanced Binary Tree
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 never diffe原创 2014-05-29 02:48:06 · 705 阅读 · 0 评论