09 2018 档案
摘要:在python2.7中这样调用代码open('file/name.txt','r',encoding= 'utf-8').read()会出现TypeError: 'encoding' is an invalid keyword argument for this fu...
阅读全文
摘要:在python2.7中这样调用代码open('file/name.txt','r',encoding= 'utf-8').read()会出现TypeError: 'encoding' is an invalid keyword argument for this fu...
阅读全文
摘要:Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:On...
阅读全文
摘要:Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:On...
阅读全文
摘要:Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictio...
阅读全文
摘要:Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictio...
阅读全文
摘要:Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Re...
阅读全文
摘要:Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Re...
阅读全文
摘要:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, "A man, a pl...
阅读全文
摘要:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, "A man, a pl...
阅读全文
摘要:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For exampl...
阅读全文
摘要:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For exampl...
阅读全文
摘要:Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that s...
阅读全文
摘要:Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that s...
阅读全文
摘要:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path...
阅读全文
摘要:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path...
阅读全文
摘要:Sort a linked list in O(n log n) time using constant space complexity.C++/** * Definition for singly-linked list. * struct ListNode { ...
阅读全文
摘要:Sort a linked list in O(n log n) time using constant space complexity.C++/** * Definition for singly-linked list. * struct ListNode { ...
阅读全文
摘要:Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear...
阅读全文
摘要:Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear...
阅读全文
摘要:Given an array of integers, every element appears three times except for one. Find that single one.Note: Your algorithm should have a ...
阅读全文
摘要:Given an array of integers, every element appears three times except for one. Find that single one.Note: Your algorithm should have a ...
阅读全文
摘要:Given a singly linked list L: L 0→L 1→…→L n-1→L n, reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…You must do this in-place without alte...
阅读全文
摘要:Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to...
阅读全文
摘要:Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to...
阅读全文
摘要:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previo...
阅读全文
摘要:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previo...
阅读全文
摘要: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 ...
阅读全文
摘要: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 ...
阅读全文
摘要:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example: Given the below bin...
阅读全文
摘要:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example: Given the below bin...
阅读全文
摘要:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1],...
阅读全文
摘要:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1],...
阅读全文
摘要:Given an index k, return the k th row of the Pascal's triangle.For example, given k = 3, Return[1,3,3,1].Note: Could you optimize your...
阅读全文
摘要:Given an index k, return the k th row of the Pascal's triangle.For example, given k = 3, Return[1,3,3,1].Note: Could you optimize your...
阅读全文
摘要:Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning o...
阅读全文
摘要:Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning o...
阅读全文
摘要:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindro...
阅读全文
摘要:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindro...
阅读全文
摘要:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down t...
阅读全文
摘要:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down t...
阅读全文
摘要:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.C++/** * Definition for a point. * ...
阅读全文
摘要:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.C++/** * Definition for a point. * ...
阅读全文
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example, Given[100, 4, 200, 1, 3...
阅读全文
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example, Given[100, 4, 200, 1, 3...
阅读全文
摘要:Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?C++/** * Definition for s...
阅读全文
摘要:Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?C++/** * Definition for s...
阅读全文
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up: Can you solve it without usin...
阅读全文
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up: Can you solve it without usin...
阅读全文
摘要:Sort a linked list using insertion sort.C++/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode ...
阅读全文
摘要:There are N gas stations along a circular route, where the amount of gas at station i isgas[i].You have a car with an unlimited gas ta...
阅读全文
摘要:Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer o...
阅读全文
摘要:Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which i...
阅读全文
摘要:A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Ret...
阅读全文
摘要:Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.OJ's undirected graph serialization: Nodes...
阅读全文
摘要:There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to t...
阅读全文
摘要:Given a binary tree, return the preorder traversal of its nodes' values.For example: Given binary tree{1,#,2,3},1 2 / 3return[1,2,3].N...
阅读全文
摘要:Given a binary tree, return the postorder traversal of its nodes' values.For example: Given binary tree{1,#,2,3},1 \ 2 / 3return[3,2...
阅读全文
摘要:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example: Given the below binary ...
阅读全文
摘要:Say you have an array for which the i th element is the price of a given stock on day i.If you were only permitted to complete at most...
阅读全文
摘要:Say you have an array for which the i th element is the price of a given stock on day i.Design an algorithm to find the maximum profit...
阅读全文
摘要:Say you have an array for which the i th element is the price of a given stock on day i.Design an algorithm to find the maximum profit...
阅读全文
摘要:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in...
阅读全文
摘要:像素设定 牛客网 程序员面试金典 题目描述 有一个单色屏幕储存在一维数组中,其中数组的每个元素代表连续的8位的像素的值,请实现一个函数,将第x到第y个像素涂上颜色(像素标号从零开始),并尝试尽量使用最快的办法。 给定表示屏幕的数组screen(数组中的每个元...
阅读全文
摘要:高度最小的BST 牛客网 程序员面试金典 C++ Python 题目描述 对于一个元素各不相同且按升序排列的有序序列,请编写一个算法,创建一棵高度最小的二叉查找树。 给定一个有序序列int[] vals,请返回创建的二叉查找树的高度。 C++class Min...
阅读全文
摘要:集合栈 牛客网 程序员面试金典 C++ Python题目描述 请实现一种数据结构SetOfStacks,由多个栈组成,其中每个栈的大小为size,当前一个栈填满时,新建一个栈。该数据结构应支持与普通栈相同的push和pop操作。 给定一个操作序列int[][2] op...
阅读全文
摘要:链表分割 牛客网 程序员面试金典 C++ Python题目描述 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据...
阅读全文
摘要:链表中倒数第K个结点 牛客网 程序员面试金典 C++ Python题目描述 输入一个链表,输出该链表中倒数第k个结点。C++/*struct ListNode { int val; struct ListNode *next; ListNode(in...
阅读全文
摘要:链式A+B 牛客网 程序员面试金典 C++ Python题目描述 有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。 给定两个链表ListNode* A,ListNode* B...
阅读全文
摘要:输出单层结点 牛客网 程序员面试金典 C++ Python 题目描述 对于一棵二叉树,请设计一个算法,创建含有某一深度上所有结点的链表。 给定二叉树的根结点指针TreeNode* root,以及链表上结点的深度,请返回一个链表ListNode,代表该深度上所有...
阅读全文
摘要:大家好!欢迎来到我的博客。阅读即表示您遵循以下守则:1、请勿盗取我设置为原创的文章,也请各位发现立即向我检举,但有些原创我会标注允许转载,这时如要转载,也要打上转载标签。2、请勿恶意踩,如果真的认为写的错误百出可以踩,并在回复区向我提出,感谢您的纠正!3、后...
阅读全文
摘要:访问单个结点的删除 牛客网 程序员面试金典 C++ Python 题目描述 实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。 给定待删除的节点,请执行删除操作,若该节点为尾节点,返回false,否则返回true C++/*struct List...
阅读全文
摘要:反转子串 牛客网 程序员面试金典 C++ Python 题目描述 假定我们都知道非常高效的算法来检查一个单词是否为其他字符串的子串。请将这个算法编写成一个函数,给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成,要求只能调用一次检查子串的函数。 给...
阅读全文
摘要:第K个数 牛客网 程序员面试金典 C++ Python 题目描述 有一些数的素因子只有3、5、7,请设计一个算法,找出其中的第k个数。 给定一个数int k,请返回第k个数。保证k小于等于100。 测试样例: 3 返回:7 C++class Kth...
阅读全文
摘要:穿点最多的直线 牛客网 程序员面试金典 C++ 题目描述 在二维平面上,有一些点,请找出经过点数最多的那条线。 给定一个点集vectorp和点集的大小n,没有两个点的横坐标相等的情况,请返回一个vector,代表经过点数最多的那条直线的斜率和截距。 C++/*...
阅读全文
摘要:空格替换 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。 给定一个str...
阅读全文
摘要:碰撞的蚂蚁 牛客网 程序员面试金典 C++ Java Python 题目描述 在n个顶点的多边形上有n只蚂蚁,这些蚂蚁同时开始沿着多边形的边爬行,请求出这些蚂蚁相撞的概率。(这里的相撞是指存在任意两只蚂蚁会相撞) 给定一个int n(3<=n<=10000),...
阅读全文
摘要:确定字符互异 牛客网 程序员面试金典 C++ Python 题目描述 请实现一个算法,确定一个字符串的所有字符是否全都不同。这里我们要求不允许使用额外的存储结构。 给定一个string iniString,请返回一个bool值,True代表所有字符全都不同,F...
阅读全文
摘要:确定两串乱序同构 牛客网 程序员面试金典 C++ Python 题目描述 给定两个字符串,请编写程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。这里规定大小写为不同字符,且考虑字符串中的空格。 给定一个string stringA和一个stri...
阅读全文
摘要:两个栈实现队列 牛客网 程序员面试金典 C++ Python题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。C++//run:5ms memeory:612kclass Solution{public: void p...
阅读全文
摘要:猫狗收容所 牛客网 程序员面试金典 C++ 题目描述 有家动物收容所只收留猫和狗,但有特殊的收养规则,收养人有两种收养方式,第一种为直接收养所有动物中最早进入收容所的,第二种为选择收养的动物类型(猫或狗),并收养该种动物中最早进入收容所的。 给定一个操作序列i...
阅读全文
摘要:清除行列 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个算法,若N阶方阵中某个元素为0,则将其所在的行与列清零。 给定一个N阶方阵int[]mat和矩阵的阶数n,请返回完成操作后的int[][]方阵(C++中为vector>),保证n小于等...
阅读全文
摘要:检查是否是BST 牛客网 程序员面试金典 C++ java Python题目描述 请实现一个函数,检查一棵二叉树是否为二叉查找树。 给定树的根结点指针TreeNode* root,请返回一个bool,代表该树是否为二叉查找树。C++/*struct TreeNode...
阅读全文
摘要:有向路径检查 牛客网 程序员面试金典 C++ Python题目描述 对于一个有向图,请实现一个算法,找出两点之间是否存在一条路径。 给定图中的两个结点的指针DirectedGraphNode* a, DirectedGraphNode* b(请不要在意数据类型,图是有...
阅读全文
摘要:最近公共祖先 牛客网 程序员面试金典 C++ Python 题目描述 有一棵无穷大的满二叉树,其结点按根结点一层一层地从左往右依次编号,根结点编号为1。现在有两个结点a,b。请设计一个算法,求出a和b点的最近公共祖先的编号。 给定两个int a,b。为给定结点...
阅读全文
摘要:最接近的数 牛客网 程序员面试金典 C++ Python 题目描述 有一个正整数,请找出其二进制表示中1的个数相同、且大小最接近的那两个数。(一个略大,一个略小) 给定正整数int x,请返回一个vector,代表所求的两个数(小的在前)。保证答案存在。 ...
阅读全文
摘要:最大连续数列和 牛客网 程序员面试金典 C++ Python 题目描述 对于一个有正有负的整数数组,请找出总和最大的连续数列。 给定一个int数组A和数组大小n,请返回最大的连续数列的和。保证n的大小小于等于3000。 测试样例: [1,2,3,-6...
阅读全文
摘要:无缓存交换 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个函数,函数内不使用任何临时变量,直接交换两个数的值。 给定一个int数组AB,其第零个元素和第一个元素为待交换的值,请返回交换后的数组。 测试样例: [1,2] 返回:[...
阅读全文
摘要:无判断max 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个方法,找出两个数字中最大的那个。条件是不得使用if-else等比较和判断运算符。 给定两个int a和b,请返回较大的一个数。若两数相同则返回任意一个。 测试样例: 1,...
阅读全文
摘要:整数转化 牛客网 程序员面试金典 C++ Python 题目描述 编写一个函数,确定需要改变几个位,才能将整数A转变成整数B。 给定两个整数int A,int B。请返回需要改变的数位个数。 测试样例: 10,5 返回:4 C++class Tra...
阅读全文
摘要:平衡二叉树检查 牛客网 程序员面试金典 C++ Python 题目描述 实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1。 给定指向树根结点的指针TreeNode* root,请返回一个bool,代表这棵树是...
阅读全文
摘要:平分的直线 牛客网 程序员面试金典 C++ Python 题目描述 在二维平面上,有两个正方形,请找出一条直线,能够将这两个正方形对半分。假定正方形的上下两条边与x轴平行。 给定两个vecotrA和B,分别为两个正方形的四个顶点。请返回一个vector,代表所...
阅读全文
摘要:寻找下一个结点 牛客网 程序员面试金典 C++ java Python题目描述 请设计一个算法,寻找二叉树中指定结点的下一个结点(即中序遍历的后继)。 给定树的根结点指针TreeNode* root和结点的值int p,请返回值为p的结点的后继结点的值。保证结点的值大...
阅读全文
摘要:奇偶位交换 牛客网 程序员面试金典 C++ Python 题目描述 请编写程序交换一个数的二进制的奇数位和偶数位。(使用越少的指令越好) 给定一个int x,请返回交换后的数int。 测试样例: 10 返回:5 C++class Exchange ...
阅读全文
摘要:字符串压缩 牛客网 程序员面试金典 C++ Python 题目描述 利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能。比如,字符串“aabcccccaaa”经压缩会变成“a2b1c5a3”。若压缩后的字符串没有变短,则返回原先的字符串。 给定一个...
阅读全文
摘要:回文链表 牛客网 程序员面试金典 C++ Python题目描述 请编写一个函数,检查链表是否为回文。 给定一个链表ListNode* pHead,请返回一个bool,代表链表是否为回文。 测试样例: {1,2,3,2,1} 返回:true {1,2,3,2,3} 返...
阅读全文
摘要:另类加法 牛客网 程序员面试经典 C++ Python 题目描述 请编写一个函数,将两个数字相加。不得使用+或其他算数运算符。 给定两个int A和B。请返回A+B的值 测试样例: 1,2 返回:3 C++class UnusualAdd {pu...
阅读全文
摘要:双栈排序 牛客网 程序员面试金典 C++ Python 题目描述 请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中。 给定一个int[] numbers(C++中为vector...
阅读全文
摘要:原串反转 牛客网 程序员面试金典 C++ Python 题目描述 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量)。 给定一个string iniString,请返回一个string,为翻转后的字符串。保证字...
阅读全文
摘要:加法运算替代 牛客网 程序员面试金典 题目描述 请编写一个方法,实现整数的乘法、减法和除法运算(这里的除指整除)。只允许使用加号。 给定两个正整数int a,int b,同时给定一个int type代表运算的类型,1为求a * b,0为求a / b,-1为求...
阅读全文
摘要:判断直线相交 牛客网 程序员面试金典 题目描述 给定直角坐标系上的两条直线,确定这两条直线会不会相交。 线段以斜率和截距的形式给出,即double s1,double s2,double y1,double y2,分别代表直线1和2的斜率(即s1,s2)和截距...
阅读全文
摘要:像素反转 牛客网 程序员面试金典 题目描述 有一副由NxN矩阵表示的图像,这里每个像素用一个int表示,请编写一个算法,在不占用额外内存空间的情况下(即不使用缓存矩阵),将图像顺时针旋转90度。 给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵...
阅读全文
摘要:二进制插入 牛客网 程序员面试金典 题目描述 有两个32位整数n和m,请编写算法将m的二进制数位插入到n的二进制的第j到第i位,其中二进制的位数从低位数到高位且以0开始。 给定两个数int n和int m,同时给定int j和int i,意义如题所述,请返回操...
阅读全文
摘要:二进制小数 牛客网 程序员面试金典 题目描述 有一个介于0和1之间的实数,类型为double,返回它的二进制表示。如果该数字无法精确地用32位以内的二进制表示,返回“Error”。 给定一个double num,表示0到1的实数,请返回一个string,代表该...
阅读全文
摘要:二叉树中和为某一值的路径 牛客网 程序员面试金典题目描述 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)c++/...
阅读全文
摘要:from pylab import *w1 = 1 w2 = 25fs = 18y = np.arange(-2,2,0.001)x = w1*y*log(y)-1.0/w2*exp(-(w2*y-w2/exp(1))**4)plt.title(u'DARREN DR...
阅读全文
摘要:matplotlib是Python中强大的绘图库。matplotlib下pyplot和pylab均可以绘图。具体来说两者的区别pyplot 方便快速绘制matplotlib通过pyplot模块提供了一套和MATLAB类似的绘图API,将众多绘图对象构成的复杂结构隐藏在...
阅读全文
摘要:curl是利用URL语法在命令行方式显工作的开元文件传输工具。安装$ sudo apt install -y curl使用$ curl http://www.baidu.com这是最简单的使用方法。用这个命令获得到http://www.baidu.com指向的页面,同...
阅读全文
摘要:Ubuntu菜单栏的位置可以调 到左侧 或者底部调整到底部$ gsettings set com.canonical.Unity.Launcher launcher-position Bottom调整到左侧$ gsettings set com.canonical.U...
阅读全文
摘要:RSS(简易信息聚合)简易信息聚合(也叫聚合内容)是一种RSS基于XML标准,在互联网上被广泛采用的内容包装和投递协议。RSS(Really Simple Syndication)是一种描述和同步网站的内容格式,是使用最广泛的XML应用。RSS搭建了信息迅速传播的一个...
阅读全文
摘要:xrandr -o left$ xrandr -o left 向左旋转90度$ xrandr -o right 向右旋转90度$ xrandr -o inverted 上下翻转$ xrandr -o normal 回到正常角度
阅读全文
摘要:import numpy as npimport matplotlib.pyplot as plt import mathdef create_data(w1=3,w2=-7,b=4,seed=1,size=30): np.random.seed(seed) ...
阅读全文
摘要:import numpy as npimport matplotlib.pyplot as plt mu,sigma = 2,0.5v = np.random.normal(mu,sigma,10000)plt.hist(v,bins=50,density=1)plt...
阅读全文
摘要:import numpy as npimport matplotlib.pyplot as plt def mandelbrot(h,w,maxit=20): y,x = np.ogrid[-1.4:1.4:h*1j,-2:0.8:w*1j] c = x+...
阅读全文
摘要:顺时针打印矩阵 牛客网 剑指Offer题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,1...
阅读全文
摘要:链表中环的入口结点 牛客网 剑指Offer题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。# class ListNode:# def __init__(self, x):# self.val = x# ...
阅读全文
摘要:链表中倒数第K个结点 牛客网 剑指Offer题目描述 输入一个链表,输出该链表中倒数第k个结点。# class ListNode:# def __init__(self, x):# self.val = x# self.next...
阅读全文
摘要:重建二叉树 牛客网 剑指Offer题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二...
阅读全文
摘要:连续子序列的最大和 牛客网 剑指Offer题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某...
阅读全文
摘要:跳台阶 牛客网 剑指Offer题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。class Solution: #run:26ms memory:5624k def jumpFl...
阅读全文
摘要:调整数组顺序使奇数位于偶数前面 牛客网 剑指Offer题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。class Solution: ...
阅读全文
摘要:表示数值的字符串 牛客网 剑指Offer题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5...
阅读全文
摘要:反转单词顺序列 牛客网 剑指Offer题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识...
阅读全文
摘要:第一个只出现一次字符的位置 牛客网 剑指Offer题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写)class Solution: #run:28ms ...
阅读全文
摘要:矩阵中的路径 牛客网 剑指Offer题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入...
阅读全文
摘要:矩形覆盖 牛客网 剑指Offer题目描述 我们可以用21的小矩形横着或者竖着去覆盖更大的矩形。请问用n个21的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?class Solution: def rectCover(self, number): ...
阅读全文
摘要:两个栈实现队列 牛客网 剑指Offer 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。class Solution: def __init__(self): self.stk1 = [] ...
阅读全文
摘要:滑动窗口的最大值 牛客网 剑指Offer题目描述 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{...
阅读全文
摘要:求1+2+3...+n 牛客网 剑指Offer题目描述 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。class Solution: #run:23ms memory:...
阅读全文
摘要:正则表达式匹配 牛客网 剑指Offer题目描述 请实现一个函数用来匹配包括'.'和''的正则表达式。模式中的字符'.'表示任意一个字符,而''表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a...
阅读全文
摘要:树的子结构 牛客网 剑指Offer题目描述 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)# class TreeNode:# def __init__(self, x):# self.val = x...
阅读全文
摘要:栈的压入、弹出顺序 牛客网 剑指Offer题目描述输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但...
阅读全文
摘要:构建成绩数组 牛客网 剑指Offer题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]A[1]...*A[i-1]A[i+1]...*A[n-1]。不能使用除法class Solution:...
阅读全文
摘要:机器人的运动范围 牛客网 剑指Offer题目描述 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因...
阅读全文
摘要:最小的K个数 牛客网 剑指Offer题目描述 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。class Solution: #run:33ms memory:5732k def G...
阅读全文
摘要:替换空格 牛客网 剑指Offer题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。class Solution: #run:25ms memor...
阅读全文
摘要:旋转数组的最小数字 牛客网 剑指Offer题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 ...
阅读全文
摘要:斐波那契数列 牛客网 剑指Offer题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 n<=39class Solution: def Fibonacci(self, n): if n...
阅读全文
摘要:整数中1出现的次数 牛客网 剑指Offer题目描述 求出113的整数中1出现的次数,并算出1001300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把...
阅读全文
摘要:数组中重复的数字 牛客网 剑指Offer题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2...
阅读全文
摘要:数组中的逆序对 牛客网 剑指Offer 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007 输入描...
阅读全文
摘要:数组中只出现过一次的数字 牛客网 剑指Offer题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了偶数次。请写程序找出这两个只出现一次的数字。 def FindNumsAppearOnce(self, array): if array =...
阅读全文
摘要:数组中出现次数超过一半的数字 牛客网 剑指Offer题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出...
阅读全文
摘要:数据流中的中位数 牛客网 剑指Offer题目描述 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取...
阅读全文
摘要:数字在排序数组中出现的次数 牛客网 剑指Offer题目描述 统计一个数字在排序数组中出现的次数。class Solution: def GetNumberOfK(self, data, k): if data == None or data == ...
阅读全文
摘要:数值的整数次方 牛客网 剑指Offer题目描述 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方class Solution: #run:23ms memory:5728k def Power(s...
阅读全文
摘要:按之字形顺序打印二叉树 牛客网 剑指Offer 题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。# class TreeNode:# def __init...
阅读全文
摘要:把数组排成最小的数 牛客网 剑指Offer题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。from functools impor...
阅读全文
摘要:把字符串转换成整数 牛客网 剑指Offer题目描述 将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。 输...
阅读全文
摘要:把二叉树打印成多行 牛客网 剑指Offer 题目描述 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行# class TreeNode:# def __init__(self, x):# self.val = x# ...
阅读全文