C++
沧浪之水清兮
这个作者很懒,什么都没留下…
展开
-
sizeof和strlen
#include<iostream>using namespace std;void testSize(char str[100]){ printf("%d\n", sizeof(str)); printf("%d\n", strlen(str));}int main(){ char str[] = "nowcoder"; printf("%d\n", sizeof...翻译 2018-10-16 13:39:10 · 143 阅读 · 0 评论 -
C/C++输入输出
输入输出1. 输入多个整数a. 如果是一次输入,存完然后再统一处理,一般就scanf("%d %d",&a,&b);输入a和b时用空格或回车都可以,因为scanf不识别空格和回车!b. 如果是多次输入,输入一次处理一次输出一次(一般是这种模式),就需要循环的输入。比如一次输入两个整数,那么可以是:while(scanf("%d %d",&a,&b...原创 2018-08-12 17:41:05 · 565 阅读 · 0 评论 -
堆排序、桶排序、归并排序
九种经典排序算法详解(冒泡排序,插入排序,选择排序,快速排序,归并排序,堆排序,计数排序,桶排序,基数排序) #include<iostream>#include<vector>using namespace std;/* * (最大)堆的向下调整算法 * * 注:数组实现的堆中,第N个节点的左孩子的索引值是(2N+1),右孩子的索引是(2N+2)。...翻译 2018-08-07 20:40:38 · 689 阅读 · 0 评论 -
背包问题
简单背包练习——0/1背包简单背包练习——完全背包问题简单背包练习——庆功会简单背包练习—— 混合背包简单背包练习——潜水员简单背包练习——分组背包背包九讲背包九讲详解背包问题github ...转载 2018-08-07 15:01:22 · 420 阅读 · 0 评论 -
动态规划-------数组之和最大以及乘积最大
//数组中连续数之和最大int maxSubArray(vector<int> nums){ if (nums.size() == 0) return 0; int *dp = new int[nums.size()]; int r = nums[0]; dp[0] = nums[0]; //这里可以不必要使用数组 for (int i = 1; i < n...原创 2018-08-07 10:52:22 · 694 阅读 · 0 评论 -
动态规划-----商铺购买问题
#include<iostream>#include<vector>#include<algorithm>using namespace std;int buyProduct(vector<int> a){ int n = a.size(); int *dp = new int[n]; dp[0] = a[0]; dp[...原创 2018-08-07 10:22:23 · 615 阅读 · 0 评论 -
输入一个字符串,按字典序打印出该字符串中字符的所有排列
class Solution {public: void dfs(string str, int begin, vector<string>& res) { if (str.empty()) return; if (begin == str.size() - 1) res.pus...转载 2018-08-10 20:32:04 · 2701 阅读 · 0 评论 -
将二叉搜索树转换成一个排序的双向链表
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*///递归解法TreeNode* convertNode(TreeNode* root){ if (NUL...转载 2018-08-10 20:08:25 · 309 阅读 · 0 评论 -
二叉树祖先问题
//找到第一个共同祖先class Solution{ //若p为root的子孙,则返回true bool covers(TreeNode*root, TreeNode*p){ if(NULL == root) return false; if(root == p) return true; return covers(root->left,p)||co...翻译 2018-08-06 10:15:58 · 995 阅读 · 0 评论 -
不用加减实现加法
class Solution{public: int aPlusb(int a, int b){ int res = a^b; int carry = (a&b) << 1; if (carry){ res = aPlusb(res, carry); } return res;}int aPlusb2(int a, int b){ int res ...原创 2018-07-31 20:59:34 · 299 阅读 · 0 评论 -
给定一个整数数组,将其堆化为最小堆数组。
class Solution{ void heapify(vector<int>& A){ for(int i = A.size()/2; i >= 0;i--) min_heap(A, i); } void min_heap(vector<int> &nums, int k){ int len = nums.size();...转载 2018-07-31 19:25:11 · 1577 阅读 · 0 评论 -
把数组排成最小的数
//方法一:定义lamda函数进行比较操作,比较容易理解string PrintMinNumber(vector<int> numbers){ sort(numbers.begin(), numbers.end(), [](const int& a, const int& b){ return to_string(a) + to_string(b) <...原创 2018-08-12 20:06:57 · 117 阅读 · 0 评论 -
priority_queue、multiset的简单使用
class Solution {public: vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { int size = input.size(); vector<int> ret; if (size == 0 || k <= 0 ...翻译 2018-08-08 09:38:11 · 312 阅读 · 0 评论 -
大神博客
全球和问题Windows多线程编程珠玑C语言中文网Leetcode系列总结Leetcode全解MOOC机器学习高效面试TCP的三次握手和四次挥手(实验)转载 2018-09-02 21:55:53 · 126 阅读 · 0 评论 -
序列化二叉树
/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: /...转载 2018-08-13 20:25:54 · 368 阅读 · 0 评论 -
删除链表中重复的节点
/*在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留, 返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5*///设置三个指针ListNode* deleteDuplication(ListNode* pHead){ if (NULL == pHead || NULL...原创 2018-08-13 19:20:19 · 122 阅读 · 0 评论 -
表示数值的字符串
题目描述请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是bool isNumeric(char*转载 2018-08-13 17:26:08 · 108 阅读 · 0 评论 -
正则表达式
题目描述请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配 bool match(char* str, char* pattern) ...原创 2018-08-13 17:11:29 · 117 阅读 · 0 评论 -
数组中重复的数字
题目描述在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。int duplicate(int numbers[], int length) { for (int i ...原创 2018-08-13 16:39:01 · 95 阅读 · 0 评论 -
字符串翻转问题
//三步翻转法void reverseString(string &str, int start, int end){ if (start > end) return; while (start < end) swap(str[start++], str[end--]);}string LeftRotateString(string str, int n)...原创 2018-08-13 12:02:35 · 129 阅读 · 0 评论 -
删除字符串中重复的字符
#include<iostream>#include<string>using namespace std;int main(){ string str; cin >> str; string resultString; const int N = 256; bool hash[N] = {false}; //memset(hash...原创 2018-08-15 21:22:59 · 2443 阅读 · 0 评论 -
字符串分割c++实现
//将一个字符串分割成多个字符串,c为分割符(“,” 、“;” 、“ ”等)//s为输入的一个长字符串 //v为输出的多个子字符串void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c){ std::string::size_ty...转载 2018-08-15 21:18:10 · 402 阅读 · 0 评论 -
四种方法实现:找出数组中两个只出现一次的数字
//先排序然后查找void FindNumsAppearOnce1(vector<int> data, int* num1, int *num2) { if (data.size() < 2) return; sort(data.begin(), data.end()); vector<int> res; for (int i = 0; i <...原创 2018-08-13 10:04:03 · 2689 阅读 · 1 评论 -
swap使用,奇偶排序
#include <iostream>#include<ctime>#include<vector>#include<stack>#include <forward_list>#include<array>using namespace std;void fun(int a[], int len1, int b[...原创 2018-05-23 10:58:01 · 225 阅读 · 0 评论 -
动态规划实例
#include<iostream>#include<Windows.h>#include<assert.h>#include <algorithm>#include <functional>#include<vector>#include<math.h>#include<unorder翻译 2018-05-23 10:51:19 · 178 阅读 · 0 评论 -
数据冒险之二叉树(链表实现)
Node.h#ifndef NODE_H#define NODE_Hclass Node{public: Node(); Node *SearchNode(int nodeIndex); //寻找索引号所在节点,若存在返回该节点,否则返回NULL void DeleteNode(); //删除节点 void PreorderTraverse()原创 2017-08-04 17:15:11 · 193 阅读 · 0 评论 -
数据冒险之二叉树(数组)
Tree.h#ifndef TREE_H#define TREE_H//数组实现class Tree{public: Tree(int size, int *pRoot); //创建树 ~Tree(); //销毁树 i原创 2017-08-04 15:30:50 · 176 阅读 · 0 评论 -
数据冒险之图以及最小生成树
点击打开链接转载 2017-08-18 18:41:07 · 176 阅读 · 0 评论 -
数据冒险之单链表(简易通信录)
Node和LIst的定义与实现基本不改变,上篇已经详细给出,这里仅仅将部分改变的内容粘贴出来,要想完整实现通信录还需要把Node.h、Node.cpp、List.h、List.cpp补充完整,也可发挥自己创造实现一些新的东西。Node.h 这里只是数据类型发生了变化#ifndef NODE_H#define NODE_H#include"Person.h"class Node{原创 2017-08-03 11:29:10 · 273 阅读 · 0 评论 -
数据冒险之顺序表
List.h#ifndef LIST_H#define LIST_H/******顺序表*****/class List{public: List(int size); //创建线性表 ~List(); //销毁线性表 void ClearList(原创 2017-07-31 21:07:27 · 278 阅读 · 0 评论 -
数据冒险之单链表
定义链表的结点:Node.h#ifndef NODE_H#define NODE_Hclass Node{public: int data; Node *next; void printNode();};#endif 打印结点数据:Node.cpp#include"Node.h"#includeusing namespace std;void Node::p原创 2017-07-31 21:48:57 · 198 阅读 · 0 评论 -
绘制星号图----坐标分析法
坐标分析法:如下是几个常见的实例第一种常见图:void main(){ /*坐标分析法*/ for (int iy = -3; iy <= 3; iy++) { for (int ix = 0; ix < abs(iy); ix++) cout << " "; for (int ix = -(3-abs(iy)); ix <= 3-abs(iy); ix++) cout原创 2017-07-18 16:09:31 · 1092 阅读 · 0 评论 -
输入3个学生的学号、姓名和三门功课成绩,输出平均分最高的同学
新建一个学生类Student.h:属性有姓名、学号、三门功课成绩#includeusing namespace std;class Student{public: Student():classA_score(0), classB_score(0), classC_score(0){ } void setClassA_score(double A_score); void s原创 2017-07-18 18:14:47 · 18580 阅读 · 2 评论 -
数据冒险之栈应用(数制转换、括号匹配)
#include#include"stdlib.h"#include"MyStack.h"using namespace std;/******************************栈应用----数制转换描述:输入任意的十进制正整数N,分别输出该整数N的二进制、八进制、十六进制的数公式:N=(N div d)*d+N mod d (div表示整除, mod表示求原创 2017-07-31 09:23:37 · 364 阅读 · 0 评论 -
数据冒险之栈
MyStack.h#ifndef MYSTACK_H#define MYSTACK_Htemplateclass MyStack{public: MyStack(int size); //分配内存初始化栈空间,设定栈容量,栈顶 ~MyStack(); //回收栈空间内存,防止内存泄漏 bool stackEmpty(); //判原创 2017-07-31 09:10:42 · 204 阅读 · 0 评论 -
数据冒险之队列实例
Customer.h#ifndef CUSTOMER_H#define CUSTOMER_H#includeusing namespace std;class Customer{public: //Customer(); Customer(string name="", int age=0); void printInfo() const;private: string原创 2017-07-30 19:45:48 · 231 阅读 · 0 评论 -
数据探险之循环队列
MyQueue.h#ifndef MYQUEUE_H#define MYQUEUE_Hclass MyQueue{public: MyQueue(int quequeCapacity); // 创建队列 virtual ~MyQueue(); // 销毁队列 void ClearQueue(); //清空队列原创 2017-07-30 19:01:01 · 172 阅读 · 0 评论 -
快速排序
#include using namespace std;int a[5] = { 1, 4, 2, 5, 3 };void quickSort(int left, int right){ if (left>right) { return; } int temp = a[left]; int i = left; int j = right; while (i != j)原创 2017-08-05 16:41:00 · 223 阅读 · 0 评论 -
文件的操作(文件的读写、计算文件大小)
#include using namespace std;/*文件的读写*/int main1(){ FILE *fp; char pathName[20], txt1[20] = { '\0' }, txt2[20] = { '\0' }; int fileLen; cout << "输入文件名:"<<endl; cin >> pathName; fp = fopen(pa原创 2017-08-06 16:09:40 · 387 阅读 · 0 评论 -
最大公约数、最小公倍数
#include using namespace std;/*最大公约数*/int gcd(int a, int b){ int min = 0; if (a <= 0 || b <= 0) return -1; if (a > b) min = b; else min = a; while (min) { if (a%min == 0 && b%min ==原创 2017-08-06 16:34:51 · 177 阅读 · 0 评论