算法
Lin_QC
这个作者很懒,什么都没留下…
展开
-
京东2018笔试题 神奇数
题目东东在一本古籍上看到有一种神奇数,如果能够将一个数的数字分成两组,其中一组数字的和等于另一组数字的和,我们就将这个数称为神奇数。例如242就是一个神奇数,我们能够将这个数的数字分成两组,分别是{2,2}以及{4},而且这两组数的和都是4.东东现在需要统计给定区间中有多少个神奇数,即给定区间[l, r],统计这个区间中有多少个神奇数,请你来帮助他。输入描述:输入包括一行,一行中两个整数l和r(1 ≤ l, r ≤ 10^9, 0 ≤ r - l ≤ 10^6),以空格分割输出描述:转载 2020-09-03 14:36:46 · 572 阅读 · 0 评论 -
腾讯8.23号笔试 刷木板题 DP
作者:〢ヽ夜╰︶ ̄太美链接:https://www.nowcoder.com/discuss/486642?type=2来源:牛客网题意有n(n在5000内)块木板,宽度是1,长度不固定,这些小木板拼接起来一块大木板。给一个宽度为1的刷子,每刷一次可以选择横着刷和竖着刷,过程中都不能离开木板。问最少要刷几次能把木板完全刷一遍。做法动态规划题,dp[i][j]dp[i][j]dp[i][j]表示当前完全刷完了前iii块木板,横着刷的部分能延伸到之后木板的部分高度为jjj的最小代.转载 2020-08-30 19:40:54 · 724 阅读 · 0 评论 -
面试经典题 手撸LRU
#include <iostream>#include <unordered_map>#include <list>#include <utility>using namespace std;using namespace stdext;class LRUCache {public: LRUCache(int capacity) { m_capacity = capacity; } int get(i.原创 2020-08-14 20:27:21 · 1768 阅读 · 0 评论 -
记面试遇到的一个智力题:追击问题
一个带环的单链表,一个快指针(每次走三步),一个慢指针(每次走一步),请问这两个指针可能无法相遇吗?解:假设慢指针入环时,快指针与慢指针的距离为L,环中共有n个节点,之后慢指针走了m步解1:那么两个指针重合的条件是: (线性同余方程)m % n =(3m+L) % n也就是 m = 3m +L(mod n)2m=n-L(mod n)所以有解的条件是gcd(2,n)能被n-L整除,所以为了无解,只能是n为偶数,n-l为奇数也就是 环的...原创 2020-08-11 17:59:51 · 257 阅读 · 0 评论 -
记 7.24 阿里巴巴机试题
题一题目:吃烧饼大赛。有n个盘子,每个盘子内有s[i]个烧饼。每次选取一个 x(1≤x≤n),可以吃到1 ~ x 号盘子里的一个烧饼。若这1~x个盘子中有空盘时无法进行该操作。假设小明的食量是无限大,最多可以吃掉多少烧饼。其实这题主要是n和s[i]的范围都很大,忘记用long类型了(用int会不够用),所以只ac了一部分。思路:代码:题二题目:开关灯。N行L列的灯,有L个开关,第i个开关Li可以控制第i列,打开该开关使得该列灯状态反转...原创 2020-07-26 14:51:24 · 719 阅读 · 0 评论 -
LeetCode 查并集系列 朋友圈 冗余链接等
网上有作者已经总结的很好 ,这里转载一下:https://www.jianshu.com/p/b81f6db6beaf什么是并查集一种数据结构,用来描述集合。查(find):某个元素是否属于某个集合 并(Combine):某个元素和另一个元素属于同一个集合基本的场景:假设用10个人,用大小为10的数组来表示,a[0] ~ a[9],数据的内容是下标这十个人中间有互相认识的,互相认识的需要分成一组比如 5,6 认识,5和6成为一组,这时a[5]的值变成了6,表示5,6已经是.转载 2020-07-24 11:23:26 · 410 阅读 · 0 评论 -
Leetcode 105 106 重构二叉树
Leetcode上105,106题很相似,都是重构二叉树的题题目:105:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, givenpreorder =[3,9,20,15,7]inorder = [9,3,15,20,7]Re.原创 2020-07-11 10:46:26 · 267 阅读 · 0 评论 -
Leetcode 210. Course Schedule II (利用拓扑排序)
一、题目There are a total ofncourses you have to take, labeled from0ton-1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:[0,1]Given the total number of courses and a l...原创 2020-06-02 23:43:33 · 270 阅读 · 0 评论 -
Leetcode 212. Word Search II (tries树 + DFS)
题目:Given a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are tho...原创 2020-04-26 16:42:08 · 317 阅读 · 0 评论 -
微软笔试题 回忆(回文方面)
这道题当年我没有做出来,主要还是对动态规划掌握的不够熟练。题目: 最少射击几次N个瓶子都有编号,每次能射击1个或多个瓶子,如果是回文的就能一次性击倒。最少几次能全击倒?测试输入:[1, 2]输出:2输入:[1,3,4,1,5]输出:3说明:第一次先射3,变成[1,3,1,5],因为有[1,3,1]回文可以一次击倒,剩下[5]再一次解题思...原创 2020-03-25 23:47:18 · 2106 阅读 · 0 评论 -
c++ map需要使用多个键值如何编写
最近完成项目遇到一个问题 map需要设计多个键值,也就是key-value的key希望是一个结构体,里面有好几个元素:网上查阅了资料(主要参考:https://blog.csdn.net/u010636181/article/details/100533726),解决方案如下:主要就是需要设置一下 < 比较符的定义,因为只有一个键值的时候是很好比较的,但使用结构体后,我们就需要考虑比...原创 2020-02-08 11:57:57 · 9519 阅读 · 0 评论 -
Leetcode 62. Unique Paths (动态规划)
题目:A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach t...原创 2020-01-19 17:03:02 · 146 阅读 · 0 评论 -
Leetcode 57. Insert Interval
题目:Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times....原创 2020-01-06 17:25:02 · 143 阅读 · 0 评论 -
Leetcode 54. Spiral Matrix
题目:Given a matrix ofmxnelements (mrows,ncolumns), 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...原创 2019-12-31 15:08:06 · 159 阅读 · 0 评论 -
Leetcode 44. Wildcard Matching (动态规划解法)
题目:Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (includin...原创 2019-12-30 11:13:03 · 199 阅读 · 0 评论 -
Leetcode 33. Search in Rotated Sorted Array 二分查找法
题目:Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e.,[0,1,2,4,5,6,7]might become[4,5,6,7,0,1,2]).You are given a target value to search. If f...原创 2019-12-03 09:53:09 · 152 阅读 · 0 评论 -
Leetcode 41. First Missing Positive
题目:Given an unsorted integer array, find the smallest missingpositive integer.Example 1:Input: [1,2,0]Output: 3Example 2:Input: [3,4,-1,1]Output: 2Example 3:Input: [7,8,9,11,1...原创 2019-12-02 11:23:57 · 159 阅读 · 0 评论 -
C++ multimap 迭代器 查找与删除 指定元素的 正确姿势
对于multimap,它是一个可以拥有重复key值的数据结构,当进行相应地查找时,需要使用迭代器来存放寻找到的key对应的所有value,同时可以进行相应的删除。在删除时,很容易出现一些错误 (之前写项目遇到了这方面的坑),现在来介绍删除的正确方式。当然 代码中也将 map的建立,遍历加入了进来下面直接上代码://// main.cpp//// Created by Q...原创 2019-11-27 11:25:27 · 7412 阅读 · 0 评论 -
Leetcode 32. Longest Valid Parentheses 使用栈
题目Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.Example 1:Input: "(()"Output: 2Explanation: The longest v...原创 2019-11-19 10:45:21 · 154 阅读 · 0 评论 -
Leetcode 49. Group Anagrams
题目:Given an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat","tan"], ["bat"]]Note:All inputs...原创 2019-11-14 10:22:51 · 104 阅读 · 0 评论 -
Leetcode 47. Permutations II 回溯法
Permutations I 相对来说会好做很多,因为题目中默认给的是不重复(distinct)的数组题目:Given a collection of numbers that might contain duplicates, return all possible unique permutations.Example:Input: [1,1,2]Output:[...原创 2019-10-29 11:33:06 · 172 阅读 · 0 评论 -
Leetcode 39. Combination Sum 回溯法
题目:Given asetof candidate numbers (candidates)(without duplicates)and a target number (target), find all unique combinations incandidateswhere the candidate numbers sums totarget.Thesamer...原创 2019-10-18 17:11:16 · 184 阅读 · 0 评论 -
Leetcode 22. Generate Parentheses
题目:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:[ "((()))", "(()())", "(())()", "()...原创 2019-09-16 10:29:02 · 106 阅读 · 0 评论 -
Leetcode 17. Letter Combinations of a Phone Number
题目:Given a string containing digits from2-9inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) i...原创 2019-09-05 10:47:21 · 114 阅读 · 0 评论 -
opencv 简单的人脸识别
对于opencv 它提供了许多已经练习好的模型可供使用,我们需要通过他们来进行人脸识别参考了网上许多资料假设你已经配好了开发环境 ,在我之前的博客中由开发环境的配置。项目代码结构:dataSet : 存储训练用的图片,他由data_gen生成,当然也可以修改代码由其他方式生成haarcascade_frontalface_alt.xml 、 haarcascade...原创 2019-07-26 11:16:52 · 11983 阅读 · 0 评论 -
Leetcode 52 N-Queens II N皇后问题
题目:Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return the number ofdistinct solutions to then-queen...原创 2019-03-31 16:07:42 · 1071 阅读 · 0 评论 -
Leetcode 34. Find First and Last Position of Element in Sorted Array
题目:Given an array of integersnumssorted in ascending order, find the starting and ending position of a giventargetvalue.Your algorithm's runtime complexity must be in the order ofO(logn).I...原创 2019-09-20 11:20:47 · 136 阅读 · 0 评论 -
Leetcode 23. Merge k Sorted Lists
题目:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.Example:Input:[ 1->4->5, 1->3->4, 2->6]Output: 1->1->2->3-&...原创 2019-09-23 10:43:00 · 117 阅读 · 0 评论 -
Leetcode 25. Reverse Nodes in k-Group 非递归解法
题目:Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.kis a positive integer and is less than or equal to the length of the linked list. If the numbe...原创 2019-09-29 16:06:37 · 191 阅读 · 2 评论 -
Leetcode 31. Next Permutation 记一道一开始题目都没看懂的题
上题目:Implementnext permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest p...原创 2019-09-30 15:58:11 · 241 阅读 · 0 评论 -
Leetcode 5. Longest Palindromic Substring
题目:Given a strings, find the longest palindromic substring ins. You may assume that the maximum length ofsis 1000.Example 1:Input: "babad"Output: "bab"Note: "aba" is also a valid answer....原创 2019-07-22 15:28:32 · 840 阅读 · 0 评论 -
Leetcode 200. Number of Islands
题目: 该题有在google面试中出现Given a 2d grid map of'1's (land) and'0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or verti...原创 2019-07-26 16:52:37 · 563 阅读 · 0 评论 -
Leetcode 11. Container With Most Water
题目:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Fin...原创 2019-07-24 16:35:50 · 660 阅读 · 0 评论 -
Leetcode 21. Merge Two Sorted Lists
题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Outpu...原创 2019-04-17 15:15:46 · 156 阅读 · 0 评论 -
写机试题不要用静态全局变量
之前写一个机试题,用的C++,有一个递归,想用一个全局变量记录一个总和,写的是 static int sum=0造成每次过完一个用例,第二个都会报错,原因就是机试题的测试用例基本都是一个程序连续调用的,例如:fun(参数,参数、、、) //用例一fun(参数,参数,、、)//用例二、、、、所以当跑完第一个用例时,sum变为了4,执行第二个用例,sum就不是从0开始,而是从4开始...原创 2019-04-16 09:52:51 · 525 阅读 · 0 评论 -
leetcode 146 LRU Cache
Design and implement a data structure forLeast Recently Used (LRU) cache. It should support the following operations:getandput.get(key)- Get the value (will always be positive) of the key if th...原创 2019-04-16 09:53:22 · 293 阅读 · 1 评论 -
leetcode48 Rotate Image 旋转图片
题目:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the imagein-place, which means you have to modify the input 2D mat...原创 2019-03-28 11:58:39 · 371 阅读 · 0 评论 -
Leetcode 78 subset 求子集
题目如下:Given a set ofdistinctintegers,nums, return all possible subsets (the power set).Note:The solution set must not contain duplicate subsets.Example:Input: nums = [1,2,3]Output:[ [3...原创 2019-04-10 17:01:33 · 583 阅读 · 0 评论 -
leetcode42: Trapping Rain Water
题目:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.The above elevation map is represented by ...原创 2019-03-18 17:09:00 · 715 阅读 · 0 评论 -
Leetcode 50 pow(x,n)
这就是一题实现求解高次方的题目一开始想到的是简单的逐个相乘,例如2^4=2*2*2*2.class Solution { public double myPow(double x, int n) { double sum=1; if(n==0) return 1; for(int i=1;i<=Math.abs(n);i+...原创 2019-03-11 18:22:31 · 450 阅读 · 0 评论