上一页 1 ··· 19 20 21 22 23 24 25 26 27 ··· 43 下一页
摘要: 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.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of 阅读全文
posted @ 2013-08-04 10:56 feiling 阅读(260) 评论(0) 推荐(0) 编辑
摘要: 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.本题要求在O(n)时间内完成,并且使用O(1)空间O(n)时间则说明要使用hash来解本题,O(1)空间要求不能另开一个数组来进行hash,只能声明变量或者使用输入数组来进行这题是使用输入数组来进行hash本题的输入正整数范 阅读全文
posted @ 2013-08-04 09:57 feiling 阅读(210) 评论(0) 推荐(0) 编辑
摘要: Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, � ,ak 阅读全文
posted @ 2013-08-03 17:26 feiling 阅读(356) 评论(0) 推荐(0) 编辑
摘要: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of inte 阅读全文
posted @ 2013-08-02 23:56 feiling 阅读(428) 评论(0) 推荐(0) 编辑
摘要: 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.A sudoku puzzle...思路:回溯(Backtracking),回溯题练得太少,知道应该用回溯写,但不知如何下笔。。。。。下面代码是参考网上博客写的,回溯题有如下几点需要注意:1. 应当有个独立的isValid判断函数2. 递 阅读全文
posted @ 2013-08-02 22:59 feiling 阅读(419) 评论(0) 推荐(0) 编辑
摘要: 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 character'.'.A partially filled sudoku which is valid.判断数独是否合法:1. 同一行中1-9出现次数不重复2. 同一列中1-9出现次数不重复3. 9宫格中1-9出现次数不重复这里将1,2的判断在一个循环体中进行,分开进行跑 阅读全文
posted @ 2013-08-02 15:23 feiling 阅读(252) 评论(0) 推荐(0) 编辑
摘要: 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.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0思路:二分搜索,如search到则返 阅读全文
posted @ 2013-08-02 13:18 feiling 阅读(307) 评论(0) 推荐(0) 编辑
摘要: 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 ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].思路:使用二分搜索 阅读全文
posted @ 2013-08-01 21:06 feiling 阅读(346) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.上题中A[l] A[m]) {23 // upper is sorted24 if(A[m] < target && target <= A[r... 阅读全文
posted @ 2013-08-01 20:38 feiling 阅读(498) 评论(0) 推荐(0) 编辑
摘要: 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.使用二分搜索需计算l,r 左右边界1.当A[l] <= A 阅读全文
posted @ 2013-08-01 17:37 feiling 阅读(234) 评论(0) 推荐(0) 编辑
上一页 1 ··· 19 20 21 22 23 24 25 26 27 ··· 43 下一页