数据结构和算法
huizhang0110
厚积薄发
展开
-
数组环形扫描
在每次扫描结束的时候,更新当前矩形的边际即可#include <stdio.h>#include <stdlib.h>#include <assert.h>int * newIntRaw(int n){ return (int *)malloc(sizeof(int) * n);}int ** newMap(int n, int m){ int i; int **原创 2017-09-15 19:08:51 · 500 阅读 · 0 评论 -
卡丹尔算法(max subarray problem)
问题介绍 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of th原创 2017-09-15 21:49:17 · 1438 阅读 · 0 评论 -
素数的运用1
题目Initially on a notepad only one character ‘A’ is present. You can perform two operations on this notepad for each step: Copy All: You can copy all the characters present on the notepad (partial c原创 2017-09-16 00:41:01 · 735 阅读 · 0 评论 -
多次进行dfs没有对辅助数据结构进行重新赋初始值
在图进行多次dfs的时候,在下次调用的时候辅助数据结构中的数据需要重新进行初始化,否则将会出现难以发现的Bug。如下代码中对创建的图进行了多次的dfs。 Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (f原创 2017-09-16 14:35:24 · 304 阅读 · 0 评论 -
深度优先遍历找出一个无向图中的环
进行深度优先遍历的时候,当考察的点的下一个邻接点是已经被遍历的点,并且不是自己之前的父亲节点的时候,我们就找到了一条逆向边,因此可以判断该无向图中存在环路。visited数组记录了节点的访问状态,visited[i] = 0表示节点i尚未被访问过; visited[i] = 1表示节点i被访问了,但是尚未被检测完毕; visited[i] = 2表示节点i已经被检测完毕了,对于检测完毕了的节点,其所原创 2017-09-16 17:05:46 · 9838 阅读 · 2 评论 -
使用DFS进行拓扑排序,如果可以完成的话,输出路径
进行深度优先搜索的时候,如果当前访问的点是已经被访问过的节点的话,说明出现了逆边。因此有环,无法完成拓扑排序。如果可以完成拓扑排序,检测点序列就是一个合法的拓扑排序!#include <iostream>#include <vector>using namespace std;class Solution { vector<vector<int> > graph; vector<原创 2017-09-16 17:34:17 · 800 阅读 · 0 评论 -
在一个二维平面上找到离固定点最近的k个点位置
问题描述/* * 给定一些 points 和一个 origin, 从points中找出k个离origin最近的点,按照距离从小到大返回 * 如果有两个点有相同距离,按照x值进行排序,如果x值相等,按照y值进行排序 * * 如果是静态数据,数据量不是特别多的话,直接做个排序就可以了 * 如果是动态数据的话,维护一个最小堆 */代码#include <iostream>#inc原创 2017-10-23 18:19:07 · 6650 阅读 · 0 评论 -
数串
1. 题目描述 设有n个正整数,将他们连接成一排,组成一个最大的多位整数。 如:n=3时,3个整数13,312,343,连成的最大整数为34331213。 如:n=4时,4个整数7,13,4,246连接成的最大整数为7424613。 2. 输入描述 有多组测试样例,每组测试样例包含两行,第一行为一个整数N(N<=100),第二行包含N个数(每个数不超过1000,空格分开)。 3. 输出描原创 2017-10-31 18:08:22 · 412 阅读 · 0 评论