Nim Game, Reverse String, Sum of Two Integers, nimintegers

Source: Internet
Author: User

Nim Game, Reverse String, Sum of Two Integers, nimintegers

The following are the questions I wrote today:

292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. the one who removes the last stone will be the winner. you will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
1 bool canWinNim(int n) {2   if ( 0<n && n<4 ) return true;3   else if(n==4) return false;4   else return n%4 != 0;5 }
  344. Reverse String Write a function that takes a string as input and returns the string reversed.1. C ++ code Runtime: 12 ms
1 class Solution {2 public:3     string reverseString(string s) {4        reverse(s.begin(),s.end());5        return s;6     }7 };

2. C code Runtime: 4 ms

 1 char* reverseString(char* s) { 2     int len = strlen(s); 3     for(int i=0;i<len/2;i++) 4     { 5         char tempc = s[i]; 6         s[i] = s[len-1-i]; 7         s[len-1-i] = tempc; 8     } 9     return s;10 }

371. Sum of Two Integers

Calculate the sum of two integers a and B, but you are not allowed to use the operator+And-

This question although I know to use bitwise operations but did not come out at the beginning, the following method for reference on the Internet, the original address http://www.cnblogs.com/la0bei/p/5659829.html

 1 int main() 2 { 3        int a,b,c; 4        cin >> a >> b; 5        while(b) 6        { 7               c = a^b; 8               b = (a&b)<<1; 9               a = c;10        }11        cout << a << endl;12        system("pause");13 }

Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------

The following method does not conform to the question. The +-symbol is not required in the question, but it is actually ac on LeetCode. I need to be careful about the boundary of this Code, that is, the minimum value of a negative number.

1 // integer to binary array 2 int intToBit (int * str, long num) 3 {4 int I = 0; 5 do 6 {7 str [I ++] = num % 2; 8 num = num/2; 9} while (num> 0); 10 11 return I; 12} 13 // if the number is negative, the binary value is reversed + 1 14 void reverdBitAdd1 (int * str, int * num) 15 {16 // The value is reversed to 17 int temp = * num; 18 while (temp --) str [temp] = (str [temp] = 0 )? 1: 0); 19 20 // The negative number needs to be reversed + 1; 21 int sum = 1; 22 for (int I = 0; I <* num; I ++) 23 {24 sum + = str [I]; 25 str [I] = sum % 2; 26 sum = sum/2; 27} 28 str [* num] = 1; 29 (* num) ++; 30} 31 32 // two binary values add 33 int bitAdd (int * sumbit, int * a, int sizea, int * B, int sizeb) 34 {35 int sum = 0; 36 int num = (sizea> sizeb? Sizea: sizeb); 37 for (int I = 0; I <num; I ++) 38 {39 sum + = (a [I] + B [I]); 40 sumbit [I] = sum % 2; 41 sum/= 2; 42} 43 if (sum = 1) sumbit [num ++] = 1; 44 return num; 45} 46 47 // convert the binary into a decimal 48 int sumBit (int * bitNum, int n) 49 {50 int sum = 0; 51 int bit2 = 1; 52 for (int I = 0; I <n; I ++) 53 {54 sum + = bitNum [I] * bit2; 55 bit2 * = 2; 56} 57 return sum; 58} 59 60 int getSum (int a, int B) {61 // defines the sum of the stored results, which identifies, falg 62 int sum = 0, flaga = 0, flagb = 0; 63 // define the storage AB and the added binary array 64 int bita [33] = {0}; 65 int bitb [33] = {0 }; 66 int sumbit [34] = {0}; 67 // considering a, B may be negative and the minimum value is 68 long la = a; 69 long lb = B; 70 // first process 71 if (la <0) 72 {73 la =-la; 74 flaga = 1; 75} 76 77 if (lb <0) 78 {79 lb =-lb; 80 flagb = 1; 81} 82 83 int sizea = intToBit (bita, la); 84 int sizeb = intToBit (bitb, lb ); 85 86 // if the symbols are the same, directly add 87 if (flaga = flagb) 88 {89 int num = bitAdd (sumbit, bita, sizea, bitb, sizeb ); 90 sum = sumBit (sumbit, num); 91 if (flaga = 1) sum =-sum; 92} 93 // if a is a negative 94 else if (flaga = 1) 95 {96 // first reverse a + 1, at this time, a has 97 reverdBitAdd1 (bita, & sizea); 98 while (sizea <sizeb) // supplements the negative digit 99 {100 bita [sizea ++] = 1; 101} 102 int num = bitAdd (sumbit, bita, sizea, bitb, sizeb); 103 if (sizeb> sizea-1) sum = sumBit (sumbit, num-1 ); // positive binary multiple 104 else if (sizea-1> sizeb) // negative sign multiple 105 {106 num --; 107 reverdBitAdd1 (sumbit, & num ); 108 sum =-sumBit (sumbit, num-1); 109} 110 else if (sizea-1 = sizeb) // not including the sign bit two digits as much as 111 {112 if (sumbit [num] = 1) sum =-sumBit (sumbit, num-1); 113 else sum = sumBit (sumbit, num-1); 114} 115} 116 else if (flagb = 1) 117 {118 reverdBitAdd1 (bitb, & sizeb); 119 while (sizeb <sizea) 120 {121 bitb [sizeb ++] = 1; 122} 123 int num = bitAdd (sumbit, bita, sizea, bitb, sizeb); 124 if (sizea> sizeb-1) sum = sumBit (sumbit, num-1); 125 else if (sizeb-1> sizea) 126 {127 num --; 128 reverdBitAdd1 (sumbit, & num); 129 sum =-sumBit (sumbit, num-1); 130} 131 else if (sizeb-1 = sizea) 132 {133 if (sumbit [num] = 1) sum =-sumBit (sumbit, num-1 ); 134 else sum = sumBit (sumbit, num-1); 135} 136 return sum; 137}

Related Article

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.