Skip to content

Commit 8021701

Browse files
committed
bit manipulation & math
1 parent 6126e59 commit 8021701

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*******************************************
2+
Author : LHearen
3+
4+
Time : Tue, 24 May 2016 19:24 CST
5+
Description : Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
6+
Example:
7+
Given num = 16, return true. Given num = 5, return false.
8+
Follow up: Could you solve it without loops/recursion?
9+
Source : https://leetcode.com/problems/power-of-four/
10+
*******************************************/
11+
#include <cstdbool.h>
12+
//AC - 4ms - using loop to check;
13+
bool isPowerOfFour0(int n)
14+
{
15+
int arr[] = {1, 4,16,64,256,1024,4096,16384,65536,262144,1048576,4194304,
16+
16777216,67108864,268435456,1073741824}; //using the OJ to get the array;
17+
for(int i = 0; i < sizeof(arr)/sizeof(int); i++) //check whether it's one of them;
18+
if(num == arr[i]) return true;
19+
return false;
20+
}
21+
22+
//AC - 4ms - recursive;
23+
bool isPowerOfFour1(int n)
24+
{
25+
if(n < 1) return false;
26+
if(n == 1) return true;
27+
return (n&3)==0 && isPowerOfFour(n>>2); //n&3 -> n%4;
28+
}
29+
30+
//AC - 4ms - using loop;
31+
bool isPowerOfFour2(int n)
32+
{
33+
if(n < 1) return false;
34+
if(n == 1) return true;
35+
while(n > 1)
36+
{
37+
if(n%4) return false;
38+
n /= 4;
39+
}
40+
return true;
41+
}
42+
43+
//AC - 4ms - adopting bit manipulation;
44+
bool isPowerOfFour(int n)
45+
{
46+
return n==1 || (n>1 && (n&-n)==n && (n&0x2aaaaaab)==0); //n&-n used to get the last bit of n -> used here to check whether it only has one 1-bit;
47+
//n&0x2aaaaaab used to make sure the 1-bit lies in the right position - power of four;
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*******************************************
2+
Author : LHearen
3+
4+
Time : Tue, 24 May 2016 20:21 CST
5+
Description : Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.
6+
For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
7+
Note: you may assume that n is not less than 2.
8+
Source : https://leetcode.com/problems/integer-break/
9+
*******************************************/
10+
//AC - 0ms - DP;
11+
#define MAX(a, b) ((a) > (b) ? (a) : (b))
12+
int integerBreak0(int n)
13+
{
14+
int *arr = (int*)malloc(sizeof(int)*(n+1)); //used to store the max product of the index;
15+
arr[2]=1, arr[3]=2, arr[4]=4; //corner cases;
16+
for(int i = 5; i <= n; i++)
17+
{
18+
int max = 0;
19+
for(int j = 2; j < i-1; j++)
20+
{
21+
int t = MAX(j, arr[j])*MAX(i-j, arr[i-j]); //select the value itself or the its product value;
22+
if(t > max) max = t;
23+
}
24+
arr[i] = max;
25+
}
26+
return arr[n];
27+
}
28+
29+
//AC - 0ms - mathematical solution;
30+
//only 2 or 3 will be used to make the product bigger;
31+
//elements bigger than them can be splitted further to make bigger;
32+
//meantime 3*3 > 2*2*2 so we should get as many 3 as possible;
33+
int integerBreak(int n)
34+
{
35+
if(n == 2) return 1;
36+
if(n == 3) return 2;
37+
int ret = 1;
38+
while(n > 4)
39+
{
40+
ret *= 3;
41+
n -= 3;
42+
}
43+
return ret*n;
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*******************************************
2+
Author : LHearen
3+
4+
Time : Tue, 24 May 2016 19:58 CST
5+
Description : Write a function that takes a string as input and reverse only the vowels of a string.
6+
Example 1:
7+
Given s = "hello", return "holle".
8+
Example 2:
9+
Given s = "leetcode", return "leotcede".
10+
Source : https://leetcode.com/problems/reverse-vowels-of-a-string/
11+
*******************************************/
12+
#include <cstdbool.h>
13+
//AC - 4ms;
14+
bool isVowel(char a)
15+
{
16+
char c = tolower(a);
17+
return c=='a' || c=='e' || c=='i' || c=='o' || c=='u';
18+
}
19+
char* reverseVowels(char* s) {
20+
int len = strlen(s);
21+
int l=0, r=len-1;
22+
while(l < r)
23+
{
24+
while(l<len && !isVowel(s[l])) l++;
25+
while(r>-1 && !isVowel(s[r])) r--;
26+
if(l<len && r>-1 && l < r)
27+
{
28+
char t=s[l]; s[l]=s[r]; s[r]=t;
29+
l++, r--;
30+
}
31+
}
32+
return s;
33+
}

0 commit comments

Comments
 (0)