
算法
jirryzhang
。
展开
-
两数相加
给定两个非空链表来代表两个非负数,位数按照逆序方式存储,它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。你可以假设除了数字 0 之外,这两个数字都不会以零开头。示例:输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807/** * Definition for sing...原创 2018-03-16 15:07:30 · 369 阅读 · 0 评论 -
商汤科技自动驾驶研究员2019校招笔试第一题-跳台阶(每次可跳1~m级,跳到第n级的跳法总数)
递推公式如下:n<=m时v[n]=2<<(n-2)n>m时v[n]=2*v[n-1]-v[n-1-m]输入n超过10^18,考虑是维护一个供迭代的双向队列做动态规划,更新尾部第i级台阶的跳法数,删除头部第i-m-1级台阶的跳法数,直到算出第n级的解。#include<iostream>#include<deque>u...原创 2018-09-07 21:24:57 · 1946 阅读 · 0 评论 -
字节跳动2019校招第三次笔试题解
1、最长不重复子串#include<iostream>#include<string>using namespace std;int solve(const string &str){ int hash[256]={0}; int start=0; int mstart=0; int mlen=0; int idx...原创 2018-09-09 12:49:31 · 6641 阅读 · 0 评论 -
腾讯2019校招研发卷
第一题给出n,求最小的正整数m 使得LCM(1...n)==LCM(n+1...m)/** * 给出n 求最小的正整数m 使得LCM(1...n)==LCM(n+1...m) * 推结论的题 直接是求小于等于n的素数次方数*2 */#include <iostream>using namespace std;const int MAXN = 1e6+5;lon...原创 2018-09-16 12:41:03 · 1421 阅读 · 0 评论 -
欢聚时代(YY)2019校招-推荐系统开发(C++)A卷编程题解
据说都是剑指offer原题,但没用剑指offer的方法写(因为没怎么背答案)。1、特定字符替换为20%因为输入没有限制为char数组,也没说不能开新数组,所以没有在原有内存空间替换,用string遍历一把做替换,生成一个新的string.#include<iostream>#include<string>using namespace std;stri...原创 2018-09-14 18:05:29 · 1991 阅读 · 0 评论 -
Indeed2019校园招聘第三次笔试
第一题A - Sum of Even NumbersTime limit : 2sec / Memory limit : 1024MBProblem StatementThere are N integers: A1,A2,…,AN. We will make Q changes to these integers. The q-th change is as follows:...原创 2018-09-14 19:42:53 · 1373 阅读 · 4 评论 -
由树节点的父子关系计算树的深度(据说是小米笔试题)
题目描述现在有一棵合法的二叉树,树的节点都是用数字表示,现在给定这棵树上所有的父子关系,求这棵树的高度输入描述:输入的第一行表示节点的个数n(1 ≤ n ≤ 1000,节点的编号为0到n-1)组成,下面是n-1行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号输出描述:输出树的高度,为一个整数示例1输入50 10 21 31 4...原创 2018-09-25 16:51:11 · 2348 阅读 · 0 评论 -
二叉树节点间的最长距离
距离最远的两点必然在以某个节点A为根的子树上,它们间的路径必然经过该子树的根节点A。因此,对二叉树中任意一个节点B为根的子树,计算出经过该子树根节点B的最大距离,则所有最大距离的最大值就是所要求的二叉树的最大距离。而经过树的根节点的最大距离为:左子树的高度+右子树的高度+2(两个子树到跟节点的距离均为1),因而,原问题等同于“计算每个节点的左子树和右子树的高度和,取最大值”。struct T...原创 2018-09-20 12:40:16 · 2506 阅读 · 0 评论 -
链表排序(归并排序法)
ListNode *sortList(ListNode *head) { //链表归并排序 if(head == nullptr || head->next == nullptr) return head; else{ //快慢指针找到中间节点 ListNode *fast = head,*slow = head; ...原创 2018-09-01 00:09:58 · 2743 阅读 · 0 评论 -
华为2019软件类笔试
第一题忘记题目了...复盘不了#include<iostream>#include<string>#include <vector>#include <map>using namespace std;int main(){ cout<<"Hello Alipay!"<<endl;原创 2018-08-10 11:35:11 · 5205 阅读 · 1 评论 -
堆排序-C++版(Java类似)
#include <algorithm>#include <iostream>using namespace std;//小顶堆向下调整,按升序排序或最大的k个值时建大顶堆,按降序排序或计算最大的k个值时建小顶堆。void MinHeapFixDown(int* a, int i, int n){ int j = 2*i+1; int temp = ...原创 2018-05-25 22:57:42 · 221 阅读 · 0 评论 -
删除链表中的重复节点(重复的保留一个)
ListNode* deleteDuplication(ListNode* pHead){ if(pHead==nullptr) return nullptr; auto p=pHead,pn=p->next,pnn=p; while(p!=nullptr&&pn!=nullptr) {...原创 2018-06-03 20:11:24 · 2798 阅读 · 0 评论 -
字典树(Trie树)
字典树——Trie树,又称为前缀树(Prefix Tree)、单词查找树或键树,是一种多叉树结构。上图是一棵 Trie 树,表示了关键字集合{“a”, “to”, “tea”, “ted”, “ten”, “i”, “in”, “inn”} 。从上图可见,Trie树的基本性质为:根节点不包含字符,除根节点外的每一个子节点都包含一个字符。 从根节点到某一个节点,路径上经过的字符连接起来...原创 2018-07-22 13:02:22 · 502 阅读 · 0 评论 -
10进制字符串Add strings和任意进制的Add strings
10进制字符串#include <iostream>#include <string>using namespace std;string addStrings(string num1, string num2) { int len1 = num1.size(), len2 = num2.size(), add = 0;//add用于记录逐位相加...原创 2018-07-30 20:16:27 · 285 阅读 · 0 评论 -
二分查找
int binarySerach(const vector<int>& v,const int& key){ int len=v.size(); int l=0,r=len-1,mid; while (l<=r){ mid=(l+r)/2; if(v[mid]==key) return mid; else if (v[mid]<ke...原创 2018-08-05 20:46:28 · 162 阅读 · 0 评论 -
快排(quickSort)
#include <iostream>using namespace std;void quickSort(int *a,int l,int r){ if(l<r){//用这个条件判断是否退出递归 int i=l,j=r,x=a[l]; while(i<j){ while(i<j&&a[j]>x) --j; ...原创 2018-08-05 21:05:09 · 288 阅读 · 0 评论 -
旋转数组-O(1)空间复杂度,O(n)时间复杂度)
#include <iostream>using namespace std;void myReverse(int nums[], int start, int end) { while (start < end) { int tmp = nums[start]; nums[start++] = nums[end]; nums[end--] = tmp; ...原创 2018-08-05 21:22:02 · 1945 阅读 · 0 评论 -
网易2019游戏研发工程师笔试题
第一题:计算个人所得税#include <iostream>using namespace std;int main(){ int t,n,result; float tmp; const float g=0.5000001; while(cin>>t){ for(auto i=0;i<t;++i){ ...原创 2018-08-10 11:16:13 · 6048 阅读 · 3 评论 -
二叉树反转(转换为原来的镜像二叉树)
struct TreeNode{ TreeNode(int _val):val(_val){} TreeNode *left; TreeNode *right;private: int val;}void reverse(TreeNode *root){ if(!root) return; queue<TreeNo...原创 2018-10-26 10:30:05 · 1292 阅读 · 0 评论