(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!