careercup-高等难度 18.1】的更多相关文章

18.9 随机生成一些数字并传入某个方法.编写一个程序,每当收到新字符数字时,找出并记录中位数. 类似:设计一个数据结构,包括两个函数,插入数据和获得中位数 解法: 一种解法是使用两个优先级堆:一个大根堆,存放小于中位数的值,以及一个小根堆存放大于中位数的值.这会将所有元素大致分为两半,中间的两个元素位于两个堆的堆顶.这样一来,要找到中位数就是小事一桩. 不过,“大致分为两半”又是什么意思呢?“大致”的意思是,如果有奇数个值,其中一个堆就会多一个值.经观察可知,以下两点为真. 如果maxHeap…
18.7 给定一组单词,编写一个程序,找出其中的最长单词,且该单词由这组单词中的其他单词组合而成. 解法: 原题 给定字符串,以及一个字典,判断字符串是否能够拆分为字段中的单词.例如,字段为{hello,world},字符串为hellohelloworld,则可以拆分为hello,hello,world,都是字典中的单词. 分析 这个题目唤作“分词问题”,略显宽泛.只是想提及这个问题,这是在自然语言处理,搜索引擎等等领域中,非常基础的一个问题,解决的方法也比较多,相对比较成熟,不过这仍旧是一个值…
18.6 设计一个算法,给定10亿个数字,找出最小的100万个数字.假定计算机内存足以容纳全部10亿个数字. 解法: 方法1:排序 按升序排序所有的元素,然后取出前100万个数,时间复杂度为O(nlog(n)) 方法2:大顶堆 我们可以使用大顶堆来解题.首先,为前100万个数字创建一个大顶堆 然后,遍历整个数列,将每个元素插入大顶堆,并删除最大的元素. 遍历结束后,我们将得到一个堆,刚好包含最小的100万个数字.这个算法的时间复杂度为O(nlog(m)),其中m为待查找数值的数量. 方法3:选择…
18.5 有个内含单词的超大文本文件,给定任意两个单词,找出在这个文件中这两个单词的最短距离(也即相隔几个单词).有办法在O(1)时间里完成搜索操作吗?解法的空间复杂度如何? 解法1:我们假设单词word1和word2谁在前谁在后无关紧要.要解决此题,我们需要遍历一次这个文件.在遍历期间,我们会记下最后看见word1和word2的地方,并把它们的位置存入lastPosWord1和lastPosWord2中.碰到word1时,就拿他跟lastPosWord2比较,如有必要则更新min,然后更新la…
18.2 编写一个方法,洗一副牌.要求做到完美洗牌,换言之,这幅牌52!种排列组合出现的概率相同.假设给定一个完美的随机发生器. 解法:假定有个数组,含有n个元素,类似如下: [1][2][3][4][5] 利用简单构造法,我们不妨先问自己,假定有个方法shuffle(...)对n-1个元素有效,我们可以用它来打乱n个元素的次序吗?当然可以,而且非常容易实现.我们会先打乱前n-1个元素的次序,然后,取出第n个元素,将它和数组中的元素随机交换.就这么简单!递归解法的算法如下: //lower和hi…
18.1  编写一个函数,将两个数字相加,不得使用+或其他算术运算符. int add(int a,int b) { ) return a; int sum=a^b; ; return add(sum,carry); }…
算法面试过程中,题目类型多,数量大.大家都不可避免的会在LeetCode上进行训练.但问题是,题目杂,而且已经超过1300道题. 全部刷完且掌握,不是一件容易的事情.那我们应该怎么办呢?找规律,总结才是制胜法宝. 下面我们就来看看 Grokking the Coding Interview: Patterns for Coding Questions​ 的分类及每个类型的经典题目: 1. Pattern: Sliding window,滑动窗口类型 经典题目: Maximum Sum Subar…
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我们实现两数相加,但是不能用加号或者其他什么数学运算符号,那么我们只能回归计算机运算的本质,位操作Bit Manipulation,我们在做加法运算的时候,每位相加之后可能会有进位Carry产生,然后在下一位计算时需要加上进位一起运算,那么我们能不能将两部分拆开呢,我们来看一个例子759+674 1.…
18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with the largest possible sum. 这道求和最大的子矩阵,跟LeetCode上的Maximum Size Subarray Sum Equals k和Maximum Subarray很类似.这道题不建议使用brute force的方法,因为实在是不高效,我们需要借鉴上面LeetCode…
18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels. LeetCode上的原题,请参见我之前的解法Maximal Square.书上给了两种解法,但是比较长:…
18.10 Given two words of equal length that are in a dictionary, write a method to transform one word into another word by changing only one letter at a time. The new word you get in each step must be in the dictionary. 这道题让我们将一个单词转换成另一个单词,每次只能改变一个字母,…
18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated. LeetCode上的原题,请参见我之前的博客Find Median from Data Stream. 解法一: priority_queue<int> small; priority_queue<int,…
18.8 Given a string s and an array of smaller strings T, design a method to search s for each small string in T. 这道题给我们一个字符串s,和一个字符串数组T,让我们找T中的每一个小字符串在s中出现的位置,这道题很适合用后缀树Suffix Tree来做,LeetCode中有几道关于前缀树(Prefix Tree, Trie)的题,Implement Trie (Prefix Tree)…
18.6 Describe an algorithm to find the smallest one million numbers in one billion numbers. Assume that the computer memory can hold all one billion numbers. 这道题让我们在十亿个数字中找到最小的一百万个数字,而且限定了计算机只有能存十亿个数字的内存.这题有三种解法,排序,最小堆,和选择排序. 首先来看排序方法,这种方法简单明了,就是把这十亿…
18.5 You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. If the operation will be repeated many times for the same file (but different pairs of words), c…
18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可.LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处.那么首先这题可以用brute force来解,我们对区间内的每一个数字…
18.3 Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen. 这道题让我们从一个数组中随机取出m个数字,要求每个数字被取出的概率相同,其实这道题用的是之前那道18.2 Shuffle Cards的方法,同样我们可以用递归和迭代两种方法来做,递归的思路还用的回溯法,回溯到i+…
18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of the 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect. 这道题让我们实现一个洗牌的算法,实际上洗…
18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangle of letters such that every row forms a word (reading left to right) and every column forms a word (reading top to bottom). The words need not be cho…
5.7 Given a list of words, write a program to find the longest word made of other words in the list. 这道题给了我们一个字符串数组,让我们找到最长的那个单词是由字符串数组中的其他单词组成的,LeetCode上跟类似的题目有Word Break和Word Break II.那么我们首先来想如果是拆分两个单词怎么做,那我们要首先把所有的单词存到哈希表里,然后遍历每个单词,每个位置上都拆分成左右两个字符…
中介者模式(Mediator Pattern):用一个中介对象(中介者)来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互,中介者模式又称为调停者模式. 模式角色与结构: 示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharp.DesignPattern.MediatorPat…
前言 MATLAB一向是理工科学生的必备神器,但随着中美贸易冲突的一再升级,禁售与禁用的阴云也持续笼罩在高等学院的头顶.也许我们都应当考虑更多的途径,来辅助我们的学习和研究工作. 虽然PYTHON和众多模块也属于美国技术的范围,但开源软件的自由度毕竟不是商业软件可比拟的. 本文是一篇入门性文章,以麻省理工学院(MIT) 18.06版本线性代数课程为例,按照学习顺序介绍PYTHON在代数运算中的基本应用. 介绍PYTHON代数计算的文章非常多,但通常都是按照模块作为划分顺序,在实际应用中仍然有较多…
17.6 Given an array of integers, write a method to find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minimize n - m (that is, find the smallest such sequence). 为了更好的理解题意,我们通过一个例子来分析,比如我们有如下的数组: 1, 2,…
1 Lagrange---78岁 约瑟夫·拉格朗日, 全名约瑟夫·路易斯·拉格朗日 (Joseph-Louis Lagrange 1735~1813) 法国数学家.物理学家. 1736年1月25日生于意大利都灵,  1813年4月10日卒于巴黎. 他在数学.力学和天文学三个学科领域中都有历史性的贡献,  其中尤以数学方面的成就最为突出. 1.1 生平 拉格朗日1736年1月25日生于意大利西北部的都灵. 父亲 约瑟夫·拉格朗日是法国陆军骑兵里的一名军官, 后由于经商破产, 家道中落. 据拉格朗日…
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealing with redundant data? 题目:如果你有大量数据流入,如何处理冗余数据? 解法:又是“Guy”出的题,题意不清,没有情景.神马意思?做个Cache吧. 代码: // http://www.careercup.com/question?id=5680330589601792 //…
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval Tree to get this done in O(logn), but I did not understand how to construct and use the Interval Tree after reading its wiki page. Is there any other w…
点这里 社交APP经典死法18种,听野路子产品菜狗怎么说 时间 2015-04-06 11:24:53  虎嗅网相似文章 (4)原文  http://www.huxiu.com/article/112016/1.html 自从2004年,一个乳臭未干的哈佛大二宅男闷着头做出世界上最大的社交网络以来,SNS社交网络就一直是大众关注的要点.在移动互联网时代,SNS几乎是投资界风险最高但一旦成功却收益最大的领域.尽管很多人已经表示2015年不再看好SNS,但这并不妨碍大量的热钱向这里集中. 人们看好社…
2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插入元素,使得链表仍然有序. 解法:由于是循环链表,所以表尾指向表头.链表只能顺序访问,不额外添加数据的情况下就没法以对数的时间进行查找了.有几个边缘情况需要考虑:1. 链表为空 2. 插入到链表头 3. 插入到链表尾.考虑到各种可能情况,就能做出这题. 代码: // http://www.caree…
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find the longest word that only uses letters from the string. [I didn't meet this question, what's the best solution?] 题目:给定一个字符串,和一个字典.从字典中找出一个最长的词,并且这个词…
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'node' is a binary search tree. // 'node' is guaranteed to be a binary tree. } n / \ a b \ c 题目:检查一棵二叉树是否为二叉搜索树. 解法:二叉搜索树,就是每个节点的左边全都小于它,右边全都大于它.如果真的对于每…