![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构与算法-高级算法
超超级钢铁侠
每天进步一点点
展开
-
图论复习
为了准备面试,复习了图的基础知识,理论知识参考相关书籍即可。邻接矩阵实现的图及DFS和BFS:// GraphTheory.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#include<queue>using namespace std;typedef char VertextType;typedef int Edge原创 2017-03-04 14:24:30 · 314 阅读 · 0 评论 -
两个基本的动态规划问题
1.题目: 有一个XxY的网格,一个机器人只能走格点且只能向右或向下走,要从左上角走到右下角。请设计一个算法,计算机器人有多少种走法。 给定两个正整数int x,int y,请返回机器人的走法数目。保证x+y小于等于12。 测试样例: 2,2 返回:2import java.util.*;public class Robot { public int countWays(int x,原创 2017-03-15 13:39:29 · 661 阅读 · 0 评论 -
选择排序和冒泡排序算法的优化
常规的选择排序和冒泡排序通常都做了许多无用功,在选择排序中,就算元素已经排列好了,for循环还是会执行n-1次,在查找最大元素的时候,可以顺便检查元素是否已经按序排列。代码如下:#include "stdafx.h"#include<iostream>using namespace std;template<class T>void SelectionSort(T a[], int n){原创 2016-03-09 17:28:33 · 554 阅读 · 4 评论 -
Logistic回归算法
原理就不说了,其它博客上说得非常详细,Logistic回归梯度上升算法代码如下:from numpy import *def loadDataSet(): dataMat= [];labelMat= [] fr = open('testSet.txt') for line in fr.readlines(): lineArr = line.strip().sp原创 2016-03-07 19:58:37 · 803 阅读 · 1 评论 -
基础排序算法复习
1.计数排序 计数排序的四个步骤: a.初始化c[0…k]为0; b.对于每个元素a[i], c[ a[i] ]++,c记录a中各个元素出现的次数,比如{1, 1, 3}中,c[1] = 2, c[2] = 0, c[3] = 1; c.对于i=1 to k, c[i] = c[i] + c[i-1],此时c记录小于等于i的元素的个数,c[1] = 2, c[2] = 2, c[3] = 3原创 2016-08-10 18:09:59 · 277 阅读 · 0 评论 -
按层次遍历二叉树
题目:有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树,分层输出。 题目有两个难点,一个是二叉树的分层遍历,一个是换行输出。分层遍历时需要注意使用队列来存储访问过的节点的子节点,换行输出主要是考虑使用标志位来确定是否访问到了某一层的末尾。 《编程之美》上给出的解法:struct TreeNode { int val; struct TreeNode *left; st原创 2016-08-29 09:25:39 · 2896 阅读 · 0 评论 -
平衡二叉树复习
遇见一个BST的问题,然而已经忘得差不多了,参考博文敲一遍,复习一下。。。using namespace std;template<class T>class TreeNode{public: TreeNode() :lson(NULL), rson(NULL), freq(1) {} T data; unsigned int freq; TreeNode* l转载 2016-10-18 19:49:00 · 212 阅读 · 0 评论 -
快速排序复习
void quik_sort(int S[], int L, int R){ if (L < R) { swap(S[L], S[R]); int i = L, j = R, x = S[L]; while (i < j) { while (i < j && S[j] >= x)原创 2016-11-25 17:52:25 · 357 阅读 · 0 评论 -
KMP算法复习
g++ (Ubuntu 4.9.2-10ubuntu13) 4.9.2下编译通过#include<iostream>#include<string.h>using namespace std;int * getnext(char* substr){ unsigned int i = 0; int j = -1; int n = strlen(substr);原创 2016-11-28 01:13:34 · 263 阅读 · 0 评论 -
二叉树前序遍历复习
做一下简单二叉数遍历的题目: Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].递归遍历比较简单,此处略; 非递归遍历:/原创 2016-12-06 13:12:36 · 263 阅读 · 0 评论 -
二叉树中序遍历复习
Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,3,2].Note: Recursive solution is trivia原创 2016-12-07 00:26:35 · 292 阅读 · 0 评论 -
归并排序复习
#include<iostream>using namespace std;void Merge(int A[], int TmpArray[], int Lpos, int Rpos, int RightEnd){ int i, LeftEnd, NumElements, TmpPos; LeftEnd = Rpos - 1; TmpPos = Lpos; Nu原创 2016-11-16 23:33:56 · 266 阅读 · 0 评论