摘要: 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) 编辑
摘要: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1.这道题目总是理解错,最开始理解就是左子树的任何一个叶子节点和右子树的任何一个叶子节点的depth差不能大于1. 并且左右子树都是平衡的,所以设了leftMinDep, leftM 阅读全文
posted @ 2012-10-27 22:40 chkkch 阅读(4457) 评论(5) 推荐(0) 编辑
摘要: Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]BFS,... 阅读全文
posted @ 2012-10-27 22:15 chkkch 阅读(994) 评论(3) 推荐(0) 编辑
摘要: Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,20,#,#,15,7},DFS遍历方法: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right;... 阅读全文
posted @ 2012-10-27 22:04 chkkch 阅读(4901) 评论(2) 推荐(0) 编辑
摘要: Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3},非递归版本:用一个栈来模拟递归的情况,首先思考inorder traverse的递归函数traverse(left tree);visit current nodetraverse(right tree);也就是说我们把一个节点压入栈中,首先它会先递归访问左子树(左节点入栈),再访问本身(这个时候这个节点就可以出栈了),在访问右子树(右节点入栈)。最后我们定义一个数据结构1 阅读全文
posted @ 2012-10-27 21:48 chkkch 阅读(3326) 评论(0) 推荐(0) 编辑
摘要: Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.这题最开始的思路是用map<string, vector<sting> >来保存与string对应的anagrams,然后有一张vector<string> word来保存key string,但问题是每次新的string要遍历word来进行比较是否存在,用的是一个count[26]来保存字符的个数,然后一一比较。最后发现其实先把每个s 阅读全文
posted @ 2012-10-27 20:53 chkkch 阅读(2633) 评论(0) 推荐(0) 编辑