随笔分类 -  数据结构&算法

摘要: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 阅读(291) 评论(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 阅读(117) 评论(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 阅读(502) 评论(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 阅读(569) 评论(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 阅读(210) 评论(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 阅读(148) 评论(0) 推荐(0)