
算法
love music.
中山大学计算机博士在读,曾就职于腾讯等公司,目前从事AI多模态大模型研究。
展开
-
从1到n整数中1出现的次数:O(logn)算法
原文链接: https://blog.youkuaiyun.com/yi_Afly/article/details/520125931. 题目描述输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,1一共出现了5次。2. 题目来源第一次看到是在《剑指Offer》第2版上,面试题32。leetcode和牛客网上都有...转载 2018-09-06 22:24:22 · 514 阅读 · 0 评论 -
PAT甲级 1016 Phone Bills (25)
A long-distance telephone company charges its customers by the following rules:Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When...原创 2018-07-25 18:33:51 · 231 阅读 · 0 评论 -
网易游戏面试题 如何判断一棵二叉树是AVL(平衡二叉树)
一棵树是否是AVL,则只要这棵二叉树满足是BST与每个结点的平衡因子都满足在1、0、-1的范围。则符合条件。核心代码如下:template <typename keyType>bool is_AVL(BT<keyType> &bt){ return is_AVL_Core(bt.root);}template <typename keyType>...原创 2018-07-15 11:05:12 · 364 阅读 · 0 评论 -
C 两个递增链表求差集以及两个递增链表去除重复部分
求A-B(仅在A出现而不在B出现,并将结果保存在A):两个链表都是有序的,当p所指结点小于q所指结点的值,p后移一位。当q所指结点小于p所指结点的值,q后移一位。若两结点值相同,则删除p所指结点。核心代码如下://求A与B的交叉集(仅在A中出现而不在B中出现),并将结果保存到链表A void Difference(LNode *A,LNode *B)//时间复杂度O(m+n){ i...原创 2018-07-26 11:03:08 · 669 阅读 · 0 评论 -
C++中引用与指针的区别
参考链接:https://blog.youkuaiyun.com/zhengqijun_/article/details/54980769学过C的朋友应该都知道指针,刚开始学习指针的时候,都会觉得指针很难,学完了指针才发现指针就是保存的地址。指针十分不安全,使用的不恰当,就会使程序出错!C++里面提出了“引用”代替指针,提高程序的安全性。下面来讲讲什么是引用。一、引用的定义引用是给另外一个变量起别...转载 2018-07-21 09:48:18 · 175 阅读 · 0 评论 -
网易面试题 字符串编码
链接:https://www.nowcoder.com/test/question/56a487c342a64d2ea4c3a0b0144b42d0?pid=4111169&tid=17079055时间限制:1秒空间限制:32768K给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。 ...原创 2018-07-29 17:54:34 · 375 阅读 · 0 评论 -
面试题 最大和
链接:https://www.nowcoder.com/question/next?pid=4111169&qid=76265&tid=17079055时间限制:1秒空间限制:32768K在一个N*N的数组中寻找所有横,竖,左上到右下,右上到左下,四种方向的直线连续D个数字的和里面最大的值 输入描述:每个测试输入包含1个测试用例,第一行包括两个整数 N 和 D...原创 2018-07-29 17:58:29 · 178 阅读 · 0 评论 -
面试题 赛马
链接:https://www.nowcoder.com/question/next?pid=4111169&qid=76264&tid=17079055时间限制:1秒空间限制:32768K在一条无限长的跑道上,有N匹马在不同的位置上出发开始赛马。当开始赛马比赛后,所有的马开始以自己的速度一直匀速前进。每匹马的速度都不一样,且全部是同样的均匀随机分布。在比赛中当某匹马追上...原创 2018-07-29 18:15:20 · 1160 阅读 · 0 评论 -
网易2018面试题 魔法币
链接:https://www.nowcoder.com/test/question/32c71b52db52424c89a565e4134bfe4e?pid=6910869&tid=17093182小易准备去魔法王国采购魔法神器,购买魔法神器需要使用魔法币,但是小易现在一枚魔法币都没有,但是小易有两台魔法机器可以通过投入x(x可以为0)个魔法币产生更多的魔法币。魔法机器1:如果投入x...原创 2018-07-30 09:57:54 · 301 阅读 · 0 评论 -
面试题 合并两棵平衡二叉树
首先看我的另一篇文章,如何构建一棵AVL树:C++ 平衡二叉树的创建然后可以将每棵AVL树的结点单独获取,并将结点整合到一个队列,再重新用队列构建出一棵合并后的AVL树。实现代码如下:#include<iostream>#include<algorithm>#include<queue>using namespace std; templ...原创 2018-07-14 16:31:37 · 3181 阅读 · 0 评论 -
C++ int const * p和int * const p区别,const int *p呢
参考链接:点击打开链接 点击打开链接首先int const*p=const int *p;都表示*p的值不能改变,而p(即地址)是可以改变的;而int *const p表示 p(地址)是常量,不可改变,但是*p的值可以改变常量指针是指向常量的指针,指针指向的内存地址的内容是不可修改的。指针常量是指针的常量,它是不可改变地址的指针,但可以对它所指向的内容进行修改。示例代码如下:#include&...原创 2018-07-04 21:15:17 · 3469 阅读 · 2 评论 -
C++ 统计二叉树中所有结子数、叶子节点数
计算所有结点数核心代码:int count_BT(BinaryTreeNode *myBT)//递归求解二叉树所有结点数{ if(myBT == nullptr) return 0; int count = 0; count = 1+ count_BT(myBT->m_pLeft) + count_BT(myBT->m_pRight); return count;...原创 2018-07-05 09:23:13 · 3481 阅读 · 0 评论 -
PAT甲级 1011 World Cup Betting
1011 World Cup Betting (20)(20 分)With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the Wor...原创 2018-07-11 22:25:33 · 120 阅读 · 0 评论 -
PAT甲级 1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.InputEach input file contains one test case. Each case starts with a line contai...原创 2018-07-06 17:48:20 · 178 阅读 · 0 评论 -
C 双链表的构建与增加删除节点
具体实现代码如下:#include<stdio.h>#include<malloc.h>using namespace std;typedef struct DNode{//双链表 int data; struct DNode *prior; struct DNode *next;}DNode;void CreatelistR(DNode *&a...原创 2018-07-24 19:55:07 · 217 阅读 · 0 评论 -
PAT甲级 1013 Battle Over Cities (25) 统计连通图的个数
参考链接:https://www.liuchuo.net/archives/2346It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city ar...原创 2018-07-19 11:20:08 · 177 阅读 · 0 评论 -
C++ 二叉树求最大高度
通过一个递归即可轻松实现,通过h记录访问结点所在的深度,max_height记录所到达的最大深度。核心代码如下:int max_height = -1;void GetBTHeight(BTNode *bt,int h){//获取二叉树的高度 if(bt == nullptr || h < 1 ) return ; if(bt->m_pLeft == nullptr &...原创 2018-07-07 10:06:43 · 2097 阅读 · 0 评论 -
C++ 判断一颗树是否是BST(二叉排序树)
方法一:因为二叉排序树的中序遍历结果是递增的,所以可以通过中序遍历存储结果,再判断是否为递增的数组。代码如下:#include<iostream>#include<algorithm>using namespace std; typedef struct BinaryTreeNode{ int value; BinaryTreeNode *m_pLeft; B...原创 2018-07-07 12:15:41 · 2580 阅读 · 0 评论 -
网易2018面试题 相反数
链接:https://www.nowcoder.com/question/next?pid=6910869&qid=126948&tid=17093182相反数时间限制:1秒空间限制:32768K为了得到一个数的"相反数",我们将这个数的数字顺序颠倒,然后再加上原先的数得到"相反数"。例如,为了得到1325的"相反数",首先我们将该数的数字顺序颠倒,我们得到5231...原创 2018-07-30 10:00:13 · 199 阅读 · 0 评论 -
面试题 字符串碎片
字符串碎片时间限制:1秒空间限制:32768K一个由小写字母组成的字符串可以看成一些同一字母的最大碎片组成的。例如,"aaabbaaac"是由下面碎片组成的:'aaa','bb','c'。牛牛现在给定一个字符串,请你帮助计算这个字符串的所有碎片的平均长度是多少。 输入描述:输入包括一个字符串s,字符串s的长度length(1 ≤ length ≤ 50),s只含小写字母('...原创 2018-07-30 10:02:13 · 441 阅读 · 0 评论 -
C 顺序栈的基本操作
实现代码如下:#include<iostream>using namespace std;const int maxSize = 100;typedef struct{//主要是要有一个数组,和一个top,top空时可用-1代替 int data[maxSize]; int top;}SqStack;void initStack(SqStack &s...原创 2018-08-06 23:54:21 · 974 阅读 · 0 评论 -
网易面试题 一封奇怪的信
链接:https://www.nowcoder.com/test/11647121/summary[编程题] 一封奇怪的信时间限制:1秒空间限制:32768K现在你需要用一台奇怪的打字机书写一封书信。信的每行只能容纳宽度为100的字符,也就是说如果写下某个字符会导致行宽超过100,那么就要另起一行书写信的内容由a-z的26个小写字母构成,而每个字母的宽度均会事先约定。例如字符宽...原创 2018-08-07 11:35:44 · 671 阅读 · 0 评论 -
网易面试题 糖果谜题
[编程题] 糖果谜题时间限制:1秒空间限制:32768K小明是幼儿园的一名老师。某天幼儿园园长给小朋友们每人发一颗糖果,小朋友们拿到后发现有一些同学拿到的糖果颜色和自己相同,有一些同学糖果颜色和自己不同。假定每个小朋友只知道有多少同学和自己拿到了相同颜色的糖果。上课后,有一部分小朋友兴奋的把这一结果告诉小明老师,并让小明老师猜一猜,最少有多少同学拿到了糖果。例如有三个小朋友...原创 2018-08-07 11:41:48 · 1275 阅读 · 0 评论 -
PAT甲级 1019. General Palindromic Number
1019. General Palindromic Number (20)A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single ...原创 2018-08-07 16:23:56 · 187 阅读 · 0 评论 -
PAT甲级 1020. Tree Traversals
1020. Tree Traversals (25)Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order tra...原创 2018-08-07 17:09:24 · 134 阅读 · 0 评论 -
PAT甲级 1021. Deepest Root
1021. Deepest Root (25)A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a hig...原创 2018-08-08 12:25:16 · 209 阅读 · 0 评论 -
PAT甲级 1025. PAT Ranking (25) 排序
1025. PAT Ranking (25)Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several place...原创 2018-08-13 13:56:08 · 296 阅读 · 0 评论 -
KMP算法最浅显理解——一看就明白
原文内容比较完整,链接: https://blog.youkuaiyun.com/starstar1992/article/details/54913261/算法说明一般匹配字符串时,我们从目标字符串str(假设长度为n)的第一个下标选取和ptr长度(长度为m)一样的子字符串进行比较,如果一样,就返回开始处的下标值,不一样,选取str下一个下标,同样选取长度为n的字符串进行比较,直到str的末尾(实际...转载 2018-09-04 11:22:24 · 162 阅读 · 0 评论 -
已知后序与中序输出前序
参考链接:https://www.liuchuo.net/archives/2090 https://blog.youkuaiyun.com/qq_29762941/article/details/80676658已知后序与中序输出前序(先序):后序:3, 4, 2, 6, 5, 1(左右根)中序:3, 2, 4, 1, 6, 5(左根右)分析:通过递归实现。因为后序...原创 2018-08-01 20:57:27 · 587 阅读 · 0 评论 -
PAT甲级 1022 Digital Library (30) map映射,set
1022 Digital Library(30 分)A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigne...原创 2018-08-10 16:39:22 · 198 阅读 · 0 评论 -
C++ 线性表之顺序表的构建
(一)该顺序表下标从一开始,具有增删查等操作实现如下:#include<iostream>using namespace std;const int maxSize = 100;class sqlist{public: sqlist():length(0){} int LocateElem(int x);//查找元素位置 int insert(int ...原创 2018-07-22 10:48:29 · 2177 阅读 · 0 评论 -
PAT甲级 1017 Queueing at Bank (25)
参考链接:https://www.liuchuo.net/archives/2945Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the custome...原创 2018-07-27 09:32:07 · 160 阅读 · 0 评论 -
邻接矩阵的DFS与BFS
代码实现如下:#include<iostream>#include<queue>#include<algorithm>using namespace std;int n,m;const int maxSize = 1010;int v[maxSize][maxSize];bool visited[maxSize];void DFS(i...原创 2018-07-27 11:37:29 · 841 阅读 · 0 评论 -
单链表的算法操作、两个单链表的归并
合并的主要函数与归并排序中的Merge思想一致核心代码如下:void MergeR(LNode *&A,LNode *&B,LNode *&C){//尾插法归并成递增的 if(A == nullptr || B == nullptr) return; LNode *p = A->next; LNode *q = B->next; ...原创 2018-07-22 18:45:20 · 2856 阅读 · 0 评论 -
PAT甲级 1018.Public Bike Management (30)Dijkstra+DFS
参考链接:https://www.liuchuo.net/archives/23731018. Public Bike Management (30)There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. O...原创 2018-07-30 21:19:07 · 165 阅读 · 0 评论 -
PAT甲级 1014 Waiting in Line
参考链接:https://www.liuchuo.net/archives/2943Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for t...原创 2018-07-23 09:27:29 · 158 阅读 · 0 评论 -
C++ 输入一行数字或字符串(未知个数),以空格间格,换行后结束输入并输出
说明:通过getchar()来判断是否是\n来终止输入方法一: 一行中输入多个数字并以空格间隔,通过int数组存储#include<iostream>#include<string.h>#include<stdio.h> using namespace std;int main(){ int arr[100]; int a,index ...原创 2018-08-09 13:24:37 · 35831 阅读 · 0 评论 -
BFS和DFS算法原理(通俗易懂版)
原文链接:https://blog.youkuaiyun.com/u011437229/article/details/53188837DFS 算法思想:一直往深处走,直到找到解或者走不下去为止BFS算法DFS:使用栈保存未被检测的结点,结点按照深度优先的次序被访问并依次被压入栈中,并以相反的次序出栈进行新的检测。BFS:使用队列保存未被检测的结点。结点按照宽度优先的次序被访问...转载 2018-07-28 12:35:05 · 563 阅读 · 0 评论 -
PAT甲级 1015 Reversible Primes
A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a pr...原创 2018-07-23 11:41:58 · 184 阅读 · 0 评论 -
C++ 反转二叉树(镜像)
参考链接:翻转二叉树(C++)对于树这种结构,我们经常采用的策略是使用递归的方式,在这里,我们也使用递归来解决这个问题。递归算法步骤: 1、对二叉树的左子树进行翻转 2、对二叉树的右子树进行翻转 3、将左右子树的根节点进行交换(不用考虑原二叉树的根节点,因为原二叉树的根节点在翻转前后没有改变)核心代码如下:BinaryTreeNode * rever...原创 2018-07-04 11:08:37 · 4075 阅读 · 1 评论