
LeetCode从零单排
我的LeetCode从零单排笔记
Garvin Li
Dancing with data
展开
-
【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one ca原创 2015-07-29 15:14:51 · 2248 阅读 · 1 评论 -
【LeetCode从零单排】No221.Maximal Square
题目Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Retur原创 2015-07-02 09:58:17 · 1741 阅读 · 0 评论 -
【LeetCode从零单排】No22.Generate Parentheses
题目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:"((()))", "(()())", "(())()", "()(())", "()()()"代码F原创 2015-07-14 12:01:34 · 1403 阅读 · 0 评论 -
【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景(以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool)DFS(Dpeth-first Search)顾名思义,就是深度搜索,一条路走到黑,再选新的路。记得上Algorithm的时候,教授举得例子就是说,DFS很像好奇的小孩,你给这个小孩几个盒子套盒子,好奇的小孩肯定会一个盒子打开后继续再在这个原创 2015-04-06 16:57:23 · 1783 阅读 · 0 评论 -
单链表问题(反转、是否有环、删除结尾第N个节点、合并两个sortlist、找到交点)
1.时间复杂度O(N),内存O(1)的效率下实现单链表的翻转public static TreeNode revers(TreeNode head){ TreeNode temp,first,second; first=head; second=head.next; while(second!=null){ temp=second.next; second.next=原创 2015-03-26 15:21:00 · 2264 阅读 · 0 评论 -
【LeetCode从零单排】No121 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), d原创 2015-04-01 09:16:04 · 1567 阅读 · 0 评论 -
【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)
题目Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 0000000000原创 2015-03-13 15:50:15 · 4620 阅读 · 2 评论 -
【LeetCode从零单排】No 3 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原创 2015-03-17 16:33:47 · 1449 阅读 · 0 评论 -
【LeetCode从零单排】No 114 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 6The flattened tree should look like: 1 \ 2原创 2015-03-16 14:57:08 · 1432 阅读 · 0 评论 -
【LeetCode从零单排】No118 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 Solution { public原创 2015-03-09 15:10:31 · 1523 阅读 · 0 评论 -
【LeetCode从零单排】No112 Path Sum
题目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 tree and sum =原创 2015-03-04 16:48:47 · 1665 阅读 · 0 评论 -
【LeetCode从零单排】No104 Maximum Depth of Binary Tree
题目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.代码/** * Definition for binary tree * p原创 2015-03-04 14:51:02 · 1652 阅读 · 0 评论 -
【LeetCode从零单排】No102 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 /原创 2015-03-03 16:22:03 · 1572 阅读 · 2 评论 -
【LeetCode从零单排】No100 Same Tree && No101 Symmetric Tree
题目1.same treeGiven two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.2.sym原创 2015-03-02 16:10:29 · 1515 阅读 · 0 评论 -
【LeetCode从零单排】No83 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.代码 public static ListNode deleteD原创 2015-03-02 11:18:50 · 1464 阅读 · 0 评论 -
【LeetCode从零单排】No88.Merge Sorted Array
题目Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. T原创 2015-03-02 09:54:12 · 1400 阅读 · 0 评论 -
【LeetCode从零单排】No70.ClimbingStairs
题目 爬楼梯问题,这是一道很有趣的问题。首先看题目: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?这原创 2015-02-20 13:16:45 · 2122 阅读 · 0 评论 -
【LeetCode从零单排】No67.AddBinary
题目Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".代码public class Solution { public String addBinary(String a, String b) { S原创 2015-02-15 16:44:53 · 1322 阅读 · 0 评论 -
【LeetCode从零单排】No38.CountAndSay
题目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 2, then原创 2015-02-14 18:13:40 · 1415 阅读 · 0 评论 -
【LeetCode从零单排】No58.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.Note: A word is defined原创 2015-02-15 10:19:37 · 1479 阅读 · 0 评论 -
【LeetCode从零单排】No36 Valid Sudoku
题目 判断数独是否成立的一道题,看的是某大神的答案,写的太漂亮了。Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the charact原创 2015-02-13 16:50:03 · 1306 阅读 · 0 评论 -
【LeetCode从零单排】No27.Remove Element
题目 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 length.代码p原创 2015-02-12 16:34:25 · 1254 阅读 · 0 评论 -
【LeetCode从零单排】No28 Implement strStr()
题目Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.代码public class Solution { public int strStr(String haystack, String ne原创 2015-02-12 16:36:11 · 1400 阅读 · 0 评论 -
【LeetCode从零单排】No21.MergeTwoSortedLists
题目 这道题是链表的简单应用,将两个有序链表合成一个有序链表。 思路是:表一,表二各取两个对象,分别指向current和next,进行交叉比较排序。Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nod原创 2015-02-11 11:18:30 · 1415 阅读 · 0 评论 -
【LeetCode从零单排】No.8 String to Integer (丧心病狂的一道题)
题目 题目很简单,就是写一个函数把string转换成int,但是通过率只有可怜的11%,难点是要考虑所有情况,特别是int溢出边界,反正我是写了2个小时还没解决,先放到这,有空接着搞,现在应该还有最后一个bug。Implement atoi to convert a string to an integer.Hint: Carefully consider all possible i原创 2015-02-07 11:41:29 · 1551 阅读 · 0 评论 -
【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
题目 题目要求:去除sort int数组中的重复项。 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,原创 2015-02-10 11:16:00 · 1286 阅读 · 0 评论 -
【LeetCode从零单排】No19.RemoveNthNodeFromEndofList
题目 这是道链表的简单应用题目,删除从结尾数第n个节点。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原创 2015-02-09 17:02:44 · 1404 阅读 · 0 评论 -
【LeetCode从零单排】No20.ValidParentheses
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}"原创 2015-02-09 11:23:14 · 1246 阅读 · 0 评论 -
【LeetCode从零单排】No14.LongestCommonPrefix
题目 Write a function to find the longest common prefix string amongst an array of strings.代码public class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length==0)原创 2015-02-08 16:14:27 · 1329 阅读 · 0 评论 -
【LeetCode从零单排】No.9 Palindrome Number
题目 这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意)。Palindrome就是判断一个整数是否对称。Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negativ原创 2015-02-07 11:46:03 · 1353 阅读 · 0 评论 -
【LeetCode从零单排】No.7 Reverse Integer
前话 今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐)。从easy的开始,数一下差不多有40道,争取两个月搞定。题目 没想到做的第一道题目,虽然看似简单,却费了很大周折。题目如下:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -3原创 2015-02-06 14:16:59 · 1619 阅读 · 2 评论