Math start!

Source: Internet
Author: User

(1) ADD Digits

Problem Solving Ideas:

A mathematical attribute based on congruence. The root of the number (that is, the final result required to be returned by the topic) is the same as the remainder of the number divided by 9 (and the remainder will always be a single number).

Take 438 as an example

[Step 1]:

438  == 40*10 +3*10 +8 ;4+3+8 == 4*(10%9)*(10%9)+3*(10%9)+8%9= 15 ;

[Step 2]:

15  == 1*10 + 5 ; 1+5 == 1*(10%9)+5%9= 6 ;

[So we can see]:

return num%9; and don‘t forget num==0 or num==9   

The code is as follows:

1  Public class Solution {2      Public  int adddigits (int  num) {3         return  num = = 0? 0: (num%9 = = 0? 9 : (num%9)); 4     }5 }
View Code

(2) Minimum Moves to Equal Array Elements

Problem Solving Ideas:

To achieve the goal of making the elements in the array equal, add 1 to the n-1 element and subtract 1 from one element. therefore, the best way is to make all the elements in the array equal to the min element. sum (array)-N * Minimum

The code is as follows:

1  Public classSolution {2      Public intMinmoves (int[] nums) {3         if(Nums.length = = 0) {4             return0;5         }6         intMin = nums[0];7         intsum = 0;8          for(intn:nums) {9Min =math.min (n, min);TenSum + =N; One         } A         returnsum-nums.length*min; -     } -}
View Code

(3) Excel Sheet Column Title

Problem Solving Ideas:

The essence is to convert the number of 10 into 26 binary.

The code is as follows:

1  Public class Solution {2      Public String converttotitle (int  n) {3         return n = = 0? "": Converttotitle (--N/26) + (char) (' A ' + (n%)); 4         // return n = = 0? "": Converttotitle ((n-1)/+) + (char) (' A ' + ((n-1)%)); 5     }6 }
View Code

Math start!

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: info-contact@alibabacloud.com 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.