随笔分类 -  Algorithm

摘要: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 编辑
摘要: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 编辑

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