- 博客(21)
- 收藏
- 关注
原创 minimum value in a tree
Given a node in non-empty binary search tree, return the minimum data value found in that subtree.int minValue(struct node* node) { struct node* current = node; /* loop down to find
2013-05-15 13:46:00 342
原创 Merge Sort
#include #include //To save space, only one temp array is usedvoid mergeArray(int a[], int first, int mid, int last, int temp[]){ int i = first, j = mid + 1; int m = mid, n = last; int
2013-05-13 03:28:24 370
原创 Quick Sort
#include #include int adjustArray(int s[], int l, int r){ int i = l, j = r; int x = s[i]; while(i < j){ while(i x){ j--; } if(i < j){ s[
2013-05-13 03:11:32 382
原创 dynamically allocated 2d & 3d array
#include #include int main(){ //arr[10] int* arr = (int*)malloc(sizeof(int)*10); //arr[20][10] int** arr2d = (int**)malloc(sizeof(int*) * 20); int i; for(i = 0; i < 20; i+
2013-05-12 13:47:39 471
原创 Tower of Hanoi
#include #include void towerOfHanoi(int n, char from, char to, char temp);int main(){ towerOfHanoi(5,'A','C','B'); return 0;}void towerOfHanoi(int n, char from, char to, char temp){
2013-05-12 12:54:45 338
原创 Reverse a positive number
int reverseNum(int num){ int result = 0; int tmp; while(num > 0){ tmp = num % 10; result = 10 * result + tmp; num /= 10; } return result;}
2013-05-12 12:49:51 368
原创 Implement Your Own sizeof
#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
2013-05-12 12:37:41 321
原创 Reverse the words in a sentence in place
Given a sentence like this "I am a good boy", the in place reverse would be "boy good a am I".#include #include void reverseWordInStr(char*);void reverseStr(char*, char*);int main(){ char
2013-05-12 02:18:35 411
原创 Remove duplicates in a sorted array
//Return size of the new arrayint removeDuplicates(int arr[], int length){ int i, j = 0; for(i = 1; i < length; i++){ if(arr[i] != arr[j]){ arr[++j] = arr[i]; }
2013-05-12 02:10:57 279
原创 power of 2 or not
int ispowerof2(unsigned int x) { return x && !(x & (x - 1));}Note that the bit pattern of a power of two is of the form 10...0 and that of a number just one less is 011...1.
2013-05-12 01:34:34 617
原创 pow function
Signiture:int pow(int, int);int myPow(int x, int n){ if(n == 1) return x; return x * myPow(x, n-1);}Exponentiation by squaring is a more efficient way.
2013-05-12 01:27:50 411
原创 Palindrome
Check if a string is palindrome.#include #include int palindrome(char str[]);int main(){ char str[] ="abcba"; printf("%c\n",str[5]); if(palindrome(str)){ printf("is palindr
2013-05-12 01:16:00 320
原创 sqrt implementation
Signiture:double sqrt (double x);#include #include #include #include double square_root(double x){ assert(x >= 0); double low; double high; if(x < 1){ low = x;
2013-05-11 10:44:07 352
原创 Little endian vs Big endian
Concept:In big endian, you store the most significant byte in the smallest address.In little endian, you store the least significant byte in the smallest address.#include #include int main()
2013-05-11 10:36:39 341
原创 Greatest Common Divisor(GCD) of Two Numbers
#include #include int gcd(int a, int b);int gcdRecursive(int, int);int gcdRecursiveHelper(int, int);void swap(int* a, int* b);int main(){ int a = gcd(56,12); int b = gcdRecursive(18, 1
2013-05-11 10:05:57 421
原创 Fibonacci Numbers
#include #include int fabonicciRecursive(int);int fabonicci(int);int main(){ int rs = fabonicci(5); int rs2 = fabonicciRecursive(5); printf("%d\n", rs); printf("%d\n", rs2);
2013-05-11 09:51:16 334
原创 Factorial of a number
#include #include int factorial(int n);int main(){ int rs = fact(5); printf("%d\n", rs); return 0;}int factorial(int n){ if(n == 1) return 1; return factorial(n-1)
2013-05-11 09:47:17 384
原创 Decimal to Binary
#include #include void printBinary(int);int main(){ decimal2Binary(15); printf("\n"); printBinary(15); return 0;}void printBinary(int a){ int i; for(i = sizeof(a)*8 - 1
2013-05-11 09:38:32 390
原创 Rounded Numbers
Write code to round numbers?#include #include int roundedNumber(double num);int main(){ int num = roundedNumber(5.56); printf("%d\n", num); return 0;}int roundedNumber(double num)
2013-05-11 09:34:44 354
原创 Binary Search Implementation
#include #include int binarySearch();int binarySearchRecursive(int a[], int start, int end, int target);int main(){ int a[5] = {2,8,9,13,21}; int pos1 = binarySearch(a, 5, 6); printf
2013-05-11 09:28:01 322
原创 atoi
#include #include int myAtoi(const char*);int main(){ char str[] = "-324232"; int a = myAtoi(str); printf("%d\n", a); return 0;}int myAtoi(const char* str){ int sign; s
2013-05-11 09:08:18 316
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人