2014年3月17日

Unique Binary Search Trees

摘要: Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 ... 阅读全文

posted @ 2014-03-17 21:49 pengyu2003 阅读(134) 评论(0) 推荐(0) 编辑

Reverse Linked List II

摘要: 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 阅读全文

posted @ 2014-03-17 15:39 pengyu2003 阅读(120) 评论(0) 推荐(0) 编辑

Decode Ways

摘要: A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12", it cou 阅读全文

posted @ 2014-03-17 15:15 pengyu2003 阅读(100) 评论(0) 推荐(0) 编辑

Partition List

摘要: Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4->3- 阅读全文

posted @ 2014-03-17 14:40 pengyu2003 阅读(133) 评论(0) 推荐(0) 编辑

2014年3月14日

Word Search

摘要: 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,Givenboard=[ [&q 阅读全文

posted @ 2014-03-14 11:01 pengyu2003 阅读(391) 评论(0) 推荐(1) 编辑

Subsets

摘要: Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]S先排序下,使全集能够按顺序输出。class Soluti... 阅读全文

posted @ 2014-03-14 09:42 pengyu2003 阅读(129) 评论(0) 推荐(0) 编辑

2014年3月13日

Combinations

摘要: Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]不知道有没有更好的方法。class Solution {public: vector > re; vector > combine(int n, int k) { vector m; if(n already... 阅读全文

posted @ 2014-03-13 21:06 pengyu2003 阅读(119) 评论(0) 推荐(0) 编辑

Search a 2D Matrix

摘要: Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, 3, ... 阅读全文

posted @ 2014-03-13 19:42 pengyu2003 阅读(158) 评论(0) 推荐(0) 编辑

2014年3月12日

求比正整数N大的最小正整数M,且M与N的二进制表示中有相同数目的1

摘要: 转自http://blog.csdn.net/ligt0610/article/details/7262757一般最容易想到的方法就是先计算正整数N用二进制表示时1的个数count1,然后不停地计算N++用二进制表示时1的个数count2,直到碰到count1 == count2成立,代码如下:[cpp]typedefunsignedintuint;//解法一:uintcount1Bits(uintn){uintcount=0;while(0!=n){n&=n-1;count++;}returncount;}uintgetNextN_1(uintn){uintcount=count1B 阅读全文

posted @ 2014-03-12 21:30 pengyu2003 阅读(672) 评论(0) 推荐(0) 编辑

Set Matrix Zeroes

摘要: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m+n) space, but still not the best solution.Could you de 阅读全文

posted @ 2014-03-12 14:44 pengyu2003 阅读(103) 评论(0) 推荐(0) 编辑

导航