Add Digits, Maximum Depth of BinaryTree, Search for a Range, single number,find the difference

Source: Internet
Author: User

The most recent questions have been recorded.

258. Add Digits

Given a non-negative integer num , repeatedly add all its digits until the result have only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2have only one digit, return it.
1 intAdddigits (intnum) {2         Charstrint[ A] = {0};3sprintf (Strint,"%d", num);4         intI=0, a =0;5          while(strint[i+1]!=' /')6         {7A = (strint[i]-'0') + (strint[i+1]-'0');8            if(a>9)9            {TenA = a%Ten+a/Ten; One            } AStrint[++i] = a +'0'; -         } -         returnstrint[i]-'0'; the}

Above this is the complexity of O (n). There is also an O (1) complexity on the Web: return (num-1)% 9 + 1;

104. Maximum Depth of Binary Tree

To find the depth of the binary tree, you can use recursion or loop, as follows:

1 /**2 * Definition for a binary tree node.3 * struct TreeNode {4 * int val;5 * struct TreeNode *left;6 * struct TreeNode *right;7  * };8  */9 intMaxDepth (structtreenode*root) {Ten     intleft =0, right =0; One     if(Root = NULL)return 0; A  -left = MaxDepth (root->Left ); -right = MaxDepth (root->Right ); the  -     returnLeft>right? left+1: right+1; -}

*************************************************************************************************************** ***

With the queue node, record the node number of each layer levelcount, each out of a node levelcount--, if the minus 0 to go to the next layer, and then depth++, and the queue at this point is the node is the next layer of nodes.

1 /**2 * Definition for a binary tree node.3 * struct TreeNode {4 * int val;5 * TreeNode *left;6 * TreeNode *right;7 * TreeNode (int x): Val (x), left (null), right (null) {}8  * };9  */Ten classSolution { One  Public: A     intMaxDepth (treenode*root) { -         if(Root = NULL)return 0; -  the         //depth and number of nodes per layer -         intdepth =0, Levelcount =1; -         //the number of nodes that the queue holds for each layer -Queue<treenode*>node; +  - Node.push (root); +          while(!node.empty ()) A         { at             //iterate through each node in turn. -TreeNode *p =Node.front (); - Node.pop (); -levelcount--; -  -             if(p->left) Node.push (p->Left ); in             if(p->right) Node.push (p->Right ); -  to             if(Levelcount = =0) +             { -                 //Save the next level of nodes, plus 1 in depth thedepth++; *Levelcount =node.size (); $             }Panax Notoginseng         } -         returndepth; the     } +};
Search for a Range

Given a sorted array of integers, find the starting and ending position of a Given target value.

Your algorithm ' s runtime complexity must is in the order of O (log n).

If the target is not a found in the array, return [-1, -1] .

For Example,given [5, 7, 7, 8, 8, 10]and target value 8,return [3, 4]
1vector<int> Searchrange (vector<int>& Nums,inttarget) {2         intSize =nums.size ();3         intL=0, r=size-1;4          while(l<=R)5         {6             intMid = (l+r)/2;7             if(Nums[mid] >=target)8             {9r = mid-1;Ten             } One             Else if(Nums[mid] <target) A             { -L = mid+1; -             } the         } -         intleft =l; -L =0; -r = size-1; +          while(l<=R) -         { +             intMid = (l+r)/2; A             if(Nums[mid] <=target) at             { -L = mid+1; -             } -             Else if(Nums[mid] >target) -             { -r = mid-1; in             } -         } to         intright =R; +vector<int>v; - V.push_back (left); the V.push_back (right); *         if(Nums[left]! = Target | | nums[right]! =target) $         {Panax Notoginsengv[0] = -1; -v[1] = -1; the         } +         returnv; A}

The problem is to grasp the boundary, such as the left border when the = number to the right edge of the judgment, looking for the right edge of the opposite. So that we don't miss it.

136. Single NumberGiven an array of integers, every element appears twiceExcept for one. Find that single one. Directly with XOR or
1 intSinglenumber (vector<int>&nums) {2         intresult=0;3         intLen =nums.size ();4         if(Len <=0)return 0;5          for(intI=0; i<len;i++)6         {7Result ^=Nums[i];8         }9         returnresult;Ten}
389. Find the Difference

Given strings s and T which consist of only lowercase letters.

String t is generated by the random shuffling string s and then add one more than letter at a random position.

Find the letter is added in TThis method is very slow, I submit the display 16ms, there is no kind of char array to simulate the hash fast.
1 CharFindthedifference (stringSstringt) {2map<Char,int>Sumchar;3         CharRes;4          for(Auto ch:s) sumchar[ch]++;5          for(auto ch:t)6         {7            if(--sumchar[ch]<0)8res =ch;9         }    Ten         returnRes; One}

This problem can also be different or because the same will be different or lost, the remaining one is the extra char.

Add Digits, Maximum Depth of BinaryTree, Search for a Range, single number,find the difference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.