- 博客(47)
- 资源 (1)
- 收藏
- 关注
原创 CareerCup 4.8
4.8 You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1.A tree T2 is a subtree of T1 if there exi
2013-07-08 09:37:34
502
原创 CareerCup 4.7
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary searc
2013-07-03 08:29:33
521
原创 CareerCup 4.6
4.6 Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in a binary search tree. You may assume that each node has a link to its parent.struct TreeNode { int va
2013-07-03 05:48:22
581
原创 CareerCup 4.5
4.5 Implement a function to check if a binary tree is a binary search tree.struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NUL
2013-07-03 03:14:07
487
原创 CareerCup 4.4
4.4 Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked lists).
2013-07-02 14:02:13
571
原创 CareerCup 4.3
4.3 Given a sorted (increasing order) array, write an algorithm to create a binary search tree with minimal height.struct TreeNode { int val; TreeNode *left, *right; TreeNode(int v) : va
2013-07-02 13:39:18
412
原创 CareerCup 4.2
4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes.#include #include #include using namespace std;struct GraphNode { enum State {unvisite
2013-07-02 13:18:08
475
原创 LeetCode 9 - Palindrome Number
Palindrome NumberJan 4 '124196 / 10440Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you
2013-07-01 04:35:04
491
原创 CareerCup 4.1
4.1 Implement a function to check if a binary tree is balanced. For the purpose of this question, a balanced tree is defined to be a tree such that the height of the two subtrees of any node never dif
2013-06-28 07:20:43
381
原创 CareerCup 3.7
3.7 An animal shelter holds only dogs and cats, and operates on a strictly "first in, first out" basis. People must adopt either the "oldest" (based on arrival time) of all animals at the shelter, or
2013-06-27 07:58:41
522
原创 CareerCup 3.6
3.6 Write a program to sort a stack in ascending order (with biggest items on top). You may use additional stacks to hold items, but you may not copy the elements into any other data structure (such a
2013-06-26 08:02:32
459
原创 CareerCup 3.5
3.5 Implement a MyQueue class which implements a queue using two stacks.template class MyQueue {private: stack oldest, newest; void shiftStacks() { if (oldest.empty()) {
2013-06-26 07:47:12
461
原创 CareerCup 3.4
3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from
2013-06-26 07:04:00
509
原创 CareerCup 3.3
3.3 Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds same threshold. Impleme
2013-06-26 00:06:52
459
原创 CareerCup 3.2
3.2 How would you design a stack which, in addition to push and pop, also has a functionminwhich returns the minimum element?Push,pop and min should all operate in O(1) time.Solution 1:t
2013-06-25 08:01:48
394
原创 CareerCup 3.1
3.1 Describe how you could use a single array to implement three stacks.Approach 1: Fixed Divisionclass ThreeStack {private: int stackSize; int *buffer; int stackPointer[3];public:
2013-06-25 02:43:46
825
原创 CareerCup 2.7
2.7 Implement a function to check if a linked list is a palindrome.struct ListNode { int data; ListNode *next; ListNode(int x) : data(x), next(NULL) {}};Solution #1: Iterative App
2013-06-24 15:14:02
482
原创 CareerCup 2.6
2.6 Given a circular linked list, implement an algorithem which returns the node at the beginning of the loop.struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(
2013-06-24 13:49:16
425
原创 EPI 5.8 SPREADSHEET COLUMN ENCODING
#include #include using namespace std;int ssDecodeColID(const string& col) { int x = 0; for (const char &c : col) { x = x * 26 + c - 'A' + 1; } return x;}string rand_str
2013-06-13 02:54:58
500
原创 5.7 BASE CONVERSION
#include #include #include using namespace std;string convert_base(const string& s, const int b1, const int b2) { bool neg = (s[0] == '-'); int x = 0; for (int i = neg; i < s.size();
2013-06-13 02:53:57
509
原创 EPI 5.6 STRING AND INTEGER CONVERSIONS
#include #include #include #include #include using namespace std;string intToString(int x) { bool is_negative = false; if (x < 0) { is_negative = true; x = -x; }
2013-06-13 02:52:56
830
原创 EPI 5.5 THE POWER SET
#include #include using namespace std;template void generate_power_set(const vector& S) { for (int i = 1; i < (1 << S.size()); i++) { int x = i; for (int j = 0; x >> j; j++) {
2013-06-13 02:51:50
645
原创 EPI 5.4 CLOSEST INTEGERS WITH THE SAME WEIGHT
#include #include #include using namespace std;unsigned long long closest_int_same_bits(unsigned long long x) { for (int i = 0; i < 63; i++) { if (((x >> i) & 1) != ((x >> (i + 1)) &
2013-06-13 02:50:50
609
原创 EPI 5.3 BIT REVERSAL
#include #include using namespace std;long long swap_bits(long long x, int i, int j) { if (((x >> i) & 1) != ((x >> j) & 1)) { x ^= (1LL << i) | (1LL << j); } return x;}long
2013-06-11 11:02:42
659
原创 EPI 5.2 SWAP BITS
#include using namespace std;long long swap_bits(long long x, int i, int j) { if (((x >> i) & 1) != ((x >> j) & 1)) { x ^= (1LL << i) | (1LL << j); } return x;}int main(int a
2013-06-10 14:53:22
582
原创 EPI 5.1 COMPUTING PARITY
#include #include using namespace std;int parity1(unsigned long long n) { int p = 0; while (n) { p ^= (n & 1); n >>= 1; } return p;}int parity2(unsigned long lon
2013-06-10 14:28:53
741
原创 CareerCup 2.5
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a func
2013-04-01 11:08:30
408
原创 CareerCup 2.4
2.4 Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x.struct ListNode { int val; ListNode *next;
2013-04-01 01:11:21
531
原创 CareerCup 2.3
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), nex
2013-03-31 09:43:24
310
原创 CareerCup 2.2
2.2 Implement an algorithm to find the kth to last element of a singly linked list.struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};ListNode* nthT
2013-03-31 08:22:17
390
原创 CareerCup 2.1
2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed?struct ListNode { int val; ListNode *next;
2013-03-30 08:08:18
479
原创 CareerCup 1.8
1.8 Assume you have a method isSubstringwhich checks if one word is a substring of another. Given two strings, s1 and s2, write code to check ifs2 is a rotation of s1 using only one call to is
2013-03-29 12:31:35
843
原创 CareerCup 1.7
1.7 Write an algorithm such that if an element in an M*N matrix is 0, its entire row and column are set to 0.void setZeros(vector >& matrix) { int m = matrix.size(); int n = matrix[0].size()
2013-03-29 12:05:02
452
原创 CareerCup 1.6
1.6 Given an image represented by an N*N matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?void rotate(vector >& matrix,
2013-03-29 11:31:43
796
原创 CareerCup 1.5
1.5 Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaa would become a2b1c5a3. If the "compressed" string would not becom
2013-03-22 11:50:55
525
原创 CareerCup 1.4
1.4 Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given
2013-03-21 13:50:43
648
原创 CareerCup 1.3
1.3 Given two strings, write a method to decide if one is a permutation of the other.Solution 1:bool isPermutation(string a, string b) { if (a.length() != b.length()) return false;
2013-03-21 13:28:02
507
原创 CareerCup 1.2
1.2 Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.void reverse(char* str) { char *p, *q, tmp; p = q = str; if (str) { while (
2013-03-21 12:37:33
570
原创 CareerCup 1.1
1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure?bool isUniqueChar(string s) { vector c(256, false); for (int
2013-03-14 12:18:21
510
原创 LeetCode 8 - String to Integer (atoi)
String to Integer (atoi)Dec 27 '11Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and a
2013-03-04 13:38:27
273
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人