上一页 1 2 3 4 5 6 7 8 9 10 ··· 34 下一页
摘要: 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.思考:在上一题的基础上思考几个边界问题。[1,3] 3 [1,3] 3[3,3] 1 [3,1] 3[1,3,1,1,1] 3[1,1,1,3,1] 1class Solution {pub 阅读全文
posted @ 2014-01-21 10:09 七年之后 阅读(148) 评论(0) 推荐(0) 编辑
摘要: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.思考:跟上一题一样,维护左右边界。参考:http://discuss.leetcode.com/questions/260/maximal-rectangle#class Solution {public: int maximalRectangle(vector > &matrix) { if (matrix.empty()) { . 阅读全文
posted @ 2014-01-21 09:32 七年之后 阅读(225) 评论(0) 推荐(0) 编辑
摘要: Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use#as a separator for each node, and,as a separator for node label and each neighbor of the node.As an example, consider the serialized g 阅读全文
posted @ 2014-01-20 21:38 七年之后 阅读(285) 评论(0) 推荐(0) 编辑
摘要: There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationito its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the starting gas 阅读全文
posted @ 2014-01-16 21:17 七年之后 阅读(176) 评论(0) 推荐(0) 编辑
摘要: Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extra space.For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 ... 阅读全文
posted @ 2014-01-16 18:14 七年之后 阅读(226) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.N... 阅读全文
posted @ 2014-01-16 17:39 七年之后 阅读(200) 评论(0) 推荐(0) 编辑
摘要: Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(k) extra space?思考:维护数组ans,从后往前更新。class Solution {public: vector getRow(int rowIndex) { vector ans; ans.resize(rowIndex+1,0); a... 阅读全文
posted @ 2014-01-16 16:56 七年之后 阅读(179) 评论(0) 推荐(0) 编辑
摘要: GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]思考:边界单独考虑。class Solution {private: vector > res; vector ans;public: vector > generate(int numRows) { if(numRows==0) return res; ... 阅读全文
posted @ 2014-01-16 16:33 七年之后 阅读(174) 评论(0) 推荐(0) 编辑
摘要: Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ≤m≤n≤ length of list./** * Definition for singly-linked list. * struct List 阅读全文
posted @ 2014-01-16 13:19 七年之后 阅读(187) 评论(0) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3./** * Definition for singly-linked list. * struct Lis 阅读全文
posted @ 2014-01-15 21:25 七年之后 阅读(193) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 9 10 ··· 34 下一页