Today's topic is more difficult to understand.

Source: Internet
Author: User

(1) String to Integer (atoi)

Problem Solving Ideas:

Implement the Atoi function to convert the string to an integer number. Title Requirements:

1. First you need to discard the spaces in front of the string; (Trim function)

2. Then there may be a sign (note that only one, if there is more than one sign, then said that the string is not convertible, return 0. For example, there is a "+-2" in the test case;

3. The string can contain characters other than 0~9, and if a non-numeric character is encountered, only the part preceding that character, such as " -00123a66", is returned as "123";

4. If the range of int is exceeded, the boundary value (2147483647 or-2147483648) is returned.

The code is as follows:

1  Public classSolution {2      Public intmyatoi (String str) {3         //1. Null or empty string4         if(str = =NULL) {5             return0; 6         }  7         8         //2. Whitespaces9str =Str.trim (); Ten         if(str.length () = = 0) { One             return0; A         }  -          -         //3. +/- sign the         intSign = 1;  -         inti = 0;  -         if(Str.charat (0) = = ' + ') {   -i++;  +}Else if(Str.charat (0) = = '-') {   -Sign =-1;  +i++;  A         }   at            -         //4. Calculate Real Value -         DoubleTMP = 0;  -          for(; i < str.length (); i++) {   -             intdigit = Str.charat (i)-' 0 ';  -             if(Digit < 0 | | digit > 9) { in                  Break;  -             }  to             //5. Handle min & Max +             if(Sign = = 1) {   -TMP = 10*tmp +Digit;  the                 if(tmp > Integer.max_value)returnInteger.max_value;  *}Else {   $TMP = 10*tmp-Digit; Panax Notoginseng                 if(TMP < Integer.min_value)returnInteger.min_value;  -             }   the         }   +            A         intRET = (int) tmp;  the         returnret;  +     }   -}
View Code

(2) Rotate Function

Problem Solving Ideas:

* Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]F(k-1) = 0 * Bk-1[0] + 1 * Bk-1[1] + ... + (n-1) * Bk-1[n-1]       = 0 * Bk[1] + 1 * Bk[2] + ... + (n-2) * Bk[n-1] + (n-1) * Bk[0]

Then,

F(k) - F(k-1) = Bk[1] + Bk[2] + ... + Bk[n-1] + (1-n)Bk[0]              = (Bk[0] + ... + Bk[n-1]) - nBk[0]              = sum - nBk[0]

Thus,

F(k) = F(k-1) + sum - nBk[0]

What is bk[0]?

k = 0; B[0] = A[0];k = 1; B[0] = A[len-1];k = 2; B[0] = A[len-2];
So we can find out f[0] and sum, and then use the formula to seek f[1],f[2] ... and compare the size to select the maximum value.
The code is as follows:
1  Public classSolution {2      Public intMaxrotatefunction (int[] A) {3         intallsum = 0;//sum of all numbers4         intlen = a.length;//Array Length5         intF = 0;6          for(inti = 0; i < Len; i++) {7F + = i * a[i];//F[0]8Allsum + =A[i];9         }Ten         intMax =F; One          for(inti = len-1; I >= 1; i--) { Af = f + allsum-len * a[i];//F[1],f[2],... -max = Math.max (F, Max);//find the maximum value -         } the         returnMax; -     } -}
View Code

(3) Add Binary

Problem Solving Ideas:

From the end to the corresponding addition, the addition of int will make the code more concise, because you can use/and% to find out the carry and the current to add to the result of the bit. The remainder of the longer string is then added to the result. Finally, it is also necessary to determine whether the carry is 1 at this point, and if 1, you need to add 1 to the result, otherwise return the result.

The code is as follows: more concise

 Public classSolution { Publicstring Addbinary (String A, string b) {StringBuilder SB=NewStringBuilder (); inti = A.length ()-1, J = b.length ()-1, carry = 0;//carry carry flag bit         while(I >= 0 | | J >= 0) {            intsum =carry; if(J >= 0) sum + = B.charat (j--)-' 0 '; if(i >= 0) sum + = A.charat (i--)-' 0 '; Sb.append (Sum% 2); Carry= SUM/2; }        if(Carry! = 0) Sb.append (carry); returnsb.reverse (). toString (); }}
View Code

Code two:

1  Public classSolution {2      Publicstring Addbinary (String A, string b) {3         if(A.length () <b.length ()) {4String tmp =A;5A =b;6b =tmp;7         }8         9         intPA = a.length ()-1;Ten         intPB = B.length ()-1; One         intcarries = 0; AString rst = ""; -          -          while(PB >= 0){ the             intsum = (int) (A.charat (PA)-' 0 ') + (int) (B.charat (Pb)-' 0 ') +carries; -rst = string.valueof (sum% 2) +rst; -carries = SUM/2; -PA--; +PB--; -         } +          A          while(PA >= 0){ at             intsum = (int) (A.charat (PA)-' 0 ') +carries; -rst = string.valueof (sum% 2) +rst; -carries = SUM/2; -PA--; -         }        -          in         if(carries = = 1) -rst = "1" +rst; to         returnrst; +     } -}
View Code

(4) Nth Digit

Problem Solving Ideas:

 * 这里首先分析一下位数和规律 * 个位数:1-9,一共9个,共计9个数字 * 2位数:10-99,一共90个,共计180个数字 * 3位数:100-999,一共900个,共计270个数字 * 4位数,1000-9999,一共9000个,共计36000个数字 * 以此类推, * 这样我们就可以首先定位到是哪个数,再找到其对应的数字

The code is as follows:

1  Public classSolution {2      Public intFindnthdigit (intN) {3         //careful overflow4         intDigittype = 1;5         LongDigitnum = 9;6         //Navigate to a number of digits7          while(N > Digitnum *Digittype) {8N-= (int) Digitnum *Digittype;9digittype++;//several numbersTenDigitnum *= 10; One         } A         //Locate the number of the first few of these numbers . -         intIndexinsubrange = (n-1)/digittype;//The first few -         intIndexinnum = (n-1)% Digittype;//The first few the         //Restore Numbers -         intnum = (int) Math.pow (10,digittype-1) +Indexinsubrange; -         intresult = Integer.parseint (("" +num). CharAt (Indexinnum) + ""); -         returnresult; +     } -}
View Code

Note: Use a power of 10 (10,100, ...) for the reason of (n-1)/digittype because the number is restored later. ), but the front is actually only (9,99,... ), minus one first.

 

Today's topic is more difficult to understand.

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.