
数据结构与算法分析
Shayne_Li
实迷途其未远,觉今是而昨非!
展开
-
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 评论 -
LeetCode 15 3个数之和 C语言 Python O(N^2)实现
int CompareIncrease(void* a, void* b){ return ( *(int *)a - *(int*)b); }int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){ *returnSize = 0; if(0 == numsSize){ return NULL; } int cur = 0; /*相当.原创 2020-09-13 21:33:04 · 205 阅读 · 0 评论 -
LeetCOde 70 爬楼梯 O(N) C语言实现
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?注意:给定 n 是一个正整数。示例 1:输入: 2输出: 2解释: 有两种方法可以爬到楼顶。1. 1 阶 + 1 阶2. 2 阶示例 2:输入: 3输出: 3解释: 有三种方法可以爬到楼顶。1. 1 阶 + 1 阶 + 1 阶2. 1 阶 + 2 阶3. 2 阶 + 1 阶...原创 2020-09-06 19:49:39 · 231 阅读 · 0 评论 -
LeetCode 11 盛水最多的容器 O(N) C语言实现 Python实现
给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。说明:你不能倾斜容器,且 n 的值至少为 2。图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。示例:输入:[1,8,6,2,5,4,8,3,7]输出:49.原创 2020-09-02 23:26:00 · 218 阅读 · 0 评论 -
逆序对/归并排序的应用
给定一个数组,a[0,.....n-1]。对于其中ia[j],则认为存在一个逆序对。可以利用归并排序思想结题#includeusing namespace std;void MergeSort(int *a, int low, int high, int &count);void Merge(int *a, int low, int mid, int h原创 2016-04-25 15:12:12 · 393 阅读 · 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 评论 -
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 评论 -
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 语言格式化读写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语言链表实现电话簿功能
// 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 评论 -
C语言 随机读写文件 fseek函数举例子
fseek(文件指针,long型便宜量, 起始点);其中起始点有3种 ,SEEK_SET表示文件首,数字为0。SEEK_CUR为文件中,数字为1。SEEK_END为文件尾,数字为2。下边举例代码问实现将文件2中的内容复制到文件1内容的后面。#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <...原创 2018-05-24 21:00:12 · 1383 阅读 · 0 评论 -
C 语言 将文件1中制表符转换为空格并生成文件2 ,利用ferror检查文件中错误
int ferror(文件指针) 返回值为0表示没有错误,返回值非0 表示有错// C_Test.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <stdlib.h>/*为了调用system("PAUSE");*/#include <time.h>/*为了调用time;*/#include <stdio.h>#...原创 2018-05-24 22:17:32 · 291 阅读 · 0 评论 -
七大排序算法对比冒泡、选择、插入、希尔、归并、快排、堆排(附测试代码)
排序算法作为程序员必备基础技能,在工作和面试中经常会拿来使用或者扩展。今天就针对常使用的冒泡排序、选择排序、插入排序、希尔排序、快速排序、归并排序、堆排序进行简单的分析总结。下图是7种排序算法对10000个随机数的排序时间消耗对比。完整测试代码详见:https://github.com/Kunpeng1989/Sort公众平台见二维码1、冒泡排序:冒泡排序的实现原理和它的名字相...原创 2019-07-07 21:07:01 · 1467 阅读 · 0 评论 -
录入电话簿并查找电话簿
#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 评论 -
2-Sum问题
bool TwoSum(int* array, int nSize, int nSum, int& a, int& b){ sort(array, array+nSize); int nBegin=0; int nEnd=nSize-1; int nCur; bool bFind= false; while(nBegin {转载 2016-05-05 09:49:11 · 406 阅读 · 0 评论 -
Eratosthenes 法找素数 (含代码)
#includeusing namespace std;void Eratosthenes(bool *a, int n){ a[1]=false;//a[0]不用 int i; for(i=2; i a[i]=true; int p=2; //第一个筛孔 int j=p*p; int c=0; while(j {原创 2016-05-05 09:35:47 · 476 阅读 · 0 评论 -
Gantt图 工作排序
甘特图(Gantt chart)又称为横道图、条状图(Bar chart)。以提出者亨利·L·甘特先生的名字命名。甘特图内在思想简单,即以图示的方式通过活动列表和时间刻度形象地表示出任何特定项目的活动顺序与持续时间。基本是一条线条图,横轴表示时间,纵轴表示活动(项目),线条表示在整个期间上计划和实际的活动完成情况。它直观地表明任务计划在什么时候进行,及实际进展与计划要求的对比。管理者由此可转载 2016-05-04 10:58:02 · 914 阅读 · 0 评论 -
杨氏矩阵的增删改查
杨氏矩阵一左上角斜着往下看可以使小顶堆的树 #include#define INFINITY 100000using namespace std;class CYoungTableau{private: int m_nRow; int m_nCol; int **m_pData;public:CYoungTableau(int原创 2016-04-26 16:14:26 · 674 阅读 · 0 评论 -
归并排序
归并排序,一般先二分进行MergeSort,然后对两半进行Merge;归并排序递归太多,不一定要二分到1才开始Merge,现实应用中一般划分为较小的块后,较小的块内用其他排序。避免大量递归。归并排序用于外排序中,外排序即处理的数据超过内存时,借助外部存储器(硬盘)进行处理。先调入内存能处理的数据,处理后放入外存,再调入.....,最后将外存中的合并。#includeusing原创 2016-04-25 14:57:16 · 375 阅读 · 0 评论 -
并查集/图的划分
某个国家上有N个小岛组成,经过多年的基础设施积累,若干小岛之间建立了桥梁。现做行政区重新划分,规定只要有桥梁相连的的岛屿划为一个城市。问该国一共有多少城市?该问题可以并查集来解决#includeusing namespace std;class CUnionFindSet{ private: int mn_N; //数目int *m_pPare原创 2016-04-25 13:39:48 · 550 阅读 · 0 评论 -
八皇后问题 DFS
八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例。该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。 高斯认为有76种方案。1854年在柏林的象棋杂志上不同的作者发表了40种不同的解,后来有人用图论的方法解出92种结果。计算机发明后,有多种计算机语言可以解决此问题。转载 2016-04-19 17:16:56 · 459 阅读 · 0 评论 -
Word Ladder问题 BFS广度优先遍历
#include#include#include#includeusing namespace std;int ladderLength(string start, string end, set &dict) { if(start.size() == 0 || end.size() == 0) return 0;原创 2016-04-18 21:27:21 · 647 阅读 · 0 评论 -
统计Txt中数字个数 并存入链表
/********************************************************************/*功能:统计txt文件中16进制数字个数。PS最后一个数字后不能有空格,数字之间是空格不是逗号/*日期:160816/*作者:/*结论:/***************************************************原创 2016-08-16 22:47:47 · 480 阅读 · 0 评论 -
统计Txt文件中数字的个数
/********************************************************************/*功能:统计txt文件中16进制数字个数。PS最后一个数字后不能有空格,数字之间是空格不是逗号/*日期:160816/*作者:/*结论:/***************************************************原创 2016-08-16 21:55:16 · 4121 阅读 · 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 评论 -
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 评论 -
汉诺塔 问题C/C++代码师范
#include using namespace std;int m =0; //全局变量记录移动次数int move (char A, int No, char C){ coutreturn 0;}int Hanoi(int n, char A, char B,char C)//将 n个盘子从A街道B移动到C;{原创 2017-07-30 12:31:53 · 722 阅读 · 0 评论 -
求最大公约数,最小公倍数,辗转相除法
#includeusingnamespace std;intmain (){intp,r,n,m,temp;coutcin>>n>>m;if(n{temp=n;n=m;m=temp; //把大数放在n中,小数放在m中}p=n*m; //先将n和m的乘积保存在p中,以便求最小公倍数时用原创 2015-02-09 19:31:22 · 386 阅读 · 0 评论 -
汉诺塔问题——递归问题
#includeusingnamespace std;intmain(){voidhanoi(int n,char one,char two,char three);intm;coutcin>>m;couthanoi(m,'A','B','C');return0;} voidhanoi(int n,char one,char two,char thr原创 2015-02-11 16:45:51 · 479 阅读 · 0 评论