CCI_chapter 1】的更多相关文章

19 1  Write a function to swap a number in place without temporary variables void swap(int &a, int &b) { b = a - b; // 4 = 9 - 5 a = a - b; // 5 = 9 - 4 b = a + b; // 9 = 4 + 5 } void swap(int & a, int &b) { a = a^b; b = a^b; a = a^b; } 19…
16.5 Write a program to find whether a machine is big endian or little endian Big-Endian和Little-Endian的定义如下:1) Little-Endian就是低位字节排放在内存的低地址端,高位字节排放在内存的高地址端.2) Big-Endian就是高位字节排放在内存的低地址端,低位字节排放在内存的高地址端.举一个例子,比如数字0x12 34 56 78在内存中的表示形式为:1)大端模式:低地址 ----…
13.9Write a smart pointer (smart_ptr) class template<class T>class SmartPoint{ public: SmartPoint(T *ref){ ref_ = ref; count_ = (unsigned int *)malloc(sizeof(unsigned int )); *count_ = 1; } SmartPoint(SmartPoint<T> &sptr){ ref_ = sptr.ref_…
8.1 水题 8.2 Imagine a robot sitting on the upper left hand corner of an NxN grid The robot can only move in two directions: right and down How many possible paths are there for the robot? FOLLOW UPImagine certain squares are “of limits”, such that the…
4.1Implement a function to check if a tree is balanced For the purposes of this question,a balanced tree is defned to be a tree such that no two leaf nodes difer in distance from the root by more than one http://www.cnblogs.com/graph/archive/2013/04/…
3.1Describe how you could use a single array to implement three stacks for stack 1, we will use [0, n/3)for stack 2, we will use [n/3, 2n/3)for stack 3, we will use [2n/3, n) ; ]; ] = {,,}; //栈顶指针,指向下一可以放元素的位置 bool isEmpty(int stackNum){ assert(stack…
2.1  Write code to remove duplicates from an unsorted linked list /* Link list node */ struct node { int data; struct node* next; }; void rem_duplicate(node *head){ if(NULL == head) return ; set<int> hash; set.insert(head->data); while(head->n…
1.1Implement an algorithm to determine if a string has all unique characters What if  you can not use additional data structures? bool isUniqueChars(string str) { unsigned ; unsigned ; ; i < str.size();i++) { ; unsigned int temp; temp = flag ? <<…