上一页 1 ··· 24 25 26 27 28 29 30 31 32 ··· 34 下一页
摘要: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?For example, Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3].思考:双指针,A[i]与A[cur]前两个值比较。class Solution {public: int removeDuplicates(int A[], int n) { // IMPO 阅读全文
posted @ 2013-11-21 15:53 七年之后 阅读(162) 评论(0) 推荐(0) 编辑
摘要: 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./** * Def... 阅读全文
posted @ 2013-11-21 13:52 七年之后 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 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?思考:DP,斐波拉契。class Solution {public: int climbStairs(int n) { // IMPORTANT: Please reset any member data you declared, as ... 阅读全文
posted @ 2013-11-21 10:19 七年之后 阅读(200) 评论(0) 推荐(0) 编辑
摘要: Given a collection of intervals, merge all overlapping intervals.For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].思考:先排序。比较相邻两个interval,left为前一个start,若后一个start小于等于前一个,说明这两个interval需要合并,right为两个end较大者。若后一个start大于前一个end,说明这两个interval不需要合并,输出前一个。/** * Definition for an interva 阅读全文
posted @ 2013-11-21 00:08 七年之后 阅读(162) 评论(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 of O(log n).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-11-19 21:21 七年之后 阅读(182) 评论(0) 推荐(0) 编辑
摘要: Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.For example, Given board =[ 阅读全文
posted @ 2013-11-19 19:54 七年之后 阅读(166) 评论(0) 推荐(0) 编辑
摘要: 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 as a character sequence consists of non-space characters only.For example, Given s = "He 阅读全文
posted @ 2013-11-19 15:05 七年之后 阅读(215) 评论(0) 推荐(0) 编辑
摘要: Implement pow(x, n).思考:一开始想的太复杂了,跟这一题联系起来了(链接)。后来看到返回值是double型,所以此题就没有那么复杂了。毕竟面试题不会要求写一大堆代码的,最重要的还是考察算法思想:快速幂取模,当然这里不用取模。class Solution {public: double pow(double x, int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for... 阅读全文
posted @ 2013-11-19 14:15 七年之后 阅读(225) 评论(0) 推荐(0) 编辑
摘要: Implement int sqrt(int x).Compute and return the square root of x.思考:参考链接:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/sotry-about-sqrt.htmlclass Solution {public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function double an... 阅读全文
posted @ 2013-11-18 16:09 七年之后 阅读(201) 评论(0) 推荐(0) 编辑
摘要: Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].The largest rectangle is shown in the shaded area, which has 阅读全文
posted @ 2013-11-18 14:48 七年之后 阅读(204) 评论(0) 推荐(0) 编辑
上一页 1 ··· 24 25 26 27 28 29 30 31 32 ··· 34 下一页