下面是今天寫的幾道題:
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:12ms
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:4ms
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 -
這道題自己雖然知道要用位運算但是一開始沒想出來,下邊的方法借鑒網上,原文地址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 }
--------------------------------------------------------------------------------------------------------------------------------------------------------
下邊這個方法按道理是不符合題目的,題中要求不使用+-符號,但是這竟然在LeetCode上ac了,我這個代碼需要小心邊界即負數最小值。
1 //整數轉換為二進制數組 2 int intToBit(int *str,long 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 //負數的話二進制取反+1 14 void reverdBitAdd1(int *str,int *num) 15 { 16 //取反 17 int temp = *num; 18 while(temp--) str[temp] = ( (str[temp] == 0)? 1 : 0 ); 19 20 //負數需要反轉+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 //兩個二進制相加 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 //將二進制轉換為十進制 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 //定義存儲結果的sum,標識a,b的符號的falg 62 int sum = 0,flaga = 0,flagb = 0; 63 //定義存儲ab以及相加後的二進制數組 64 int bita[33] = {0}; 65 int bitb[33] = {0}; 66 int sumbit[34] = {0}; 67 //考慮a,b可能為負數最小值 68 long long la = a; 69 long long lb = b; 70 //先按無符號位處理 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 //如果符號相同直接相加 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 //如果a是負數 94 else if(flaga == 1) 95 { 96 //先將a反轉+1,此時a多了個符號位 97 reverdBitAdd1(bita,&sizea); 98 while(sizea<sizeb)//補齊負數位 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);//正數二進制位多 104 else if(sizea-1>sizeb)//負數符號位多 105 { 106 num--; 107 reverdBitAdd1(sumbit,&num); 108 sum = -sumBit(sumbit,num-1); 109 } 110 else if(sizea-1 == sizeb)//不包括符號位兩個位數一樣多 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 } 137 return sum; 138 }