
C/C++
文章平均质量分 62
Shayne_Li
实迷途其未远,觉今是而昨非!
展开
-
LeetCode 283 移动零 O(N) C语言 & Python
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。示例:输入: [0,1,0,3,12]输出: [1,3,12,0,0]说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数...原创 2020-09-01 22:43:59 · 265 阅读 · 0 评论 -
LeetCode 125 验证回文字符串 C语言 & python
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。说明:本题中,我们将空字符串定义为有效的回文串。示例 1:输入: "A man, a plan, a canal: Panama"输出: true示例 2:输入: "race a car"输出: false代码bool isPalindrome(char * s){ if (NULL == s) { return false; } int str_原创 2021-05-20 22:41:22 · 275 阅读 · 1 评论 -
LeetCode 1两数之和 C语言 哈希表O(1)实现
struct hashTable { int key; /*数组元素值*/ int val; /*数组元素对应下标*/ UT_hash_handle hh;};/*Step1:创建哈希表*/struct hashTable* Myhashtable;struct hashTable * find( int ikey){ struct hashTable* tmp; HASH_FIND_INT(Myhashtable, &ikey, tm...原创 2020-11-01 21:06:10 · 1181 阅读 · 3 评论 -
LeetCode 49 字母异位词分组 C语言 哈希表实现
代码来源https://leetcode-cn.com/problems/group-anagrams/solution/cyu-yan-sui-ran-xie-qi-lai-hen-lei-dan-shi-dui-ha-/#define STR_SIZE 100typedef struct Node{ char str[STR_SIZE]; // key为字符串 int row; // value为结果所在的行 struct Node * ne.原创 2020-10-27 22:59:36 · 357 阅读 · 0 评论 -
LeetCode 242 有效的字母异位词 C语言实现 哈希O(N) 排序O(N log N)
bool isAnagram(char * s, char * t){ int a_s[256] = {0}; int s_len = strlen(s), t_len = strlen(t); int i = 0; for(i = 0; i < s_len; i++){ a_s[s[i]]++; } for(i = 0; i < t_len; i++){ a_s[t[i]]--; } for(.原创 2020-10-02 10:51:57 · 210 阅读 · 0 评论 -
LeetCode 239 滑动窗口最大值 C语言 单调递减队列 实现 O(N)
int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize){ *returnSize = numsSize - k + 1; int *res = (int *) malloc( (*returnSize) * sizeof(int) ); int dequeue[numsSize] ; /*单调递减队列*/ int head =0, tail=0; /*head为能使用的队列的队头,.原创 2020-09-20 20:37:24 · 436 阅读 · 0 评论 -
LeetCode 84 柱状图中最大矩形 栈 O(N) 和 暴力O(N^2)
栈O(N)解法如下:int largestRectangleArea(int* heights, int heightsSize){ int top = 0; int max_area = 0; int *stack_height = (int *) malloc(sizeof(int) * (heightsSize+2)); int *stack_index = (int *) malloc(sizeof(int) * (heightsSize+2)); int i =原创 2020-09-19 22:18:15 · 272 阅读 · 0 评论 -
LeetCode 155 最小栈 C语言实现 O(N)
typedef struct { int itop; /*数组元素个数*/ int min_num; /*当前最小的数值*/ int *data_buf_p; /*存储数组元素的栈*/ int *min_data_buf_p; /*存储数组当前最小值得栈*/} MinStack;#define MAXSIZE 1000/** initialize your data structure here. */MinStack* minStackCreate() { .原创 2020-09-19 13:02:32 · 172 阅读 · 0 评论 -
LeetCode 20 括号匹配 C语言 Python O(N)
bool isValid(char * s){ int str_len = strlen(s); if(str_len % 2 == 1){ return false; } char str_stack[str_len]; int top = 0, i = 0; for(i = 0; i < str_len; i++){ /*如果是左括号压栈*/ if(s[i] == '{'){ .原创 2020-09-15 23:33:39 · 188 阅读 · 0 评论 -
makefile简单梳理总结总结
1、使用makefile的原因是因为,当工程很大的时候,有很多文件,这些文件也有可能很分散,若直接用gcc编译则拼写gcc命令很长,很麻烦,很容易出错,所以借助makefile工具将所有编译命令写到文件中帮助gcc编译,避免出错,方便管理。2、makefile的命名只有两种方式makefile和Makefile3、规则中的三要素:目标,依赖,命令。目标:依赖文件 ...原创 2019-11-17 22:32:16 · 301 阅读 · 2 评论 -
C语言实现读写ini配置文件代码示例
/******************main.c*******************/#define _CRT_SECIRE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>#include"CfgFile.h"void menu(){ print...原创 2019-01-31 11:02:12 · 3490 阅读 · 1 评论 -
C编程基础day14
面向对象编程: 封装、继承、多态。1、封装:C++的封装相对于C语言的结构体里面除了变量外还有函数。2、继承:利用以前写过的内容。3、多态:多种形态,调用同一个函数得到不同的结果。 #include<stdio.h>struct Test{ int a; void setA(int x) //C++中类中可以有函数 { ...原创 2018-09-02 16:40:39 · 260 阅读 · 0 评论 -
C编程基础day13
格式化读取提取用sscanf函数。格式化输入用ssprintf函数。 fprintf函数将内容写到文件中,fprintf(fp,"%d\n",num);fscanf函数从文件中读取内容, fscanf(fp,"%d\n",&num); //如果读到的格式不是“%d\n”的格式时候会出错,提前结束读取。 研究以下代码,在fp文件最后有多个换行符的时候为什么没有继续打...原创 2018-08-19 20:47:36 · 240 阅读 · 0 评论 -
C语言实现简单的服务器端和客户端TCP的SOCKET通信(封装错误处理函数),实现小写字母转为大写字母(2)
封装的函数和库函数名字唯一差别就是封装的函数首字母是大写,如库函数socket函数封装的是Socket,这样是为了方便在vi中即便光标在Socket上使用shift + K也能查询到socket函数/****************makefile*****************/src = $(wildcard *.c)obj = $(patsubst %.c, %.o, $(src...转载 2019-04-29 22:00:42 · 445 阅读 · 1 评论 -
txt文件内的多个数字排序, fprintf, fscanf,
#include<stdio.h>#include<time.h>#include<stdlib.h>#define MAX 500void write_file(){ int index = 0, num = 0; FILE *fp; char buf[1024]; fp = fopen("1.txt","w"); ...原创 2018-08-06 19:31:27 · 837 阅读 · 0 评论 -
C语言实现从文件中读取内容 fgetc
#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <time.h>/*为了调用time;*/#include <stdio.h>#include <string.h>#include <windows.h>int main(int argc原创 2018-05-19 19:07:00 · 2681 阅读 · 0 评论 -
调整log的毫秒显示格式
原来的log显示代码修改后的log显示,由于目前只关注时间格式,目前的代码有待完善。#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, char *argv[]){ FILE * fp_r, *fp_w; int len;...原创 2019-09-13 23:09:18 · 836 阅读 · 0 评论 -
进程间通信的7种方式:共享文件、管道、消息队列、共享内存、套接字(附测试代码)
Linux进程间通信几种方式总结面试过程中Linux进程间通信作为基础知识经常会被问道,今天就在这里总结一下。Linux进程间通信常用的有7种,分别是1共享文件、2匿名管道pipe、3有名管道fifo、4消息队列、5共享内存、6信号量、7套接字Socket。现在我们就每种的使用方式和特点简单总结一下。详细代码 https://github.com/Kunpeng1989/IPC 也可关注...原创 2019-08-04 20:14:27 · 3120 阅读 · 0 评论 -
七大排序算法对比冒泡、选择、插入、希尔、归并、快排、堆排(附测试代码)
排序算法作为程序员必备基础技能,在工作和面试中经常会拿来使用或者扩展。今天就针对常使用的冒泡排序、选择排序、插入排序、希尔排序、快速排序、归并排序、堆排序进行简单的分析总结。下图是7种排序算法对10000个随机数的排序时间消耗对比。完整测试代码详见:https://github.com/Kunpeng1989/Sort公众平台见二维码1、冒泡排序:冒泡排序的实现原理和它的名字相...原创 2019-07-07 21:07:01 · 1467 阅读 · 0 评论 -
在#if #else条件编译时候用枚举变量做条件对比易出错,建议#if #else条件编译结合宏#define使用
使用#if(条件1) #else 条件编译的时候,当#if(条件1)中的条件对比内容是枚举成员的时候会出现无论什么情况,#if后边的条件都是真的情况。这可能是由于#if条件起作用是在预编译阶段起作用,而预编译阶段时候枚举变量是占用同一块内存,导致预编译的判断#if条件不同枚举的成员的值相等,所以#if条件恒成立,最终永远进入不到#else分支下。下面举了两个例子,分别说明错误原因,及修改...原创 2019-04-19 22:24:49 · 2984 阅读 · 0 评论 -
C语言实现简单的服务器端和客户端TCP的SOCKET通信,实现小写字母转为大写字母
注意:开启程序的时候应该先开启server程序,再开启client程序。关闭程序的时候应该先关闭client程序,再关闭server程序。/*****************************************服务器端************************************/#include <stdio.h>#include <un...原创 2019-04-27 21:52:01 · 2644 阅读 · 1 评论 -
VS2017下DILL动态库的生成和使用
1、新建——项目——Windows桌面向导2、应用程序类型选择动态链接库(.dill),并勾选空项目步骤3:添加.h头文件和.c函数实现文件, 其中头文件中每个函数的声明墙边都要加上一句__declspec(dllexport)其中头文件内容如下#pragma once//兼容C++编译器#ifdef __cplusplusextern "C"{#endif...原创 2019-03-19 15:08:33 · 928 阅读 · 0 评论 -
哈希表的应用实例 C语言实现
转载:https://blog.youkuaiyun.com/zouchunlaigo1988/article/details/7163920 但原文代码有瑕疵,略作修改。但是仍有不足之处,如姓名fuzongkai和姓名luzhijian及姓名xujinfeng的关键字均为974.#include<stdio.h>#define NAME_NO 30#define HASH_LE...转载 2019-03-03 18:39:30 · 2058 阅读 · 1 评论 -
在txt文件中实现四则运算, fgets, fputs, sprintf, sscanf, feof, strcat, strlen
#include<stdio.h>#include<time.h>#include<stdlib.h>#include<string.h>#define MAX 500void write_file(){ FILE *fp; fp = fopen("1.txt","w"); if(NULL == fp ...原创 2018-08-06 18:35:53 · 350 阅读 · 0 评论 -
在一个txt文件写入500个数字,并读取出来,sprintf sscanf fputs fgets feof
#include<stdio.h>#include<time.h>#include<stdlib.h>#define MAX 500void write_file(){ int index = 0, num = 0; FILE *fp; char buf[1024]; fp = fopen("1.txt","w"); ...原创 2018-08-06 18:00:31 · 710 阅读 · 0 评论 -
C 语言格式化读写fprintf fscanf 将文件中小写字母 转为大写打印在屏幕上
#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <time.h>/*为了调用time;*/#include <stdio.h>#include <string.h>#include <windows.h>int main(int argc原创 2018-05-22 21:06:55 · 599 阅读 · 0 评论 -
C 语言输入n个员工工资在磁盘上,删除某一位员工工资
#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <time.h>/*为了调用time;*/#include <stdio.h>#include <string.h>#include <windows.h>struct emploee{原创 2018-06-02 21:58:12 · 364 阅读 · 0 评论 -
C 语言 在磁盘建立文件输入n个学生的信息,并打印奇数个
#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <time.h>/*为了调用time;*/#include <stdio.h>#include <string.h>#include <windows.h>struct student_ty原创 2018-06-02 21:24:49 · 549 阅读 · 0 评论 -
链表建立、查找、增加、删除 -录入学生成绩举例
typedef struct student{ char name[15]; int mark; struct student *next;}Node, *node;SYS_STATUS main(int argc , char* argv[]){ int num,i; node p, p1, head; head = (node) malloc(sizeof(Node)); if(...原创 2018-05-15 21:57:35 · 1187 阅读 · 1 评论 -
录入电话簿并查找电话簿
#include <string.h>#define MAX 101struct aa{ char name[15]; char tel[11];};int readin(struct aa *a) /*输入电话姓名和号码,返回电话薄数目*/{ int i=0, n=0; while(1) { printf("Input a[%d].name:",i); ...原创 2018-05-15 21:03:49 · 1229 阅读 · 0 评论 -
C 语言成块的读写文件 fwrite fread
// C_Test.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <time.h>/*为了调用time;*/#include <stdio.h>#include <string.h>#include <wi原创 2018-05-20 13:10:58 · 325 阅读 · 0 评论 -
C 语言字符串形式读写文件 fputs、fgets
// C_Test.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <time.h>/*为了调用time;*/#include <stdio.h>#include <string.h>#include <wi原创 2018-05-20 11:54:11 · 385 阅读 · 0 评论 -
C 语言创建文件,写入内容 fputc
#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <time.h>/*为了调用time;*/#include <stdio.h>#include <string>#include <windows.h>int main(int argc ,原创 2018-05-19 18:56:15 · 1987 阅读 · 0 评论 -
冒泡排序, 插入排序,选择排序
/********冒泡排序******/int main(void){ int a[100]; int i = 0, j=0,temp=0; int n=10;/*比较元素个数*/ printf("Input a[10]:\n"); for (i = 0; i < n; i++) scanf("%d ", &a[i]); ...原创 2018-05-13 18:27:21 · 199 阅读 · 0 评论 -
C 实现循环左移和循环右移功能
// C_Test.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <time.h>/*为了调用time;*/#include <stdio.h>#include <string.h>#include <wi原创 2018-05-18 22:00:34 · 5004 阅读 · 0 评论 -
visual studio 2008中头文件和库文件路径设置
在程序开发中,很多时候需要用到别人开发的工具包,如OpenCV和itk。一般而言,在vs2008中,很少使用源文件,大部分是使用对类进行声明的头文件和封装了类的链接库(静态lib或动态dll)。 如果要使用这些类,需要在文件中包含头文件的名字,如#include“cv.h”。但这个并不够,因为这个文件一般来说不在当前目录下,有两种方法解决这个问题。一,将所包含的头文件复制到当前目录(另一篇转载 2017-07-07 22:01:20 · 838 阅读 · 0 评论 -
C/C++中.与->的区别
->是指针指向其成员的运算符.是结构体的成员运算符用结构体定义了一个实体,那么这个实体要引用他里面的成员,就用.操作符,如果你用结构体定义的是一个结构指针,那么要引用他里面的成员就用->原创 2016-11-24 21:08:09 · 357 阅读 · 0 评论 -
VC6.0 头文件与主文件不在同一个文件夹中的解决办法
方法一:#include"绝对路径\头文件.h" //#include"E:\C_Test_L\C_161105\INC\Type_def.h" 方法二:#include"..\Type_def.h" ".." 可读作 "上一级文件夹""./"表示当前目录,"../"表示当前目录的上一级目录。注意!这个当前目录不是工程所在的目录,而是该文件所在的目录原创 2016-11-05 11:56:24 · 2560 阅读 · 0 评论 -
创建 Win32 应用程序 (C++) ——MSDN
https://msdn.microsoft.com/zh-cn/library/bb384843.aspx本演练演示如何创建基于 Win32 的简单基本应用程序,该应用程序在窗口中显示“Hello, World!”。您可以使用在此演练中开发的代码作为创建其他基于 Win32 的应用程序的模式。Win32 API(也称为 Windows API)是用于创建 Wind转载 2016-05-10 08:50:40 · 705 阅读 · 0 评论 -
C语言链表实现电话簿功能
// C_Test.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <stdio.h>#include <string.h>typedef struct Info{ char name[15]; /*姓名...原创 2018-05-16 22:13:23 · 1549 阅读 · 0 评论