
数据结构和算法
少言才不会咸
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
【Google面试题】有四个线程1、2、3、4同步写入数据…C++11实现
#include <thread>#include <vector>namespace me { class semaphore { public: semaphore(int value = 1) : wakeups{value} {} void wait() { std::unique_lock<std::mutex> lock{mutex}; condition.原创 2020-10-14 11:47:02 · 429 阅读 · 0 评论 -
实现两个线程交替计算合数 or 素数
题目:实现两个线程交替计算1-10000内的合数 or 素数// method 1#include <thread>#include <cmath>#include <atomic>#define Max 10000typedef std::function<uint32_t()> BeginHook;typedef std::function<void(uint32_t, bool)> EndHook;std::ato.原创 2020-10-14 10:18:46 · 214 阅读 · 0 评论 -
递归实现basename命令
#include <iostream>#include <arpa/inet.h>#include <string.h>using namespace std;void xy_log1(const char *path, bool &is_ok){ if (path == nullptr) return; i...原创 2019-12-19 23:23:31 · 227 阅读 · 0 评论 -
字符串排序
#include <stddef.h>#include <stdio.h>int compareString(char *s1, char *s2){ while (*s1 != '\0' && *s2 != '\0') if (*s1 == *s2) { s1++; ...转载 2019-10-16 16:42:30 · 180 阅读 · 0 评论 -
O(n^(1.3—2))排序:希尔
```c#include &amp;amp;lt;iostream&amp;amp;gt;using namespace std;void printArray(int *arr, int size){ for(int i=0; i&amp;amp;lt;size; ++i) { cout&amp;amp;lt;&amp;amp;lt; arr[i]&amp;amp;lt原创 2019-01-18 23:01:00 · 312 阅读 · 0 评论 -
取出无序数组第K大个数(借助快速排序一次划分)
int partition(int array[], int high, int low){ int pivot = array[high]; while (low &amp;amp;amp;lt; high) { while (low &amp;amp;amp;lt; high &amp;amp;amp;amp;&amp;amp;amp;amp; array[low] &amp;amp;am原创 2019-01-18 17:55:35 · 364 阅读 · 0 评论 -
O(n+k)排序:计数
// 特点:只能用于非负数的排序void countingSort(int array[], int size){ // step 1:找出待排序数组中最大的值,确定数据的范围 int max = array[0]; for (int i= 1; i<size ; ++i) { if(max < array[i]) m...原创 2019-01-29 17:20:32 · 273 阅读 · 0 评论 -
二分查找及其变种
#include <thread>#include <vector>#include <iostream>#include <vector>#include <random>#include <functional>#include <algorithm>#include <iterat原创 2019-02-21 17:26:28 · 205 阅读 · 0 评论 -
动态规划:“杨辉三角”
动态规划题目:“杨辉三角”不知道你听说过吗?我们现在对它进行一些改造。每个位置的数字可以随意填写,经过某个数字只能到达下面一层相邻的两个数字。设你站在第一层,往下移动,我们把移动到最底层所经过的所有数字之和,定义为路径的长度。请你编程求出从最高层移动到最底层的最短路径长度。分析:将题目放入矩阵中重新改写题目:从1A开始的node,只能向右、向下移动,求到达边界时【边界:即过(5,...原创 2019-05-09 15:44:40 · 1143 阅读 · 3 评论 -
动态规划:硬币找零
硬币找零问题,我们在贪心算法那一节中讲过一次。我们今天来看一个新的硬币找零问题。假设我们有几种不同币值的硬币 v1,v2,……,vn(单位是元)。如果我们要支付 w 元,求最少需要多少个硬币。比如,我们有 3 种不同的硬币,1 元、3 元、5 元,我们要支付 9 元,最少需要 3 个硬币(3 个 3 元的硬币)。思路分析:非常简单的动态规划表,统计在上一次情况的基础上,加上一枚1\3\5...原创 2019-05-09 16:03:23 · 479 阅读 · 0 评论 -
[重新解答]阿里笔试:去重和排序,重新输出Markdown格式
在网上偶然看到一道题目,发现博主的答案有问题,所以重新解答一下题目原题链接【https://blog.youkuaiyun.com/qq_29108585/article/details/60956567】#include <fstream>#include <iostream>#include <map>#include <set>#inc...原创 2019-05-27 18:04:35 · 301 阅读 · 0 评论 -
链表快速排序
#include <iostream>#include <vector>#include <list>#include <iterator>#include <algorithm>using namespace std;struct LinkNode{ int val; LinkNode *next; ...原创 2019-06-01 16:09:20 · 384 阅读 · 0 评论 -
O(nLogn)排序 :堆
#include <iostream>#include <vector>#include <algorithm>using namespace std;void adjust_up(vector<int> &v, int index){ int parent = 0; int hole = index; ...原创 2019-06-03 10:49:42 · 369 阅读 · 0 评论 -
[重新解答]百度笔试:数组按频次排列
题意为从一个乱序数组中,将其中的整数按照出现的频次多少来排列(并且出现几次就排列几个),比如输入为[1,2,1,2,3,3,1,6,4,4,4,4],那么输出就应该为[4,4,4,4,1,1,1,2,2,3,3,6],其中,如果某两个数字的出现频次相同,那么就按照输入用例中的原顺序排列。#include <iostream>#include <set>#incl...原创 2019-06-04 13:38:40 · 784 阅读 · 0 评论 -
O(nLogn)排序 :快速
int partition(int array[], int high, int low){ int pivot = array[high]; while (low &amp;amp;lt; high) { while (low &amp;amp;lt; high &amp;amp;amp;&amp;amp;amp; array[low] &amp;amp;lt;= pivot ) {原创 2019-01-18 17:42:21 · 350 阅读 · 0 评论 -
O(nLogn)排序 :归并
void merge(int array[], int start, int mid, int end){ int i = start; int j = mid + 1; int k = 0; int *tmp = new int[end-start+1] {0}; while (i&amp;amp;lt;=mid &amp;amp;amp;&amp;amp;amp; j&amp;amp;lt;=en原创 2019-01-17 18:57:28 · 215 阅读 · 0 评论 -
递归完成10进制整数转2进制
#include <iostream>using namespace std;void zhuanhuan (int beichushu){ int shang;int yushu; if(beichushu==0) return ; shang=beichushu/2; yushu=beichushu%2; zhuanhuan(shang);原创 2016-10-23 20:39:09 · 523 阅读 · 0 评论 -
二叉树递归遍历
#include "stdlib.h"#include "stdio.h"#include "string.h"//二叉链表 typedef struct BiTNode{ int data; struct BiTNode *lchild, *rchild; //左孩子 右孩子}BiTNode, *BiTree;//三叉链表typedef struct TriTNode转载 2016-11-16 19:42:13 · 254 阅读 · 0 评论 -
静态链表
#include "stdlib.h"#include "stdio.h"#include "string.h"typedef struct Node{ int data; struct Node *next;}SLIST;SLIST *Creat_SList();int SList_Print(SLIST *pHead);//在结点数值为x的前面插入yint SLis原创 2016-11-02 15:49:52 · 475 阅读 · 0 评论 -
迭代和递归分别实现二分查找
#include <stdio.h>#include <stdlib.h>int bin_search(int * arr,int key,int Count){ int mid; int low = 0; int high = Count ; while(low <= high) { mid = (mid + high)/2;原创 2016-11-25 22:50:08 · 470 阅读 · 0 评论 -
小米面试题 :字符串重组
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ int i,count = 0; char * buf; buf = (char *)malloc(100); memset(buf,0,sizeof(100)); sprintf(buf ,"%s","***H*E**L*L原创 2017-01-21 16:13:53 · 420 阅读 · 0 评论 -
MYSQL两道试题
有表结构如下:CREATE TABLE students(name varchar(20),age int,sex varchar(10),classid int);CREATE TABLE classes(classid int,/*班级ID,与students表中的classid对应*/name varchar(20),/*班级名称*/teacher varchar(20));/转载 2016-12-18 23:01:41 · 370 阅读 · 0 评论 -
FaceBook面试题: QPainter重绘实现圆形进度条
前几天看到一道FaceBook的面试题,本来只是想用C语言实现下算法,后来闲着无聊便用Qt绘制了一下效果. FaceBook原题如下:我用Qt的实现粗略效果,如下算法思路: 用百分比*360度求出旋转偏移角度α,将判断点与圆心连线,用余弦定理求该点偏移β角度,和α比较。如果α>β,说明黑色的范围可以包含此点 . 如果不允许使用反三角函数库[ 即泰勒公式展开cos x = 1-x^2/2!+x^4/原创 2017-01-18 14:05:16 · 1015 阅读 · 0 评论 -
平台大小端 , 结构体大小-面试题
最近在网上看到两道面试题,楼主很困惑,我来解答下.[题目源地址](http://www.tuicool.com/articles/i2Yzey)解答原创 2016-12-13 17:26:18 · 417 阅读 · 0 评论 -
深度优先算法—取特定的全排列
题目:setA= {0,3,4,6,8};setB= {1,2,5,7,9};从集合A和集合B中分别取数字(不能只从一个集合取),共取3个数字。打印所有结果。#include <stdio.h>#include <stdlib.h>#include <string.h>int setA[6] = {-1,0,3,4,6,8};int setB[6] = {-1,1,2,5,7,9};原创 2017-07-23 13:46:34 · 365 阅读 · 0 评论 -
鹅厂一面题目总结
1,将/tmp目录下的.sh结尾的文件所有改为.bash原:find /tmp -name “*.sh” -exec mv {} {}.bash \;改:find /tmp -name “*.sh” -exec rename .sh .bash {} \;2,查询/A目录下大于2G的文件,并拷贝到/B目录下原:find /A +2G -exec cp {} /B \;改:find /A -原创 2017-06-05 16:02:08 · 432 阅读 · 0 评论 -
myconfig of st3 & vs code
# sublime{ "color_scheme": "Packages/Color Scheme - Default/Breakers.tmTheme", "font_size": 13, "ignored_packages": [ ], "auto_complete_commit_on_tab": true, "show_encodi原创 2017-11-16 09:54:54 · 311 阅读 · 0 评论 -
位运算问题
//查看两个字节的不同位int diff(unsigned char a[4][4], unsigned char b[4][4]){ int i, j, k; int count = 0; unsigned char comp = 0x1&lt;&lt;7; for(i = 0; i &lt; 4; i++) for(j = 0; j &lt;...原创 2018-02-27 17:38:55 · 186 阅读 · 0 评论 -
O(n^2)排序 :选择、插入、冒泡
#include &amp;lt;iostream&amp;gt;using namespace std;void printArray(int *arr, int size){ for(int i=0; i&amp;lt;size; ++i) { cout&amp;lt;&amp;lt; arr[i]&amp;lt;&amp;lt; ' '; }原创 2019-01-14 16:40:11 · 169 阅读 · 0 评论 -
不借助第三个变量,交换两个变量值的3种方式
void fun1(){ int a=10,b=100; a=a+b; b=a-b a=a-b; cout<<"a="<<a<<","<<"b="<<b<<endl;}void fun2(){ int a=10,b=100; a=a*b; b=a/b; a=a/b; cout<<"a="<<a<<","<<原创 2016-10-23 20:33:52 · 964 阅读 · 0 评论