摘要:
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 ... 阅读全文
摘要:
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 阅读全文
摘要:
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... 阅读全文
摘要:
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; ... 阅读全文
摘要:
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 阅读全文
摘要:
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... 阅读全文