(1) Arranging Coins
Idea one: The idea is about two times equation, get arithmetic and the formula is sum = (x + 1) * X/2
So for this question, if we know and, then we can know x = ( -1 + sqrt (8 * n + 1))/2 down rounding.
The code is as follows:
1 Public class Solution {2 Public int arrangecoins (int n) {3 return (int) (-1 + MATH.SQRT (1 + 8 * (long) n))/2); 4 }5 }
View Code
Problem-solving idea two: Use the given n to keep subtracting ... until n is less than the number to subtract.
The code is as follows:
1 Public classSolution {2 Public intArrangecoins (intN) {3 intresult = 0;4 intAdd = 1;5 while(N >=add) {6N-=add;7add++;8result++;9 }Ten returnresult; One } A}View Code
(2) Factorial Trailing Zeroes
Problem Solving Ideas:
Because all trailing zero comes from factor 5 * 2.
But sometimes a number can have several 5 factors, for example, 25 has two 5 factors, and 125 has three 5 factors. In n! Operation, factor 2 is always sufficient. So we only calculate the 5 factors in all the numbers from 1 to N.
The simplest way to calculate the number of 5 is SUM (n/5^1, n/5^2, n/5^3 ...)
Code One:
1 Public class Solution {2 Public int trailingzeroes (int n) {3 return n = = 0? 0:n/5 + Trailingzer OES (N/5); 4 }5 }
View Code
Code two:
1 Public classSolution {2 Public intTrailingzeroes (intN) {3 if(N < 1) {4 return0;5 }6 intNumber = 0;7 while(N/5! = 0) {8N/= 5;9Number + =N;Ten } One returnNumber ; A } -}View Code
(3) Palindrome number
Problem-solving ideas: To determine whether a number is a palindrome number, can not be like a character string as a character comparison!!! So the number reversal if the same as the original value is a palindrome number.
The code is as follows:
1 Public classSolution {2 Public BooleanIspalindrome (intx) {3 if(X < 0) {4 return false;5 }6 returnx = =reverse (x);7 }8 Public intReverseintx) {9 intRST = 0;Ten while(x! = 0) { OneRST = rst * ten + x% 10; AX/= 10; - } - returnrst; the } -}View Code
(4) Rectangle Area
Problem Solving Ideas:
Find the coordinate representation of the repeating part of the rectangle, two large rectangular areas added minus the overlapping portions of the area.
The code is as follows:
1 Public classSolution {2 Public intComputearea (intAintBintCintDintEintFintGintH) {3 intleft = Math.max (a,e), right =Math.max (Math.min (c,g), left);4 intBottom = Math.max (b,f), top =Math.max (Math.min (d,h), bottom);5 return(c-a) * (d-b)-(right-left) * (Top-bottom) + (G-E) * (H-F); 6 }7}View Code
(5) Reverse Integer
The idea of solving problems is simple and clear.
The code is as follows:
1 Public classSolution {2 Public intReverseintx) {3 intresult = 0;4 while(x! = 0) {5 inttail = x 10;6 intNewresult = result * 10 +tail;7 if((newresult-tail)/10! =result) { 8 return0; 9}//Determine if overflowTenresult =Newresult; OneX/= 10; A } - returnresult; - } the}View Code
Finish writing math! as soon as possible