摘要: 1 // 冒泡排序 2 // 2个相邻的元素相互比较,不满足顺序则交换;每遍历一次数组,使一个元素处于最终位置。 3 void BubbleSort(std::vector& nums) 4 { 5 if(nums.empty()) 6 { 7 return; 8 } 9 10 bool sorted = false; // 假... 阅读全文
posted @ 2016-09-21 13:41 Ricardo 阅读(279) 评论(0) 推荐(0) 编辑
摘要: 1 // 树的结点 2 struct TreeNode 3 { 4 int value; 5 TreeNode* left; 6 TreeNode* right; 7 TreeNode(int x) : value(x), left(NULL), right(NULL) {} 8 }; 9 10 // 先序遍历 11 void PreOrder... 阅读全文
posted @ 2016-09-20 21:32 Ricardo 阅读(114) 评论(0) 推荐(0) 编辑
摘要: 1 // 链表节点 2 struct ListNode 3 { 4 int value; 5 ListNode* next; 6 }; 7 8 // 求单链表中结点的个数 注意链表判断是否为空 时间复杂度O(n) 9 size_t GetListLength(ListNode* head) 10 { 11 if(!head) 1... 阅读全文
posted @ 2016-09-20 17:20 Ricardo 阅读(489) 评论(0) 推荐(0) 编辑
摘要: 1 /* strcpy函数实现 拷贝字符串 */ 2 char* Strcpy(char* dst, char* src) 3 { 4 assert(dst != NULL && src != NULL); // 断言 dst和src不能为NULL 5 6 char* dst_address = dst; // 保存目的地址 7 8 while((*(... 阅读全文
posted @ 2016-09-12 22:01 Ricardo 阅读(557) 评论(0) 推荐(0) 编辑
摘要: 服务端: 1 #include <sys/socket.h> 2 #include <unistd.h> 3 #include <sys/types.h> 4 #include <stdint.h> 5 #include <assert.h> 6 #include <fcntl.h> 7 #incl 阅读全文
posted @ 2016-08-13 09:40 Ricardo 阅读(428) 评论(0) 推荐(0) 编辑
摘要: 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 9 #include 10 #include 11 #include 12 13 void signal_func(int sig) 14 { 15 int saved_errno = er... 阅读全文
posted @ 2016-08-06 15:54 Ricardo 阅读(322) 评论(0) 推荐(0) 编辑
摘要: 1 #include 2 #include 3 using namespace std; 4 5 class BitCount 6 { 7 public: 8 size_t Count1(size_t n) 9 { 10 size_t counts = 0; 11 12 while (n > 0) 13 {... 阅读全文
posted @ 2016-08-04 23:16 Ricardo 阅读(198) 评论(0) 推荐(0) 编辑
摘要: 1 #include 2 #include 3 #include 4 using namespace std; 5 6 // 冒泡排序 7 class BubbleSort 8 { 9 public: 10 void SortF(vector& vec) 11 { 12 for (int j = 0; j vec[i]) 17 ... 阅读全文
posted @ 2016-08-03 23:47 Ricardo 阅读(138) 评论(0) 推荐(0) 编辑
摘要: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a functio 阅读全文
posted @ 2016-07-21 12:19 Ricardo 阅读(87) 评论(0) 推荐(0) 编辑
摘要: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target va 阅读全文
posted @ 2016-07-18 18:15 Ricardo 阅读(118) 评论(0) 推荐(0) 编辑