摘要: 给出一棵BST,将其转换为双向链表,left保存前节点,right保存后节点。例如: 4 / \ 1 7 / \ / \ 0 2 5 8 \ \ \ 3 6 9变为:0<->1<->2<->3<->4<->5<->6<->7<->8<->9.解答:这里我们可以看到一个节点的左子树的最后节点就是本节点的前驱,右子树的最左节点就是后继。于是需要一个函数能够返回这样的需求,也就有了一个可以返回本棵树的左右节点的函数。同时,在细节上进行一些处理。 1 #include <iostre... 阅读全文
posted @ 2012-10-29 20:14 chkkch 阅读(597) 评论(0) 推荐(1) 编辑
摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, … ,ak) must 阅读全文
posted @ 2012-10-29 17:36 chkkch 阅读(4451) 评论(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 @ 2012-10-29 17:20 chkkch 阅读(4179) 评论(0) 推荐(0) 编辑
摘要: 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.Note: You 阅读全文
posted @ 2012-10-29 16:57 chkkch 阅读(6456) 评论(0) 推荐(2) 编辑
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.这题的关键是能找出当前链表的中间节点,然后再递归左右的子链表,开始的时候程序先计算链表总厂,然后传入两个前后索引指针,最后每次递归找出中间节点即可。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 *... 阅读全文
posted @ 2012-10-29 15:20 chkkch 阅读(4020) 评论(2) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.二分递归转换 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 *... 阅读全文
posted @ 2012-10-29 15:08 chkkch 阅读(2383) 评论(0) 推荐(0) 编辑
摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.树的递归 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val... 阅读全文
posted @ 2012-10-29 14:57 chkkch 阅读(1145) 评论(0) 推荐(0) 编辑
摘要: Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.递归构造 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : va... 阅读全文
posted @ 2012-10-29 14:49 chkkch 阅读(3208) 评论(0) 推荐(0) 编辑
摘要: 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],]简单的组合问题,用类似dfs的方法 1 class Solution { 2 private: 3 vector<vector<int> > ret; 4 vector<int> a; 5 public: 6 void sol 阅读全文
posted @ 2012-10-29 14:38 chkkch 阅读(4077) 评论(0) 推荐(0) 编辑
摘要: You are climbing a stair case. It takesnsteps 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?fib数列 1 class Solution { 2 public: 3 int climbStairs(int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT ... 阅读全文
posted @ 2012-10-29 14:22 chkkch 阅读(636) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [3], ... 阅读全文
posted @ 2012-10-29 14:18 chkkch 阅读(1999) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array and a number n.How can u find the number of occurance of n in the array . should be o(logn)http://www.careercup.com/question?id=8877058改变一下二分查找的方法,一次找到最左边,另一次找到最右边。#include <iostream>#include <vector>using namespace std;int findPos(vector<int> &a, int left, 阅读全文
posted @ 2012-10-29 13:53 chkkch 阅读(402) 评论(0) 推荐(0) 编辑
摘要: A circus is designing a tower routine consisting of people standing atop one another’sshoulders. For practical and aesthetic reasons, each person must be both shorter and lighter than the person below him or her. Given the heights and weights of each person in the circus, write a method to compute t 阅读全文
posted @ 2012-10-29 12:56 chkkch 阅读(793) 评论(0) 推荐(0) 编辑
摘要: Given an array of elements find the largest possible number that canbe formed by using the elements of the array.eg: 10 9ans: 9102 3 5 78ans: 78532100 9ans: 9100http://www.careercup.com/question?id=9334650把字符串排个序,比较方法是循环比较各个字符,如果对应位的字符大于另一个字符串的对应位则返回true,否则false。思想就是类似于要把尽可能大的数位放在更高位。 1 #include < 阅读全文
posted @ 2012-10-29 12:10 chkkch 阅读(476) 评论(0) 推荐(0) 编辑
摘要: Given an integer 'k' and an sorted array A (can consist of both +ve/-ve nos), output 2 integers from A such that a-b=k.PS:nlogn solution would be to check for the occurence of k-a[i] (using binary search) when you encounter a[i]. methods like hash consume space.Is an O(n) solution with O(1) 阅读全文
posted @ 2012-10-29 11:29 chkkch 阅读(414) 评论(0) 推荐(0) 编辑