![](https://img-blog.csdnimg.cn/20190927151043371.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
笔试/面试题
经历求职季ing
涛涛酱
北邮研究僧在读
展开
-
【排序算法实现】快速排序、归并排序和堆排序
快速排序int partition_(vector<int>& nums, int left, int right) { int key = nums[left]; int st = left, ed = right; while(st < ed) { while(nums[ed] > key) ed--; if (st < ed) swap(nums[st++], nums[ed]); whil原创 2020-07-12 21:47:25 · 290 阅读 · 0 评论 -
【LeetCode 146】 LRU Cache
题目描述Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) - Get the value (will always be positive) of the key i...原创 2020-02-10 20:30:41 · 108 阅读 · 0 评论 -
【面试题】100GB的文件,内存2G,如何查找出现次数最多的TOPK
思路把大文件拆分成小文件,然后对每个文件求topK,最后求整个的topK。原创 2020-07-12 03:41:22 · 1433 阅读 · 0 评论 -
【面试题】Continuous Subarray Sum 变形
题目描述给你一个数组和一个target,找出和是target整数倍的连续子串。思路要找出所有这样的子串。哈希表的val改成是所有的sum%target=n时,n对应的所有位置。然后每次遇见n时,哈希表中val中的所有left位置和当前位置之间组成合法子串。代码class Solution {public: vector<vector<int>> checkSubarraySum(vector<int>& nums, int k) {原创 2020-07-08 10:03:38 · 205 阅读 · 0 评论 -
【LeetCode 329】Longest Increasing Path in a Matrix
题目描述Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outsi...原创 2020-05-06 22:34:23 · 102 阅读 · 0 评论 -
【面试题 & LeetCode 367】 Valid Perfect Square
题目描述判断一个数是否是平方数。思路平方数可以拆成一个等差数列的和。代码class Solution {public: bool isPerfectSquare(int num) { int cnt = 1; while (num > 0) { num -= cnt; cnt += 2; ...原创 2020-05-06 22:05:41 · 113 阅读 · 0 评论 -
【剑指 offer】复杂链表的复制
题目描述:输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)思路:…不太懂这个不要用引用的要求。疯狂返回空,好像是不用random的复制和引用?代码:struct RandomListNode { int label;...原创 2019-04-19 11:01:13 · 96 阅读 · 0 评论 -
【面试题 & LeetCode 486】Predict the Winner
题目描述一个整数数组,两个人一次分别从左边或者右边拿走一个数,两个人足够聪明,求第一个人拿到数的最大和。动态规划dp[i][j]表示该区间先手能取到的最大和。从小区间到大区间更新。代码class Solution {public: bool PredictTheWinner(vector<int>& nums) { int n = nums....原创 2020-04-23 23:37:26 · 203 阅读 · 0 评论 -
【面试题 & 智力题】给8个小球,7个质量相同,一个重一点,给你一个天平,称几次可以找到重的那一个。
智力堪忧。2次。第一次 3-3 在这六个或者不在这六个里。在的话,1 - 1,可以从三个里找到。不在的话,1-1 剩下的两个也可以找到。原创 2020-04-23 13:01:52 · 3948 阅读 · 0 评论 -
【面试题】实现一个数据结构,要求 set(index, val)、get(index)、setAll(val)三种操作时间复杂度都是O(1)
想了好久。。。代码class Node{private: map<int, pair<int, int> > mp; // index, cnt, val int tot; int cnt = 0;public: void set(int index, int value) { mp[index] = {cnt, va...原创 2020-04-23 11:55:02 · 762 阅读 · 0 评论 -
【面试题】12模式和123模式
12模式给一数组,让你找一对满足i<j && a[i]<a[j]的i和j (O(N)时间复杂度 O(1)空间复杂度)123模式给一数组,让你找一对满足i<j<k && a[i]<a[j]<a[k] 的i和j和k (O(N)时间复杂度 O(N)空间复杂度)思路题一,就可以直接用双指针遍历,维护从前往后和从后往前的...原创 2020-04-23 11:22:48 · 462 阅读 · 1 评论 -
【面试题 & LeetCode 88】 Merge Sorted Array
题目描述Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n respectively.You may assume t...原创 2020-04-22 14:05:42 · 111 阅读 · 0 评论 -
【面试题 & LeetCode 814】Binary Tree Pruning
题目描述We are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1.Return the same tree where every subtree (of the given tree) not containing a 1 has b...原创 2020-04-22 13:36:54 · 123 阅读 · 0 评论 -
【面试题 & LeetCode 22】Generate Parentheses
题目描述Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(()...原创 2020-04-22 12:11:30 · 108 阅读 · 0 评论 -
【面试题 & LeetCode 124】Binary Tree Maximum Path Sum
题目描述Given a non-empty binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child conne...原创 2020-04-22 00:57:21 · 166 阅读 · 0 评论 -
【面试题】实现保留几位小数并对最后一位四舍五入
a = (int)(1000.0 * a + 0.5) / 1000.0amazing…原创 2020-04-22 00:03:07 · 399 阅读 · 0 评论 -
【面试题】约瑟夫环问题
题目描述题目:0,1,…,n-1这n个数字拍成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字。求出这个圆圈里身下的最后一个数字。代码规律法:class Solution {public: int lastRemaining(int n, int m) { if (n == 1) return 0; int pre = 0; ...原创 2020-04-21 21:27:07 · 228 阅读 · 0 评论 -
【面试题 & 并查集】
题目描述n个数,分别是1-n,现把他们分堆并给出两两关系例如2-5 1-5,给出关系的两个数必须在同一堆,问最大分堆数。思路建图dfs或者并查集。代码没有写,先贴一个模板上来。// 1389.cpp : 定义控制台应用程序的入口点。// 并查集,求每个集合中的元素个数// 在合并时将子树中的结点数目加到根结点 #include "stdafx.h"#include <...原创 2020-04-21 20:37:35 · 196 阅读 · 0 评论 -
【面试题 & C++】实现智能指针
代码template<class T>class SmartPointer{public: SmartPointer():m_Pointer(nullptr), m_RefPointer(nullptr) {} SmartPointer(T* target):m_Pointer(target), m_RefPointer(nullptr) { ad...转载 2020-04-21 14:28:06 · 252 阅读 · 0 评论 -
【面试题 & LeetCode 44】Wildcard Matching
题目描述Given an input string (s) and a pattern §, implement wildcard pattern matching with support for ‘?’ and ‘*’.‘?’ Matches any single character.‘*’ Matches any sequence of characters (including th...原创 2020-04-20 22:53:32 · 117 阅读 · 0 评论 -
【面试题 & LeetCode 25】Reverse Nodes in k-Group
题目描述Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number...原创 2020-04-20 21:53:49 · 135 阅读 · 0 评论 -
【面试题 & LeetCode 112】Path Sum
题目描述Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Note: A leaf is a node with no children.Examp...原创 2020-04-20 19:12:22 · 113 阅读 · 0 评论 -
【面试题 & LeetCode 221】Maximal Square
题目描述Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.Example:Input: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Output: 4思路dp[i][j...原创 2020-04-20 18:44:42 · 156 阅读 · 0 评论 -
【面试题 & LeetCode 75】 Sort Colors
题目描述Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use th...原创 2020-04-20 10:55:31 · 168 阅读 · 0 评论 -
【面试题 & LeetCode 148】单链表排序
插入排序/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNo...原创 2020-04-19 22:33:43 · 161 阅读 · 0 评论 -
【面试题 & LeetCode 199】Binary Tree Right Side View
题目描述Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.Example:Input: [1,2,3,null,5,null,4]Output: [1, 3,...原创 2020-04-17 12:56:24 · 109 阅读 · 0 评论 -
【面试题】已知f()函数以概率p输出1,以概率1-P输出0。要求实现g()等概率的输出1和0
思路两次调用该RANDOM函数,如果其概率为P(x),调用2次P(1) = p P(0) = 1-pP'(1) =p P'(0) = 1-p概率如下:11 p*p 10 p*(1-p)01 (1-p)*p 00 (1-p)*(1-p)代码:int Rand(){ int i1 = rand(); int i2 =...转载 2020-04-16 23:06:34 · 734 阅读 · 0 评论 -
【面试题 & LeetCode 54】顺时针旋转矩阵
题目描述Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]...原创 2020-04-16 22:24:35 · 259 阅读 · 1 评论 -
【LeetCode 143】Reorder List
题目描述Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You may not modify the values in the list’s nodes, only nodes itself may be changed.Example 1:Given 1->2...原创 2020-04-15 17:25:05 · 93 阅读 · 0 评论 -
【面试题】kmp 算法
题目描述求字符串p在字符串s中出现的位置。代码#include <iostream>#include <vector>using namespace std;vector<int> getNext(string p) { int n = p.size(), k = -1, j = 0; vector<int> next...转载 2020-04-14 19:01:26 · 233 阅读 · 0 评论 -
【面试题】判断图中是否有环
无向图求出图中所有顶点的度;删除图中所有度≤1的顶点且与该顶点相关的边,把与这些边相关的顶点的度-1;如果还有度≤1的顶点,重复步骤2;如果最后还存在未被删除的顶点,表示有环,否则无环。有向图计算图中所有点的入度,把入度为0的点加入栈;如果栈非空:取出栈顶顶点a,输出该顶点值删除该顶点;从图中删除所有以a为起始点的边,如果删除过程中遇到另一个顶点入度为0,则把它入栈;...转载 2020-04-12 13:19:08 · 665 阅读 · 0 评论 -
【二叉树】递归和非递归方式
先序遍历栈,先入根节点,循环当栈不为空,访问栈顶元素,右子树入栈,左子树入栈。中序遍历栈不为空或者当前节点不为空循环,当前节点不为空,当前节点入栈,到左节点。如果当前节点为空,访问栈顶元素,节点到右节点,继续循环。后序遍历两个栈。根节点入栈1,当栈不为空的时候,访问栈顶元素,入栈2,左子树入栈,右子树入栈。最后依次访问栈2的栈顶元素。https://zhuanlan.zhihu.com...原创 2020-04-12 12:28:16 · 379 阅读 · 0 评论 -
【剑指offer】二叉树问题总结
总结的挺好的,常拿出来看看。。。。https://juejin.im/post/5b8d64346fb9a01a1d4f99fa原创 2020-04-12 11:59:46 · 96 阅读 · 0 评论 -
【LeetCode 215】Kth Largest Element in an Array
题目描述在一个无序数组中找出第k大的元素。思路方法一: 排序,取第k个,O(nlogn)方法二: 最小堆,维护最大k个元素的最小堆,堆顶元素不断和数组中剩余元素比较,如果堆顶元素小于数组元素,替换堆顶,并维护堆。O(nlogk)**方法三:**在快排过程中,不断返回标杆的位置,如果左边区间元素个数>k,那么在左区间继续寻找,否则在右区间寻找 (k-左区间元素个数)。时间复杂度接近 O(n...原创 2020-04-11 17:20:10 · 74 阅读 · 0 评论 -
【LeetCode】2Sum,3Sum,4Sum,kSum,3Sum Closest系列
题目描述给一个数组,分别求出数组中两个数之和为target的集合。三个数之和,四个数之和,三个数之和最接近target的情况。思路2sum : 哈希或者双指针。3sum: 一层循环确定一个数字,然后用2sum方法。4sum: 两层循环+2sum。closet 3sum: 在3sum的基础上,记录每次的diff,以及最接近的和。...原创 2020-04-09 11:13:08 · 147 阅读 · 1 评论 -
【网易笔试题】看电影
题目描述n个同学去看电影,给出k个条件,每个条件是两个人不挨着坐。求出所有满足条件的座位分配。思路DFS。代码#include <iostream>#include <stdio.h>#include <vector>#include <algorithm>#include <map>using namespace s...原创 2020-04-07 22:37:02 · 462 阅读 · 0 评论 -
【网易笔试题】等差数列
题目描述给一个长度为n的数组a,找一个最大的正整数d,使得对于所有的i,满足ai+1 - ai 是 d 的整数倍。2 ≤ n ≤ 10^5,1 ≤ ai ≤ 10^18。思路先求出所有的前后数字的差,求最大公约数。如果出现 ai+1 < ai ,说明不存在。代码#include <iostream>#include <stdio.h>#include ...原创 2020-04-07 22:34:16 · 366 阅读 · 0 评论 -
【腾讯面试题】有36匹马6个跑道,用最少比赛次数算出跑最快的前3匹马
原文链接:https://blog.csdn.net/JackZhang_123/article/details/7942369936匹马分6个组,分别为A、B、C、D、E、F组.第一轮,每个组各跑一次,取每组前三名,标识为A1、A2、A3,B1、B2、B3,以此类推.第二轮,每个组的第一名(A1——F1)拉出来跑一次,假设名次是:A1第一名,B1第二名,C1第三名.则:1.后三名及其所...转载 2020-04-07 22:28:58 · 984 阅读 · 2 评论 -
【面试题】求最长公共子串 or 最长公共子序列
最长公共子串动态规划,设dp[i][j]表示str1的前i个字符和str2的前j个字符的最长公共子串,且子串的最后一位是str1[i]和str2[j]。初始化i0 或者 j0的所有状态。所以,当str1[i] == str2[j] 时,dp[i][j] = dp[i-1][j-1] + 1。否则dp[i][j] = 0;最长公共子序列动态规划,dp[i][j]表示str1的前i个字符和st...原创 2020-04-07 19:19:25 · 153 阅读 · 0 评论 -
【2020 春招实习小米面试题】最大映射
题目描述有 n 个字符串,每个字符串都是由 A-J 的大写字符构成。现在你将每个字符映射为一个 0-9 的数字,不同字符映射为不同的数字。这样每个字符串就可以看做一个整数,唯一的要求是这些整数必须是正整数且它们的字符串不能有前导零。现在问你怎样映射字符才能使得这些字符串表示的整数之和最大?输入描述:每组测试用例仅包含一组数据,每组数据第一行为一个正整数 n , 接下来有 n 行,每行一个长度...原创 2020-03-31 21:47:27 · 354 阅读 · 0 评论