
数据结构于算法
VCDI
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
poj 2431 Expedition最优加油方法
思路: 1.让车一直走,走到没油才开始加油. 2.开始加油加最多的油.此问题为贪心问题。代码:#include <iostream>#include <vector>#include <queue>#include <algorithm>#include <cstdio>bool cm...原创 2019-11-11 18:52:55 · 180 阅读 · 0 评论 -
leetcode134 加油站
直接用brute force,注意数组的循环遍历怎么访问,这里用do {} while();每个元素访问两遍,时间复杂度O(N^2),当然可以优化到O(N)。class Solution {public: int canCompleteCircuit(vector<int>& gas, vector<int>& cost) { ...原创 2019-10-27 17:52:20 · 149 阅读 · 0 评论 -
图算法
一、邻接矩阵#include <iostream>using namespace std;#define MAX_VERTS 20class Vertex{public: Vertex(int lab) { label = lab; }private: char label;};class Graph{public: Graph()...原创 2019-10-16 21:28:19 · 182 阅读 · 0 评论 -
排列组合算法思想
题目:将一个字符串进行排列组合。例:"abc" 总共6中排列组合。1. a开头的,后面紧跟bc的组合,2种2. b开头的,后面紧跟ac的组合,2种3. c开头的,后面紧跟ab的组合,2种(1)直接设计算法如下:// array 为原序列,index为下标(以某字符开头的索引), end为原序列最后一个字符的下标(用以上界for遍历)void Permutations(int...原创 2019-09-22 12:20:56 · 557 阅读 · 0 评论 -
查找算法笔记
一、折半查找1.迭代折半查找#include <iostream>using namespace std;// return the position of arrayint Binary_Search(int *a, const int x, const int n){ int left = 0, right = n - 1; while (l...原创 2019-09-22 11:29:08 · 124 阅读 · 0 评论 -
leetcode 48. 旋转图像
画草稿图找规律 1.发现规律,每次旋转都是4个元素为一组,总共 n * n / 4 组。 2.寻找每一组的每一个数字,每一圈都会留下一个数字(因为第一个数会旋转到最后一个数的位置),沿着正对角线往下延伸。设z 为每一圈需要旋转的个数if (z >= n-1 - 2*x) { x++; //x转到下一行 z = 0; ...原创 2019-09-07 18:14:25 · 148 阅读 · 0 评论 -
KMP字符串匹配算法
步骤:(1) 首先根据模式串p的各 模式子串 计算 最长公共子串 (公共部分肯定适配)(2) 根据公共子串求出前缀表,前缀表表示模式串的 上一个模式子串最大公共前后缀数; 根据前缀表,发现失配时要移动的位置(或移动长度)都跟上一个模式子串相关,就干脆把前缀表向后移动一位,0位置值为-1,得出next数组,next数组 表示 失配时 模式串需要匹配的位置。KMP代码:...原创 2019-08-30 11:34:08 · 156 阅读 · 0 评论 -
数据结构算法笔记
一、回溯# 关键代码i = i - j + 1;# i 为主串元素下标, j 为匹配串下标# 回溯导致算法效率低下二、贪心总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解。贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态...原创 2019-08-18 09:46:00 · 164 阅读 · 0 评论 -
经典十大排序
一、冒泡排序#include <iostream>using namespace std;void bubble_sort(int str[], int n);int main() { int str[10] = { 3, 5, 2, 4, 1, 7, 9, 8, 0, 6 }; bubble_sort(str, sizeof(str)/4); for (int ...原创 2019-08-15 16:38:47 · 185 阅读 · 0 评论 -
LeetCode 113.路径总和 II
题目:给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。说明:叶子节点是指没有子节点的节点。示例:给定如下二叉树,以及目标和sum = 22, 5 / \ 4 8 / / \ 11 13 4 / ...原创 2019-08-24 00:09:01 · 159 阅读 · 0 评论 -
可被 K 整除的最小整数
leetcode第129场比赛第二题解析。直接上代码:class Solution {public: int smallestRepunitDivByK(int K) { set<int> s; int c = 1; int rmd = 1%K;//remainder while(!s.count(rm...原创 2019-03-24 17:44:01 · 544 阅读 · 0 评论 -
C++实现hashmap
由于hashmap不是c++ stl中标准实现,这样在跨平台使用时就可能会出现问题,于是想到自己实现一个hashmaphash算法使用开链法解决hash冲突,主要实现了添加,删除,查找几个方法头文件如下hashmap.h#ifndef _HASHMAP_H_#define _HASHMAP_H_template<class Key, class Value>c...转载 2019-03-24 11:20:33 · 1868 阅读 · 0 评论 -
无重复字符的最长子串
这里我们可以建立一个 256 位大小的整型数组,这样做的原因是 ASCII 表一共能表示 256 个字符,所以可以记录所有字符,然后我们需要定义两个变量 res 和 left,其中 res 用来记录最长无重复子串的长度,left 指向该无重复子串左边的起始位置。开始遍历字符串,更新 left,计算最大 res,更新当前字符的最后出现位置。最后得出的 res 即为所求。窗口的右边界就是当前遍历到...原创 2019-03-24 11:10:45 · 147 阅读 · 0 评论 -
双向链表轮转字母表
代码:#include <stdio.h>#include <stdlib.h>#define OK 1#define ERROR 0typedef char ElemType;typedef int Status;typedef struct DualNode{ ElemType data; struct DualNode *prior; ...原创 2018-12-24 20:35:53 · 276 阅读 · 0 评论 -
链表InitList传入双重指针分析
typedef int Element;typedef int Status;typedef struct node{ Element data; struct node *next;}Node, *LinkList;Status InitLink(LinkList *L){ *LinkList = (LinkList)malloc(sizeof(Nod...原创 2018-12-24 16:49:35 · 826 阅读 · 0 评论 -
递归数列
题目描述给定a0,a1,以及an=p*a(n-1) + q*a(n-2)中的p,q。这里n >= 2。 求第k个数对10000的模。输入描述:输入包括5个整数:a0、a1、p、q、k。输出描述:第k个数a(k)对10000的模。示例1输入20 1 1 14 5输出8359代码:#include <iostream>#include <string>using ...原创 2018-03-21 17:42:36 · 1809 阅读 · 2 评论 -
求约数
#include <iostream>#include <string>using namespace std;int main(){ long intSrc[1000]; int N = 0; int count = 0; cin >> N; for (int i = 0; i < N; i++){ cin >> in...原创 2018-03-21 13:45:55 · 482 阅读 · 0 评论 -
数据结构算法之----Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same el...原创 2018-03-12 15:25:01 · 225 阅读 · 0 评论 -
arrayList源码剖析(一)
一。arrayList是一种基于顺序表的stl数据容器;主要数据成员为指向顺序表地址指针:T* element; 如下全部定义:template<class T>class arrayList : public linearList<T>{public: // constructor, copy constructor and destructor arrayL...原创 2018-03-11 17:10:07 · 265 阅读 · 0 评论