diff --git "a/---Others---/347円275円221円347円273円234円347円274円226円347円250円213円345円237円272円346円234円254円347円232円204円api/epoll/epoll_test.cpp" "b/---Others---/347円275円221円347円273円234円347円274円226円347円250円213円345円237円272円346円234円254円347円232円204円api/epoll/epoll_test.cpp" new file mode 100644 index 0000000..132b1be --- /dev/null +++ "b/---Others---/347円275円221円347円273円234円347円274円226円347円250円213円345円237円272円346円234円254円347円232円204円api/epoll/epoll_test.cpp" @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define MAXEPOLL 10000 +#define MAXLINE 1024 +#define PORT 6000 +#define MAXBACK 1000 + +//设置非阻塞 +int setnonblocking(int fd) +{ + if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK) == -1) + { + printf("Set blocking error : %d\n", errno); + return -1; + } + return 0; +} + +int main(int argc, char **argv) +{ + int listen_fd; + int conn_fd; + int epoll_fd; + int nread; + int cur_fds; //!> 当前已经存在的数量 + int wait_fds; //!> epoll_wait 的返回值 + int i; + struct sockaddr_in servaddr; + struct sockaddr_in cliaddr; + struct epoll_event ev; + struct epoll_event evs[MAXEPOLL]; + struct rlimit rlt; //!> 设置连接数所需 + char buf[MAXLINE]; + socklen_t len = sizeof(struct sockaddr_in); + + //设置每个进程允许打开的最大文件数 + //每个主机是不一样的哦,一般服务器应该很大吧! + rlt.rlim_max = rlt.rlim_cur = MAXEPOLL; + if (setrlimit(RLIMIT_NOFILE, &rlt) == -1) + { + printf("Setrlimit Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //!> server 套接口 + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(PORT); + + //建立套接字 + if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + printf("Socket Error...%d\n", errno); + exit(EXIT_FAILURE); + } + + //设置非阻塞模式 + if (setnonblocking(listen_fd) == -1) + { + printf("Setnonblocking Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //绑定 + if (bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) == -1) + { + printf("Bind Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //监听 + if (listen(listen_fd, MAXBACK) == -1) + { + printf("Listen Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //创建epoll + epoll_fd = epoll_create(MAXEPOLL); //!> create + ev.events = EPOLLIN | EPOLLET; //!> accept Read! + ev.data.fd = listen_fd; //!> 将listen_fd 加入 + if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &ev) < 0) + { + printf("Epoll Error : %d\n", errno); + exit(EXIT_FAILURE); + } + cur_fds = 1; + + while (1) + { + if ((wait_fds = epoll_wait(epoll_fd, evs, cur_fds, -1)) == -1) + { + printf("Epoll Wait Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + for (i = 0; i < wait_fds; i++) + { + // 处理新接入的accept + if (evs[i].data.fd == listen_fd && cur_fds < MAXEPOLL) + { + cout << "debug, evs[" << i << "] events " << ev.events << " EPOLLIN : " << EPOLLIN << endl; + + if ((conn_fd = accept(listen_fd, (struct sockaddr *)&cliaddr, &len)) == -1) + { + printf("Accept Error : %d\n", errno); + exit(EXIT_FAILURE); + } + printf("Server get from client !\n" /*, inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port */); + ev.events = EPOLLIN | EPOLLET; //!> accept Read! + ev.data.fd = conn_fd; //!> 将conn_fd 加入 + if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, conn_fd, &ev) < 0) + { + printf("Epoll Error : %d\n", errno); + exit(EXIT_FAILURE); + } + ++cur_fds; + continue; + } + //!> 下面处理数据 + + cout << "deug read, evs[" << i << "] events " << ev.events << " EPOLLIN : " << EPOLLIN << endl; + nread = read(evs[i].data.fd, buf, sizeof(buf)); + if (nread <= 0) //!> 结束后者出错 + { + close(evs[i].data.fd); + epoll_ctl(epoll_fd, EPOLL_CTL_DEL, evs[i].data.fd, &ev); //!> 删除计入的fd + --cur_fds; //!> 减少一个呗! + continue; + } + write(evs[i].data.fd, buf, nread); //!> 回写 + } + } + + close(listen_fd); + return 0; +} \ No newline at end of file diff --git "a/---Others---/347円275円221円347円273円234円347円274円226円347円250円213円345円237円272円346円234円254円347円232円204円api/epoll/epoll_test_event.cpp" "b/---Others---/347円275円221円347円273円234円347円274円226円347円250円213円345円237円272円346円234円254円347円232円204円api/epoll/epoll_test_event.cpp" new file mode 100644 index 0000000..3e0ccdf --- /dev/null +++ "b/---Others---/347円275円221円347円273円234円347円274円226円347円250円213円345円237円272円346円234円254円347円232円204円api/epoll/epoll_test_event.cpp" @@ -0,0 +1,228 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define IPADDRESS "127.0.0.1" +#define PORT 6000 +#define MAXSIZE 1024 +#define LISTENQ 5 +#define FDSIZE 1000 +#define EPOLLEVENTS 100 + +#define MAXLINE 1024 +char buf[MAXLINE]; + +static void handle_accpet(int epollfd, int listenfd); +static void do_read(int epollfd, int fd, char *buf); +static void do_write(int epollfd, int fd, char *buf); +static void delete_event(int epollfd, int fd, int state); +static void modify_event(int epollfd, int fd, int state); + +//设置非阻塞 +int setnonblocking(int fd) +{ + if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFD, 0) | O_NONBLOCK) == -1) + { + printf("Set blocking error : %d\n", errno); + return -1; + } + return 0; +} + +//事件处理函数 +static void handle_events(int epollfd, struct epoll_event *events, int num, int listenfd, char *buf) +{ + int i; + int fd; + //进行遍历;这里只要遍历已经准备好的io事件。num并不是当初epoll_create时的FDSIZE。 + + cout << "[debug] num : " << num << " beign for" << endl; + for (i = 0; i < num; i++) + { + fd = events[i].data.fd; + //根据描述符的类型和事件类型进行处理 + if ((fd == listenfd) && (events[i].events & EPOLLIN)) + { + handle_accpet(epollfd, listenfd); + } + else if (events[i].events & EPOLLIN) + { + // 找do_read函数中,读到的数据写到buff上面 + do_read(epollfd, fd, buf); + } + else if (events[i].events & EPOLLOUT) + { + cout << "[debug] write" << endl; + do_write(epollfd, fd, buf); + } + } + cout << "[debug] end for" << endl; + cout << endl; +} + +//添加事件 +static void add_event(int epollfd, int fd, int state) +{ + struct epoll_event ev; + ev.events = state; + ev.data.fd = fd; + epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); +} + +//处理接收到的连接 +static void handle_accpet(int epollfd, int listenfd) +{ + int clifd; + struct sockaddr_in cliaddr; + socklen_t cliaddrlen; + clifd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddrlen); + if (clifd == -1) + perror("accpet error:"); + else + { + printf("accept a new client: %s:%d\n", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port); //添加一个客户描述符和事件 + add_event(epollfd, clifd, EPOLLIN); + } +} + +//读处理 +static void do_read(int epollfd, int fd, char *buf) +{ + int nread; + // read数据到buf上面 + nread = read(fd, buf, MAXSIZE); + if (nread == -1) + { + perror("read error:"); + close(fd); //记住close fd + delete_event(epollfd, fd, EPOLLIN); //删除监听 + } + else if (nread == 0) + { + fprintf(stderr, "client close.\n"); + close(fd); //记住close fd + delete_event(epollfd, fd, EPOLLIN); //删除监听 + } + else + { + printf("[fd %d] read message is : %s", fd, buf); + //修改描述符对应的事件,由读改为写 + modify_event(epollfd, fd, EPOLLOUT); + } +} + +//写处理 +static void do_write(int epollfd, int fd, char *buf) +{ + int nwrite; + nwrite = write(fd, buf, strlen(buf)); + if (nwrite == -1) + { + perror("write error:"); + close(fd); //记住close fd + delete_event(epollfd, fd, EPOLLOUT); //删除监听 + } + else + { + //修改描述符对应的事件,由写改为读 + modify_event(epollfd, fd, EPOLLIN); + } + memset(buf, 0, MAXSIZE); +} + +//删除事件 +static void delete_event(int epollfd, int fd, int state) +{ + struct epoll_event ev; + ev.events = state; + ev.data.fd = fd; + epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev); +} + +//修改事件 +static void modify_event(int epollfd, int fd, int state) +{ + struct epoll_event ev; + ev.events = state; + ev.data.fd = fd; + epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &ev); +} + +int socket_bind(int port) +{ + int listen_fd; + + struct sockaddr_in servaddr; + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(port); + + //建立套接字 + if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + printf("Socket Error...%d\n", errno); + exit(EXIT_FAILURE); + } + + //设置非阻塞模式 + // if (setnonblocking(listen_fd) == -1) + // { + // printf("Setnonblocking Error : %d\n", errno); + // exit(EXIT_FAILURE); + // } + + //绑定 + if (bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) == -1) + { + printf("Bind Error : %d\n", errno); + exit(EXIT_FAILURE); + } + + //监听 + if (listen(listen_fd, LISTENQ) == -1) + { + printf("Listen Error : %d\n", errno); + exit(EXIT_FAILURE); + } + cout << "listen init" << endl; + return listen_fd; +} + +int main() +{ + cout << "run.." << endl; + + int listenfd = socket_bind(PORT); + + struct epoll_event events[EPOLLEVENTS]; + + //创建一个描述符 + int epollfd = epoll_create(FDSIZE); + + //添加监听描述符事件 + add_event(epollfd, listenfd, EPOLLIN); + + //循环等待 + for (;;) + { + //该函数返回已经准备好的描述符事件数目 + int ret = epoll_wait(epollfd, events, EPOLLEVENTS, -1); + //处理接收到的连接 + handle_events(epollfd, events, ret, listenfd, buf); + } +} + +// from https://segmentfault.com/a/1190000003063859 \ No newline at end of file diff --git "a/---Others---/347円275円221円347円273円234円347円274円226円347円250円213円345円237円272円346円234円254円347円232円204円api/poll/poll_test.c" "b/---Others---/347円275円221円347円273円234円347円274円226円347円250円213円345円237円272円346円234円254円347円232円204円api/poll/poll_test.c" new file mode 100644 index 0000000..cfc42dc --- /dev/null +++ "b/---Others---/347円275円221円347円273円234円347円274円226円347円250円213円345円237円272円346円234円254円347円232円204円api/poll/poll_test.c" @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_FD_NUM 20 +#define MAXLEN 1024 + +int main(int argc,char* argv[]) +{ + printf("server start up\n"); + + if(argc <= 2) + { + printf("usage:%s ip port\n",basename(argv[0])); + return 1; + } + + //IP��ַ + const char* ip = argv[1]; + //�˿ں� + int port = atoi(argv[2]); + //�ں˼������е����󳤶ȣ���ȫ���ӵ�socket�� + //int backlog = atoi(argv[3]); + + //����socket (TIP/IPЭ���壬��ʽsocket) + int server_sockfd = socket(PF_INET,SOCK_STREAM,0); + + //TCP/IPЭ������socket��ַ�ṹ�� + struct sockaddr_in server_addr; + bzero(&server_addr,sizeof(server_addr)); + server_addr.sin_family = AF_INET; //TCP/IPv4�ĵ�ַ�� + inet_pton(AF_INET,ip,&server_addr.sin_addr); //��IP��ַ�ַ���ת��Ϊ�����Ƶ������addr.sin_server_addr + server_addr.sin_port = htons(port); //�˿ڣ�host to net���������ֽ�����С�x�ת��Ϊ�����ֽ��򣨴��x� + + //���ļ�������sock��socket��ַ����������������Ҫ���ͻ����Զ��󶨵�ַ + //ע����Ҫǿ��ת��Ϊ struct sockaddr* + int ret = bind(server_sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr)); + assert(ret != -1); + + //���� + ret = listen(server_sockfd,MAX_FD_NUM-1); + assert(ret != -1); + + //�ȴ��ͻ�����Щ���ӵ����ع��� + sleep(3); + + //�ͻ��˵�ַ��Ϣ + struct sockaddr_in client_addr; + socklen_t client_addr_len = sizeof(struct sockaddr_in); + + //poll fds + struct pollfd pollfdArry[MAX_FD_NUM]; + for(int i=0;i MAX_FD_NUM) + { + printf("socket num to much\n"); + } + else + { + //��������,���������ܵ�Զ��sock��ַ��Ϣ�����ڵڶ��������� + //ֻ�ǴӼ���������ȡ�����ӣ���ʹ�ͻ����Ѿ��Ͽ���������Ҳ��accept�ɹ� + int client_sockfd = accept(server_sockfd,(struct sockaddr*)&client_addr,&client_addr_len); + if(client_sockfd < 0) + { + perror("accept"); + } + else + { + //inet_ntoa(struct addr_in) ��IP��ַת��Ϊ�ַ����� + printf("accept client_addr %s\n",inet_ntoa(client_addr.sin_addr)); + for(int i=0;i path) +{ + + if(root != null) + { + //visit操作 + path.push_back(root->val); + + if (root->left == NULL && root->right == NULL) + { + print_vector(path); + } + + //递归 + solution(root->left,path); + solution(root->right,path); + + //清除递归条件 + path.pop_back(); + } +} +``` \ No newline at end of file diff --git "a/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円345円257円273円346円211円276円347円273円223円347円202円271円345円210円260円345円255円220円350円212円202円347円202円271円347円232円204円350円267円257円345円276円204円/readme.md" "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円345円257円273円346円211円276円347円273円223円347円202円271円345円210円260円345円255円220円350円212円202円347円202円271円347円232円204円350円267円257円345円276円204円/readme.md" new file mode 100644 index 0000000..bc98269 --- /dev/null +++ "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円345円257円273円346円211円276円347円273円223円347円202円271円345円210円260円345円255円220円350円212円202円347円202円271円347円232円204円350円267円257円345円276円204円/readme.md" @@ -0,0 +1,108 @@ +## 二叉树从根节点到子节点的路径 + +``` +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct Node +{ + int val; + Node * left; + Node * right; +}; +int get_index(vector vi, int val) +{ + int index = 0; + for (auto ele : vi) + { + if (ele == val) + { + return index; + } + index++; + } + return -1; +} +Node * solution(vector pre, vector mid) +{ + if (pre.size()==0||pre.size()!=mid.size()) + { + return NULL; + } + else + { + Node * root = new Node(); + root->val = pre[0]; + root->left = NULL; + root->right = NULL; + + int index = get_index(mid, pre[0]); + + vector pre_left(pre.begin()+1, pre.begin()+index+1); + vector pre_right(pre.begin()+index+1, pre.end()); + vector mid_left(mid.begin(),mid.begin()+index); + vector mid_right(mid.begin()+index+1, mid.end()); + + + + root->left = solution(pre_left, mid_left); + root->right = solution(pre_right, mid_right); + return root; + } +} +void print_vector(vector path) +{ + for (auto ele : path) + { + cout << ele << " "; + } + cout << endl; +} + + +// 寻找根节点到子节点的所有路径 +// 这个是类似与前序遍历的递归 +void solution2(Node * root,vector & path) +{ + if (root != NULL) + { + //类似于前序遍历里面的visit操作 + path.push_back(root->val); + if (root->left == NULL && root->right == NULL) + { + print_vector(path); + } + + //递归 + solution2(root->left, path); + solution2(root->right, path); + + //清楚递归条件 + path.pop_back(); + } +} + +int main() +{ + //构造二叉树 + vector pre = { 1,2,4,7,3,5,6,8 }; + vector mid = { 4,7,2,1,5,3,8,6 }; + Node * root = solution(pre, mid); + + //打印根结点到子节点的路径 + vector path; + solution2(root,path); + + return 0; +} + +``` \ No newline at end of file diff --git "a/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円/344円272円214円345円217円211円346円240円221円347円232円204円345円210円206円345円261円202円346円211円223円345円215円260円.md" "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円/344円272円214円345円217円211円346円240円221円347円232円204円345円210円206円345円261円202円346円211円223円345円215円260円.md" new file mode 100644 index 0000000..ba7c091 --- /dev/null +++ "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円/344円272円214円345円217円211円346円240円221円347円232円204円345円210円206円345円261円202円346円211円223円345円215円260円.md" @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +struct BinaryTree { + int vec; + BinaryTree* left; + BinaryTree* right; + BinaryTree(int data) + :vec(data), left(nullptr), right(nullptr) { + } +}; + +//层序遍历二叉树 +void travel(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + cout << q->vec << endl; + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + } +} + +//分层打印二叉树 +void travel_by_layer(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + int size = bt_queue.size(); + for (int i = 0; i < size; i++) + { + BinaryTree * q = bt_queue.front(); + cout << q->vec << " "; + bt_queue.pop(); + + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + cout << endl; + } + } +} + + +int main() +{ + BinaryTree* s_arr[6]; + s_arr[0] = new BinaryTree(0); + s_arr[1] = new BinaryTree(1); + s_arr[2] = new BinaryTree(2); + s_arr[3] = new BinaryTree(3); + s_arr[4] = new BinaryTree(4); + s_arr[5] = new BinaryTree(5); + s_arr[0]->left = s_arr[1]; // 0 + s_arr[0]->right = s_arr[2]; // 1 2 + s_arr[1]->left = s_arr[3]; // 3 5 + s_arr[3]->left = s_arr[4]; //4 + s_arr[2]->right = s_arr[5]; //所以层序遍历的结果为:0 1 2 3 5 4 + + //travel(s_arr[0]); + travel_by_layer(s_arr[0]); + + return 0; +} \ No newline at end of file diff --git "a/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円/344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円.md" "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円/344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円.md" new file mode 100644 index 0000000..ba7c091 --- /dev/null +++ "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円/344円272円214円345円217円211円346円240円221円347円232円204円345円261円202円345円272円217円351円201円215円345円216円206円.md" @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +struct BinaryTree { + int vec; + BinaryTree* left; + BinaryTree* right; + BinaryTree(int data) + :vec(data), left(nullptr), right(nullptr) { + } +}; + +//层序遍历二叉树 +void travel(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + cout << q->vec << endl; + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + } +} + +//分层打印二叉树 +void travel_by_layer(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + int size = bt_queue.size(); + for (int i = 0; i < size; i++) + { + BinaryTree * q = bt_queue.front(); + cout << q->vec << " "; + bt_queue.pop(); + + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + cout << endl; + } + } +} + + +int main() +{ + BinaryTree* s_arr[6]; + s_arr[0] = new BinaryTree(0); + s_arr[1] = new BinaryTree(1); + s_arr[2] = new BinaryTree(2); + s_arr[3] = new BinaryTree(3); + s_arr[4] = new BinaryTree(4); + s_arr[5] = new BinaryTree(5); + s_arr[0]->left = s_arr[1]; // 0 + s_arr[0]->right = s_arr[2]; // 1 2 + s_arr[1]->left = s_arr[3]; // 3 5 + s_arr[3]->left = s_arr[4]; //4 + s_arr[2]->right = s_arr[5]; //所以层序遍历的结果为:0 1 2 3 5 4 + + //travel(s_arr[0]); + travel_by_layer(s_arr[0]); + + return 0; +} \ No newline at end of file diff --git "a/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円344円275円216円345円205円254円345円205円261円347円245円226円345円205円210円/readme.md" "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円344円275円216円345円205円254円345円205円261円347円245円226円345円205円210円/readme.md" index 7f6d345..a59e04f 100644 --- "a/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円344円275円216円345円205円254円345円205円261円347円245円226円345円205円210円/readme.md" +++ "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円346円234円200円344円275円216円345円205円254円345円205円261円347円245円226円345円205円210円/readme.md" @@ -1,4 +1,4 @@ -## 二叉树的最低公共祖先 +## 二叉树的最近公共祖先 给出二叉树上的两个结点A,B,返回他们的最低公共祖先。 diff --git "a/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円351円235円236円351円200円222円345円275円222円351円201円215円345円216円206円/readme.md" "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円351円235円236円351円200円222円345円275円222円351円201円215円345円216円206円/readme.md" new file mode 100644 index 0000000..637d154 --- /dev/null +++ "b/---344円272円214円345円217円211円346円240円221円---/344円272円214円345円217円211円346円240円221円347円232円204円351円235円236円351円200円222円345円275円222円351円201円215円345円216円206円/readme.md" @@ -0,0 +1,132 @@ +## 二叉树的 前序/中序/后序 遍历 + +``` +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct BinaryTree { + int vec; + BinaryTree* left; + BinaryTree* right; + BinaryTree(int data) + :vec(data), left(nullptr), right(nullptr) { + } + int flag; +}; + +void pre_travel(BinaryTree * root) +{ + stack bt_stack; + while (root != NULL || !bt_stack.empty()) + { + while (root != NULL) + { + cout << root->vec << endl; + bt_stack.push(root); + root = root->left; + } + if (!bt_stack.empty()) + { + root = bt_stack.top(); + bt_stack.pop(); + root = root->right; + } + } +} + +void mid_travel(BinaryTree * root) +{ + stack bt_stack; + while (root != NULL || !bt_stack.empty()) + { + while (root != NULL) + { + bt_stack.push(root); + root = root->left; + } + if (!bt_stack.empty()) + { + root = bt_stack.top(); + cout << root->vec << endl; + bt_stack.pop(); + root = root->right; + } + } +} + +/* +对于任一结点P,将其入栈,然后沿其左子树一直往下搜索,直到搜索到没有左孩子的结点,此时该结点出现在栈顶, +但是此时不能将其出栈并访问,因此其右孩子还为被访问。 + +所以接下来按照相同的规则对其右子树进行相同的处理,当访问完其右孩子时,该结点又出现在栈顶, +此时可以将其出栈并访问。这样就保证了正确的访问顺序。 + +可以看出,在这个过程中,每个结点都两次出现在栈顶,只有在第二次出现在栈顶时,才能访问它。 +因此需要多设置一个变量标识该结点是否是第一次出现在栈顶。 +*/ +void post_travel(BinaryTree * root) +{ + stack bt_stack; + while (root != NULL || !bt_stack.empty()) + { + while (root != NULL) + { + root->flag = 0; + bt_stack.push(root); + root = root->left; + } + + if (!bt_stack.empty()) + { + root = bt_stack.top(); + bt_stack.pop(); + + //flag =0 说明右子树没有遍历 + if (root->flag == 0) + { + root->flag = 1; + bt_stack.push(root); + root = root->right; + } + else + { + cout << root->vec << endl; + root = NULL; + } + } + } +} + +int main() +{ + BinaryTree* s_arr[6]; + s_arr[0] = new BinaryTree(0); + s_arr[1] = new BinaryTree(1); + s_arr[2] = new BinaryTree(2); + s_arr[3] = new BinaryTree(3); + s_arr[4] = new BinaryTree(4); + s_arr[5] = new BinaryTree(5); + s_arr[0]->left = s_arr[1]; // 0 + s_arr[0]->right = s_arr[2]; // 1 2 + s_arr[1]->left = s_arr[3]; // 3 5 + s_arr[3]->left = s_arr[4]; //4 + s_arr[2]->right = s_arr[5]; //所以层序遍历的结果为:0 1 2 3 5 4 + + //travel(s_arr[0]); + //travel_by_layer(s_arr[0]); + + //cout << iscomplete_tree(s_arr[0]) << endl; + + //pre_travel(s_arr[0]); + //mid_travel(s_arr[0]); + post_travel(s_arr[0]); + + return 0; +} +``` \ No newline at end of file diff --git "a/---344円272円214円345円217円211円346円240円221円---/345円210円244円346円226円255円344円270円200円346円243円265円346円240円221円346円230円257円344円270円215円346円230円257円345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円/readme.md" "b/---344円272円214円345円217円211円346円240円221円---/345円210円244円346円226円255円344円270円200円346円243円265円346円240円221円346円230円257円344円270円215円346円230円257円345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円/readme.md" new file mode 100644 index 0000000..126a79d --- /dev/null +++ "b/---344円272円214円345円217円211円346円240円221円---/345円210円244円346円226円255円344円270円200円346円243円265円346円240円221円346円230円257円344円270円215円346円230円257円345円256円214円345円205円250円344円272円214円345円217円211円346円240円221円/readme.md" @@ -0,0 +1,136 @@ +## 判断一棵树是不是完全二叉树 + +``` +#include +#include +#include +#include +#include +#include +using namespace std; + +struct BinaryTree { + int vec; + BinaryTree* left; + BinaryTree* right; + BinaryTree(int data) + :vec(data), left(nullptr), right(nullptr) { + } +}; + +//层序遍历二叉树 +void travel(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + cout << q->vec << endl; + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + } +} + +//分层打印二叉树 +void travel_by_layer(BinaryTree * root) +{ + if (root != NULL) + { + queue bt_queue; + bt_queue.push(root); + while (!bt_queue.empty()) + { + int size = bt_queue.size(); + for (int i = 0; i < size; i++) + { + BinaryTree * q = bt_queue.front(); + cout << q->vec << " "; + bt_queue.pop(); + + if (q->left != NULL) + { + bt_queue.push(q->left); + } + if (q->right != NULL) + { + bt_queue.push(q->right); + } + } + cout << endl; + } + } +} + + +/* +任意的一个二叉树,都可以补成一个满二叉树。这样中间就会有很多空洞。在广度优先遍历的时候,如果是满二叉树,或者完全二叉树,这些空洞是在广度优先的遍历的末尾,所以,但我们遍历到空洞的时候,整个二叉树就已经遍历完成了。而如果,是非完全二叉树, + +我们遍历到空洞的时候,就会发现,空洞后面还有没有遍历到的值。这样,只要根据是否遍历到空洞,整个树的遍历是否结束来判断是否是完全的二叉树。 +*/ +bool iscomplete_tree(BinaryTree * root) +{ + queue bt_queue; + if (root != NULL) + { + bt_queue.push(root); + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + if (q == NULL) + { + break; + } + bt_queue.push(q->left); + bt_queue.push(q->right); + } + } + + while (!bt_queue.empty()) + { + BinaryTree * q = bt_queue.front(); + bt_queue.pop(); + + if (q != NULL) + { + return false; + } + } + return true; +} + + +int main() +{ + BinaryTree* s_arr[6]; + s_arr[0] = new BinaryTree(0); + s_arr[1] = new BinaryTree(1); + s_arr[2] = new BinaryTree(2); + s_arr[3] = new BinaryTree(3); + s_arr[4] = new BinaryTree(4); + s_arr[5] = new BinaryTree(5); + s_arr[0]->left = s_arr[1]; // 0 + s_arr[0]->right = s_arr[2]; // 1 2 + s_arr[1]->left = s_arr[3]; // 3 5 + s_arr[3]->left = s_arr[4]; //4 + s_arr[2]->right = s_arr[5]; //所以层序遍历的结果为:0 1 2 3 5 4 + + //travel(s_arr[0]); + //travel_by_layer(s_arr[0]); + + cout << iscomplete_tree(s_arr[0]) << endl; + + return 0; +} +``` \ No newline at end of file diff --git "a/---345円205円263円344円272円216円344円272円214円345円217円211円346円240円221円347円232円204円351円242円230円347円233円256円---/351円200円232円350円277円207円345円211円215円345円272円217円345円222円214円344円270円255円345円272円217円346円236円204円345円273円272円344円272円214円345円217円211円346円240円221円/readme.md" "b/---345円205円263円344円272円216円344円272円214円345円217円211円346円240円221円347円232円204円351円242円230円347円233円256円---/351円200円232円350円277円207円345円211円215円345円272円217円345円222円214円344円270円255円345円272円217円346円236円204円345円273円272円344円272円214円345円217円211円346円240円221円/readme.md" new file mode 100644 index 0000000..2542b0f --- /dev/null +++ "b/---345円205円263円344円272円216円344円272円214円345円217円211円346円240円221円347円232円204円351円242円230円347円233円256円---/351円200円232円350円277円207円345円211円215円345円272円217円345円222円214円344円270円255円345円272円217円346円236円204円345円273円272円344円272円214円345円217円211円346円240円221円/readme.md" @@ -0,0 +1,61 @@ +# 通过前序和中序遍历构建二叉树 + +``` + +struct Node +{ + int val; + Node * left; + Node * right; +}; +int get_index(vector vi, int val) +{ + int index = 0; + for (auto ele : vi) + { + if (ele == val) + { + return index; + } + index++; + } + return -1; +} +Node * solution(vector pre, vector mid) +{ + if (pre.size()==0||pre.size()!=mid.size()) + { + return NULL; + } + else + { + Node * root = new Node(); + root->val = pre[0]; + root->left = NULL; + root->right = NULL; + + int index = get_index(mid, pre[0]); + + vector pre_left(pre.begin()+1, pre.begin()+index+1); + vector pre_right(pre.begin()+index+1, pre.end()); + vector mid_left(mid.begin(),mid.begin()+index); + vector mid_right(mid.begin()+index+1, mid.end()); + + + + root->left = solution(pre_left, mid_left); + root->right = solution(pre_right, mid_right); + return root; + } +} + +int main() +{ + vector pre = { 1,2,4,7,3,5,6,8 }; + vector mid = { 4,7,2,1,5,3,8,6 }; + + Node * root = solution(pre, mid); + + return 0; +} +``` \ No newline at end of file diff --git "a/---347円231円275円346円235円277円345円206円231円344円273円243円347円240円201円---/question.md" "b/---347円231円275円346円235円277円345円206円231円344円273円243円347円240円201円---/question.md" deleted file mode 100644 index 5222914..0000000 --- "a/---347円231円275円346円235円277円345円206円231円344円273円243円347円240円201円---/question.md" +++ /dev/null @@ -1,96 +0,0 @@ - -### 白板写代码 - -1. 链表排序 - - ``` - #include - #include - #include - using namespace std; - - - struct node - { - int val; - node * next; - node(int value) { val = value; next = NULL; } - }; - - //链表排序 - node* list_sort(node * head) { - if (head == NULL||head->next==NULL) { - return head; - } - - node * p_cur = head->next; - head->next = NULL; - //创建一个虚拟头结点 - node * sortedhead = new node(INT_MIN); - sortedhead->next = head; - - while (p_cur != NULL) { - node * temp = p_cur->next; - p_cur->next = NULL; - - node * sorted_begin = sortedhead; - node * sorted_end = sortedhead->next; - - //插入p_cur - while (sorted_end != NULL) { - if (p_cur->val>= sorted_begin->val && p_cur->val <= sorted_end->val) { - //插入在begin 和end中间 - sorted_begin->next = p_cur; - p_cur->next = sorted_end; - sorted_begin = sorted_begin->next; - break; - } - else { - sorted_begin=sorted_begin->next; - sorted_end =sorted_end->next; - } - } - //将p_cur加入尾部 - if (sorted_end == NULL) { - sorted_begin->next = p_cur; - } - - //指向下一个,继续插入 - p_cur = temp; - } - - - return sortedhead->next; - } - - int main() - { - node head(5); - node n1(3); - node n2(8); - node n3(7); - node n4(1); - - head.next = &n1; - n1.next = &n2; - n2.next = &n3; - n3.next = &n4; - - node * new_head = list_sort(&head); - - - - system("pause"); - return 0; - } - ``` - -* 两个有序数组归并 - -* 顺时针打印矩阵(todo) - -* 快速排序 - -* 反转链表 - -* 将一个链表拆分成两个(奇数位组成一个链表;偶数位组成一个链表) \ No newline at end of file diff --git "a/---347円231円275円346円235円277円345円206円231円344円273円243円347円240円201円---/readme.md" "b/---347円231円275円346円235円277円345円206円231円344円273円243円347円240円201円---/readme.md" new file mode 100644 index 0000000..656ee01 --- /dev/null +++ "b/---347円231円275円346円235円277円345円206円231円344円273円243円347円240円201円---/readme.md" @@ -0,0 +1,271 @@ + +### 白板写代码 + +1. 链表排序 + + ``` + #include + #include + #include + using namespace std; + + + struct node + { + int val; + node * next; + node(int value) { val = value; next = NULL; } + }; + + //链表排序 + node* list_sort(node * head) { + if (head == NULL||head->next==NULL) { + return head; + } + + node * p_cur = head->next; + head->next = NULL; + //创建一个虚拟头结点 + node * sortedhead = new node(INT_MIN); + sortedhead->next = head; + + while (p_cur != NULL) { + node * temp = p_cur->next; + p_cur->next = NULL; + + node * sorted_begin = sortedhead; + node * sorted_end = sortedhead->next; + + //插入p_cur + while (sorted_end != NULL) { + if (p_cur->val>= sorted_begin->val && p_cur->val <= sorted_end->val) { + //插入在begin 和end中间 + sorted_begin->next = p_cur; + p_cur->next = sorted_end; + sorted_begin = sorted_begin->next; + break; + } + else { + sorted_begin=sorted_begin->next; + sorted_end =sorted_end->next; + } + } + //将p_cur加入尾部 + if (sorted_end == NULL) { + sorted_begin->next = p_cur; + } + + //指向下一个,继续插入 + p_cur = temp; + } + + + return sortedhead->next; + } + + int main() + { + node head(5); + node n1(3); + node n2(8); + node n3(7); + node n4(1); + + head.next = &n1; + n1.next = &n2; + n2.next = &n3; + n3.next = &n4; + + node * new_head = list_sort(&head); + + + + system("pause"); + return 0; + } + ``` + +* 两个有序数组归并 + +* 顺时针打印矩阵(todo) + +* 快速排序 + + ``` + int partition(vector & arr, int begin, int end) + { + int partition_val = arr[end]; + int sorted_index = begin; + for (int i = begin; i < end; i++) + { + if (arr[i] < partition_val) + { + swap(arr[sorted_index], arr[i]); + sorted_index++; + } + } + swap(arr[sorted_index], arr[end]); + return sorted_index; + } + void quick_sort(vector &arr,int begin,int end) + { + int left = begin; + int right = end; + if (left < right) + { + int index = partition(arr, begin, end); + quick_sort(arr, begin, index - 1); + quick_sort(arr, index + 1, end); + } + } + + + int main() + { + + vector arr = { 5,8,1,3,10,9,4,0 }; + quick_sort(arr, 0, 7); + + return 0; + } + ``` + +* 反转链表 + +* 将一个链表拆分成两个(奇数位组成一个链表;偶数位组成一个链表) + +* 归并两个有序链表 + ``` + #include + #include + + using namespace std; + + struct ListNode + { + int val; + struct ListNode *next; + ListNode(int x) :val(x), next(NULL) {} + }; + + //根据vector去创建一个链表 + ListNode * create_list_by_vector(vector vi) + { + ListNode * head = new ListNode(vi[0]); + ListNode * p = head; + for (int i = 1; i < vi.size(); i++) + { + ListNode * node = new ListNode(vi[i]); + p->next = node; + p = p->next; + } + return head; + } + + ListNode* insert(ListNode * tail, ListNode * node) + { + node->next = NULL; + tail->next = node; + return node; + } + + ListNode * merge(ListNode * head1,ListNode * head2) + { + ListNode * newhead = new ListNode(-1); + ListNode * tail = newhead; + while(head1!=NULL && head2!=NULL) + { + if(head1->val < head2->val) + { + ListNode * p = head1->next; + //插入head1 + tail = insert(tail,head1); + head1 = p; + } + else + { + ListNode * p = head2->next; + //插入head2 + tail = insert(tail,head2); + head2 = p; + } + } + + if(head1!=NULL) + { + tail->next = head1; + } + else if(head2 != NULL) + { + tail->next = head2; + } + return newhead->next; + } + + void print_list(ListNode * head) + { + while(head!=NULL) + { + cout< val<next; + } + } + + int main() + { + int arr[] = {1,3,7,9,16 }; + vector vi1(arr,arr+5); + ListNode * head1 = create_list_by_vector(vi1); + + int arr2[]= {2,4,8,10,14,20}; + vector vi2(arr2,arr2+6); + ListNode * head2 = create_list_by_vector(vi2); + + ListNode * newhead = merge(head1,head2); + + print_list(newhead); + + return 0; + } + ``` + +* 第K大的数组 + ``` + int get_partition(vector &vi, int begin, int end) + { + int partition = vi[end]; + int sorted = begin; + for (int i = begin; i < end; i++) + { + if (partition> vi[i]) + { + swap(vi[i], vi[sorted]); + sorted++; + } + } + swap(vi[sorted], vi[end]); + return sorted; + } + + void topk(vector & vi,int k) + { + int begin = 0; + int end = vi.size() - 1; + int index = get_partition(vi, begin, end); + + while (index != k - 1) + { + if (index < k - 1) + { + index = get_partition(vi, index + 1, end); + } + else + { + index = get_partition(vi, begin, index-1); + } + } + + cout << vi[index] << endl; + } + + ``` diff --git a/1.PNG b/1.PNG deleted file mode 100644 index d40cbe0..0000000 Binary files a/1.PNG and /dev/null differ diff --git a/README.md b/README.md index f3469fa..b219794 100644 --- a/README.md +++ b/README.md @@ -148,10 +148,3 @@ leetcode/lintcode上的算法题 如果你对机器学习的算法感兴趣,欢迎共同讨论: https://github.com/zhaozhengcoder/Machine-Learning - - -### Flag - -刷到200题吧~ - -![](1.PNG) \ No newline at end of file diff --git "a/344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円2721円/readme.md" "b/344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円2721円/readme.md" index 18a7a89..d3c42fe 100644 --- "a/344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円2721円/readme.md" +++ "b/344円271円260円345円215円226円350円202円241円347円245円250円347円232円204円346円234円200円344円275円263円346円227円266円346円234円2721円/readme.md" @@ -10,4 +10,6 @@ ``` //维护两个数据结构 //一个记录目前为止的 最小值 minprices ,一个记录目前的最大maxprofit -``` \ No newline at end of file +``` + +这个题的本质和最长子数组是一样的。 diff --git "a/345円277円253円346円216円222円/quicksort.cpp" "b/345円277円253円346円216円222円/quicksort.cpp" new file mode 100644 index 0000000..69ea115 --- /dev/null +++ "b/345円277円253円346円216円222円/quicksort.cpp" @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +int get_partition(vector &vi, int begin, int end) +{ + int partition = vi[end]; + int sorted = begin; + for (int i = begin; i < end; i++) + { + if (partition> vi[i]) + { + swap(vi[i], vi[sorted]); + sorted++; + } + } + swap(vi[sorted], vi[end]); + return sorted; +} + +void quicksort(vector & vi,int begin,int end) +{ + if (begin < end) + { + int index = get_partition(vi, begin, end); + quicksort(vi, begin, index - 1); + quicksort(vi, index + 1, end); + } +} + +void topk(vector & vi,int k) +{ + int begin = 0; + int end = vi.size() - 1; + int index = get_partition(vi, begin, end); + + while (index != k-1) + { + if (index < k-1) + { + begin = index + 1; + end = end; + index = get_partition(nums, begin, end); + } + else + { + begin = begin; + end = index - 1; + index = get_partition(nums, begin, end); + } + } + + cout << vi[index] << endl; +} + +int main() +{ + + vector vi = { 4,12,1,10,5,7,0,3,2,55 }; + + quicksort(vi, 0, vi.size() - 1); + + for (auto i : vi) + { + cout << i << " "; + } + + + topk(vi, 3); + topk(vi, 4); + topk(vi, 5); + return 0; +} diff --git "a/346円225円260円347円273円204円351円207円214円351円235円242円347円254円254円k345円244円247円347円232円204円346円225円260円345円255円227円/kth.cpp" "b/346円225円260円347円273円204円351円207円214円351円235円242円347円254円254円k345円244円247円347円232円204円346円225円260円345円255円227円/kth.cpp" index 6a25399..94c189e 100644 --- "a/346円225円260円347円273円204円351円207円214円351円235円242円347円254円254円k345円244円247円347円232円204円346円225円260円345円255円227円/kth.cpp" +++ "b/346円225円260円347円273円204円351円207円214円351円235円242円347円254円254円k345円244円247円347円232円204円346円225円260円345円255円227円/kth.cpp" @@ -1,115 +1,78 @@ -class Solution{ -public: - //使用快排划分的思想 - //partition 和partiton2 的区别是partition是一个通用的,partition2指定了搜索的区间 - //int partition(vector& nums){ - // int i=0; - // int j=nums.size()-1; - // int part=nums[0]; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; - // while(i=part&&(i &vi, int begin, int end) +{ + int partition = vi[end]; + int sorted = begin; + for (int i = begin; i < end; i++) + { + if (partition> vi[i]) + { + swap(vi[i], vi[sorted]); + sorted++; + } + } + swap(vi[sorted], vi[end]); + return sorted; +} + +void quicksort(vector & vi,int begin,int end) +{ + if (begin < end) + { + int index = get_partition(vi, begin, end); + quicksort(vi, begin, index - 1); + quicksort(vi, index + 1, end); + } +} - // while(nums[i]<=part&&(ipart){ - // swap(nums[i],nums[j]); - // j--; - // } - // } +void topk(vector & vi,int k) +{ + int begin = 0; + int end = vi.size() - 1; + int index = get_partition(vi, begin, end); - // //如果是快排的话,进入下一层的递归 - // //但是,我们这里不是快排 - // return i; - //} + while (index != k - 1) + { + if (index < k - 1) + { + index = get_partition(vi, index + 1, end); + } + else + { + index = get_partition(vi, begin, index-1); + } + } - int partition2(vector& nums,int begin,int end){ - int i=begin; - int j=end; - int part=nums[begin]; + cout << vi[index] << endl; +} - while(i=part&&(ipart){ - swap(nums[i],nums[j]); - j--; - } - } + vector vi = { 4,12,1,10,5,7,0,3,2,55 }; - //如果是快排的话,进入下一层的递归 - //但是,我们这里不是快排 - return i; - } - void kth(int k, vector nums,int begin,int end,int& res){ - //返回的第k大元素的下标,比如第4大的元素的index=3 - int index=partition2(nums,begin,end); - - //index_th表示返回的元素是第几大的 - int index_th=index+1; + quicksort(vi, 0, vi.size() - 1); - if(index_th==k){ - int ret=nums[index]; - res=ret; - } - //要找第七大的元素,结果现在找到了第五大的(index=5),那么就在后面找第(7-5)大的元素 - else if (index_th nums) { - int res=-1; - //比如说第十大的元素,转换成k_index= nums.size()-k+1,k_index=1,相当于转换成第1小的元素 - int k_index=nums.size()-k+1; - kth(k_index,nums,0,nums.size()-1,res); - return res; - } -}; -int main(){ - int arr[]={1,2,3,4,5,6,8,9,10,7}; - const int len=10; - int k_th=10; - //int arr[]={9,3,2,4,8}; - //const int len=5; - //int k_th=3; - vector vi(arr,arr+len); - Solution s; - int ret=s.kthLargestElement(k_th,vi); - cout<

AltStyle によって変換されたページ (->オリジナル) /