LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
摘要:题目:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.例如给出上图中的黑色部分(数组表示),让你求出蓝色部分。这也是个神题。。。当然对小白我来说。想了半天,是不是遍历数组呢,然后依次计算当前bar构成的container大小。问题在于,这个..
阅读全文
posted @
2013-07-10 19:04
lichen782
阅读(1295)
推荐(0) 编辑
LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]
摘要:题目:Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.为什么这道题值得纪念呢? 因为它教育我们看问题看本质。要看出问题本质,首先要深刻理解问题本身是在说啥。。。(越来越像学习某某core的讲话了)在一个无序的整数数组中,找出第一个没有的正数。什么意思呢?这么想,所有正数是
阅读全文
posted @
2013-07-10 10:08
lichen782
阅读(1089)
推荐(0) 编辑
LeetCode 笔记系列十 Suduko
摘要:题目:Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character'.'. You may assume that there will be only one unique solution.下面是一个数独的题目:其解:数独不是很了解,没做过。不过知道规则。就是在这个9x9的格纸中间添1到9的数字。使每一行不能重复,每一列也不能重复,然后上面那个粗线框起来的3x3的格纸中的数字也不能重复。不知道这样的游戏有啥意义。。
阅读全文
posted @
2013-07-07 18:27
lichen782
阅读(698)
推荐(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 7might become4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array.就是说,排序数组可能是右移了一定位数。让你在这...
阅读全文
posted @
2013-07-07 17:39
lichen782
阅读(2196)
推荐(0) 编辑
LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
摘要:题目: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 is"()", which has length = 2. Another example is")()())", where the lon
阅读全文
posted @
2013-07-06 20:38
lichen782
阅读(4426)
推荐(0) 编辑
LeetCode 笔记系列七 Substring with Concatenation of All Words
摘要:题目:You are given a string,S, and a list of words,L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.For example, given:S:"barfoothefoobarman"L:["foo", &qu
阅读全文
posted @
2013-07-06 11:29
lichen782
阅读(1866)
推荐(0) 编辑
LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]
摘要:题目:Given a linked list, reverse the nodes of a linked listkat a time and return its modified list. If the number of nodes is not a multiple ofkthen left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nodes itself may be changed. Only constant memory i...
阅读全文
posted @
2013-07-05 17:04
lichen782
阅读(14064)
推荐(2) 编辑
LeetCode 笔记系列五 Generate Parentheses
摘要:题目:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"解法:leetcode上的解法很赞。 其实这也是利用的递归的分支。构建了一树状结构并遍历,叶子节点就是valid
阅读全文
posted @
2013-07-04 17:56
lichen782
阅读(1089)
推荐(0) 编辑
LeetCode 笔记系列四 Remove Nth Node From End of List
摘要:题目:Given a linked list, remove thenthnode 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 linked list becomes 1->2->3->5.就是让你删除单链表倒数第n个节点,同时希望能只遍历一次。解法一: 如果不遍历完所有节点,怎么知道倒数第n个在哪里呢?
阅读全文
posted @
2013-07-04 15:50
lichen782
阅读(548)
推荐(0) 编辑
LeetCode 笔记系列三 3Sum
摘要:题目:Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: (-1, 0, 1) (-1, -1, 2)额外的要求是不能返回重复的triplets,返回的a,b,c的顺序要是非递减的。解法一:首先想一下,三个数相加,要为0的话...
阅读全文
posted @
2013-07-04 11:47
lichen782
阅读(1786)
推荐(0) 编辑
LeetCode 笔记系列二 Container With Most Water
摘要:题目:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.就是说,x轴上
阅读全文
posted @
2013-07-03 18:10
lichen782
阅读(5385)
推荐(1) 编辑
LeetCode 笔记系列一 Median of Two Sorted Arrays
摘要:题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 刚开始的时候理解有误,以为是求中位数。其实不是。这里所谓的"median of the two sorted arrays"指的是:如果A.length + B.length 是奇数,返回merge后中间的那个数;如果是偶数,返回中间两个数的平均
阅读全文
posted @
2013-07-02 15:31
lichen782
阅读(2498)
推荐(0) 编辑