11 2013 档案

摘要:Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.Note:You are not suppose to use the l 阅读全文
posted @ 2013-11-28 12:27 WinsCoder 编辑
摘要:Implement pow(x,n).Recursive solution is easier to understand. It uses the divide-and-conquer approach, which also runs intime. The formula is shown below:And the code is quite simple and straightforward.Code:class Solution {public: double pow(double x, int n) { if(n==0) return ... 阅读全文
posted @ 2013-11-26 18:02 WinsCoder 编辑
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and iterati... 阅读全文
posted @ 2013-11-24 16:18 WinsCoder 编辑
摘要:1. Define Storage Classes and explain application domain.register- tell to the compiler for use a CPU register for fast aceess for that variable.auto- it's a variable created and initialized when it is defined. It is not visible outside of the block.static- defined inside ofthe function retain i 阅读全文
posted @ 2013-11-22 19:07 WinsCoder 编辑
摘要:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", &q 阅读全文
posted @ 2013-11-22 18:00 WinsCoder 编辑
摘要: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.Code:class Solution {public: int findBalance(TreeNode *node){ if(!node) ret... 阅读全文
posted @ 2013-11-19 04:29 WinsCoder 编辑
摘要:Sort a linked list inO(nlogn) time using constant space complexity.Code:class Solution {public: ListNode *sortList(ListNode *head) { if(!head) return head; map> sortMap; vector sortVector; ListNode *anchor=new ListNode(0); anchor->next=head; ListNode *cur... 阅读全文
posted @ 2013-11-18 11:56 WinsCoder 编辑
摘要:Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].Code:class Solution {public: int removeDuplicates(int A[], int n) { if(n<3) return n; int i=0;... 阅读全文
posted @ 2013-11-17 18:52 WinsCoder 编辑
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?Code:class Solution {public: void rotate(vector > &matrix) { int n=matrix.size(); for(int i=0;i<n/2;i++){ for(int j=i;j<n-i-1;j++){ ... 阅读全文
posted @ 2013-11-14 14:54 WinsCoder 编辑
摘要:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"Code:class Solution {public: void generate(int l, int r, st 阅读全文
posted @ 2013-11-14 12:53 WinsCoder 编辑
摘要:Given a number represented as an array of digits, plus one to the number.Code:class Solution {public: vector plusOne(vector &digits) { int n=digits.size(); bool carry=true; for(int i=n-1;i>-1;i--){ if(digits[i]==9&&carry==1){ digits[i]=0; ... 阅读全文
posted @ 2013-11-14 08:38 WinsCoder 编辑
摘要:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.Code:class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { if(p&&q){ if(p->val!=q->... 阅读全文
posted @ 2013-11-14 08:05 WinsCoder 编辑
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.Code:class Solution {public: int maxSubArray(int A[], int n) { int maxSub=0; ... 阅读全文
posted @ 2013-11-14 06:10 WinsCoder 编辑
摘要:Sort a linked list using insertion sort.Code:class Solution {public: ListNode *insertionSortList(ListNode *head) { if(head==NULL) return head; ListNode *start=new ListNode(0); start->next=head; ListNode *pre=head; ListNode *cur=head->next; ListNode *nex; ... 阅读全文
posted @ 2013-11-14 06:06 WinsCoder 编辑
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is not 阅读全文
posted @ 2013-11-13 17:45 WinsCoder 编辑
摘要:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.Code:class Solution {public: int removeElement(int A[], int n, int elem) { int j=0; for(int ... 阅读全文
posted @ 2013-11-08 18:15 WinsCoder 编辑
摘要:We know the elements can be printed post-order easily using recursion,as follow:void postOrderTraversal(BinaryTree *p) { if (!p) return; postOrderTraversal(p->left); postOrderTraversal(p->right); cout data;}This is the most difficult of all types of iterative tree traversals. You should attemp 阅读全文
posted @ 2013-11-08 17:52 WinsCoder 编辑
摘要:Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note:Recursive solution is trivial, could you do it iteratively?Code:class Solution {public: void findNode(vector &nodes,TreeNode *node){ if(n... 阅读全文
posted @ 2013-11-08 17:41 WinsCoder 编辑
摘要:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.Code:class Solution {public: int romanToInt(string s) { int len=s.length(); map character; character['I'] = 1; character['V'] = 5; character['X'] = 10; ... 阅读全文
posted @ 2013-11-08 17:39 WinsCoder 编辑
摘要:Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文
posted @ 2013-11-08 17:19 WinsCoder 编辑
摘要:Implementint sqrt(int x).Compute and return the square root ofx.Code:class Solution {public: int sqrt(int x) { int start=0; int end=x/2>std::sqrt(INT_MAX)?std::sqrt(INT_MAX):x/2+1; while(start<=end){ int mid=(start+end)/2; if(x==mid*mid) r... 阅读全文
posted @ 2013-11-08 17:17 WinsCoder 编辑
摘要:Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?Code:1. Recursive:class Solution {public: void findNode(vector &nodes,TreeNode *node){... 阅读全文
posted @ 2013-11-07 08:24 WinsCoder 编辑
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8Code:class Solution { 阅读全文
posted @ 2013-11-06 08:07 WinsCoder 编辑
摘要: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.Code:class Solution {public: ListNode *deleteDuplicat 阅读全文
posted @ 2013-11-05 17:47 WinsCoder 编辑
摘要:Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}.Code:class Solution {public: void reorderList(ListNode *head) { if(head==NULL) return; Lis... 阅读全文
posted @ 2013-11-05 17:37 WinsCoder 编辑
摘要:Divide two integers without using multiplication, division and mod operator.Code:class Solution {public: int divide(int dividend, int divisor) { int quotient=0; long long d1=abs((long long)dividend); // change to type 64 bits, such as 'long long' or 'double' long long d2=abs(... 阅读全文
posted @ 2013-11-05 07:45 WinsCoder 编辑
摘要: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.Code:class Solution {public: ListNode *reverseKGroup( 阅读全文
posted @ 2013-11-05 04:59 WinsCoder 编辑
摘要:单向链表的反转是一个经常被问到的一个面试题,也是一个非常基础的问题。比如一个链表是这样的: 1->2->3->4->5 通过反转后成为5->4->3->2->1。最容易想到的方法遍历一遍链表,利用一个辅助指针,存储遍历过程中当前指针指向的下一个元素,然后将当前节点元素的指针反转后,利用已经存储的指针往后面继续遍历。源代码如下:struct linka { int data; linka* next;};void reverse(linka*& head){ if(head ==NULL) return; linka*pre, *cur, 阅读全文
posted @ 2013-11-02 08:35 WinsCoder 编辑
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?Code:class Solution {public: ListNode *detectCycle(ListNode *head) { ListNode *p=head; ListNode *cur=head; while(p){ i... 阅读全文
posted @ 2013-11-01 05:45 WinsCoder 编辑
摘要:Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algorithm should use only constant space. You maynotmodify the values inthe list, only nodesitself can bechanged.Code:class Solution {pub 阅读全文
posted @ 2013-11-01 05:35 WinsCoder 编辑

点击右上角即可分享
微信分享提示