04 2019 档案

摘要:题目描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。 使用中序遍历 1 public class Solution {//树 my 2 TreeNode head = null; 3 TreeNode pre =null; 4 阅读全文
posted @ 2019-04-18 13:57 月半榨菜 阅读(88) 评论(0) 推荐(0)
摘要:题目描述 把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。 解析来源 https://www.nowcoder.com/questionTerminal/6aa9e0 阅读全文
posted @ 2019-04-15 21:52 月半榨菜 阅读(122) 评论(0) 推荐(0)
摘要:题目描述 输入两个链表,找出它们的第一个公共结点。 首先获得两个链表的长度,将长的链表先走两个链表的长度差,然后在一起走,判断当前结点是否相同 1 public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {//链表 阅读全文
posted @ 2019-04-15 17:35 月半榨菜 阅读(92) 评论(0) 推荐(0)
摘要:题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。 将所有的数异或,根据得到的结果的最后一位1区分为两部分,再分别异或,得到的两个结果即最后答案 public void FindNumsAppearOnce(int [] array,int num 阅读全文
posted @ 2019-04-15 17:05 月半榨菜 阅读(75) 评论(0) 推荐(0)
摘要:题目描述 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.. 阅读全文
posted @ 2019-04-15 16:53 月半榨菜 阅读(97) 评论(0) 推荐(0)
摘要:题目描述 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+ 阅读全文
posted @ 2019-04-15 16:33 月半榨菜 阅读(81) 评论(0) 推荐(0)
摘要:题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。 输出描述: 如果当前字符流没有存在出现一次的字符,返回#字符。 请实现一个函 阅读全文
posted @ 2019-04-15 15:48 月半榨菜 阅读(77) 评论(0) 推荐(0)
摘要:题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快 阅读全文
posted @ 2019-04-15 14:59 月半榨菜 阅读(94) 评论(0) 推荐(0)
摘要:题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a 阅读全文
posted @ 2019-04-15 14:13 月半榨菜 阅读(95) 评论(0) 推荐(0)
摘要:题目描述 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它! 阅读全文
posted @ 2019-04-15 13:35 月半榨菜 阅读(109) 评论(0) 推荐(0)
摘要:题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 使用map记录每个字符出现的次数,并查询第一个出现一次的字符 1 public int FirstNotRepeatingChar(Str 阅读全文
posted @ 2019-04-15 12:48 月半榨菜 阅读(123) 评论(0) 推荐(0)
摘要:题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数(从1 到 阅读全文
posted @ 2019-04-12 19:27 月半榨菜 阅读(141) 评论(0) 推荐(0)
摘要:题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 使用递归,计算子树的高度,如果从上到下逐个根节点判断需要做很多重复的操作,所以从下往上更省时 public class Solution {//树 my public boolean IsBalanced_Solution(TreeNode 阅读全文
posted @ 2019-04-12 17:31 月半榨菜 阅读(72) 评论(0) 推荐(0)
摘要:题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。 使用位运算,^表示除去进位外的值,&表示进位的值 1 public int Add(int num1,int num2) { //位运算 my 2 while(num2!=0){ 3 int val = num 阅读全文
posted @ 2019-04-12 16:41 月半榨菜 阅读(81) 评论(0) 推荐(0)
摘要:题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。 输出描述: 对应每个测试案例,输出两个数,小的先输出。 方法一:使用set保存访问过得结点,新结点array[i]判断sum-array[i]是否在set中,时 阅读全文
posted @ 2019-04-12 16:09 月半榨菜 阅读(112) 评论(0) 推荐(0)
摘要:题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空) 方法一:首先建立一个新链表,并处理链表的next结点;然后逐个结点查找链表 阅读全文
posted @ 2019-04-12 15:43 月半榨菜 阅读(113) 评论(0) 推荐(0)
摘要:题目描述 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前) 使用dfs 1 public class Solution {//dfs 树 my 2 阅读全文
posted @ 2019-04-12 14:23 月半榨菜 阅读(101) 评论(0) 推荐(0)
摘要:题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长 阅读全文
posted @ 2019-04-12 13:44 月半榨菜 阅读(101) 评论(0) 推荐(0)
摘要:题目描述 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构) 使用递归 1 public class Solution {//树 my 2 public boolean HasSubtree(TreeNode root1,TreeNode root2) { 3 阅读全文
posted @ 2019-04-12 11:15 月半榨菜 阅读(111) 评论(0) 推荐(0)
摘要:题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。 方法一:使用递归 1 public class Solution {//树 my 2 public boolean VerifySquenceOfBS 阅读全文
posted @ 2019-04-12 10:40 月半榨菜 阅读(85) 评论(0) 推荐(0)
摘要:题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。 方法一:使用冒泡或插入的思想调整数字次序,时间复杂度为O(n2),空间复杂度为O(1) 方法二:使用另一个数组辅助,时间 阅读全文
posted @ 2019-04-11 21:44 月半榨菜 阅读(83) 评论(0) 推荐(0)
摘要:题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。 使用一个栈来保存当前的最小值 1 public class Solution {//栈 my 2 private int minValue = Integer.MAX_VALUE; 3 阅读全文
posted @ 2019-04-11 21:30 月半榨菜 阅读(68) 评论(0) 推荐(0)
摘要:题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印。 层次遍历 public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {//树 my Queue<TreeNode> queue = new LinkedList<Tre 阅读全文
posted @ 2019-04-11 21:26 月半榨菜 阅读(64) 评论(0) 推荐(0)
摘要:题目描述 输入一个链表,输出该链表中倒数第k个结点。 快慢指针 1 public ListNode FindKthToTail(ListNode head,int k) {//链表 my 2 if(k<=0){ 3 return null; 4 } 5 ListNode node = head; 6 阅读全文
posted @ 2019-04-11 20:25 月半榨菜 阅读(77) 评论(0) 推荐(0)
摘要:题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。 1 public ListNode Merge(ListNode list1,ListNode list2) {//链表 my 2 ListNode head = new ListNode(0); 阅读全文
posted @ 2019-04-11 20:13 月半榨菜 阅读(101) 评论(0) 推荐(0)
摘要:题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 StringBuffer是可改字符串 1 public String replaceSpace(StringBuffer str) 阅读全文
posted @ 2019-04-11 17:44 月半榨菜 阅读(77) 评论(0) 推荐(0)
摘要:题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。 1 publ 阅读全文
posted @ 2019-04-11 16:42 月半榨菜 阅读(81) 评论(0) 推荐(0)
摘要:题目描述 统计一个数字在排序数组中出现的次数。 1 public int GetNumberOfK(int [] array , int k) {//my 2 int re =0; 3 for (int i = 0; i < array.length && array[i]<= k; i++) { 阅读全文
posted @ 2019-04-11 16:40 月半榨菜 阅读(64) 评论(0) 推荐(0)
摘要:题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5使用递归 1 public void Mirror(TreeNode root) 阅读全文
posted @ 2019-04-11 16:31 月半榨菜 阅读(99) 评论(0) 推荐(0)
摘要:题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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,16,15,14,13,9,5,6,7,11,10. 首先判断需要走几圈 阅读全文
posted @ 2019-04-11 16:25 月半榨菜 阅读(100) 评论(0) 推荐(0)
摘要:题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 根据前序遍历序列找到子树根节点,并在中序遍历序列中找 阅读全文
posted @ 2019-04-10 21:59 月半榨菜 阅读(123) 评论(0) 推荐(0)
摘要:题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。 中序遍历二叉搜索树,找到第k个结点,除了使用递归,还可以使用栈实现非递归写法 1 public class Solution {//树 my 2 int ind 阅读全文
posted @ 2019-04-10 21:53 月半榨菜 阅读(107) 评论(0) 推荐(0)
摘要:题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。 使用双向链表,先把当前行按顺序打印,再取出当前行并将下一行存入链表 1 public ArrayList<ArrayList<Integer> 阅读全文
posted @ 2019-04-09 21:29 月半榨菜 阅读(106) 评论(0) 推荐(0)
摘要:题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 若结点存在右孩子,则右孩子的最坐下结点为中序遍历下一个结点 若没有右孩子,则该结点为祖先的左子树最右下结点的最近的一个祖先,为下一个结点 public T 阅读全文
posted @ 2019-04-09 19:51 月半榨菜 阅读(84) 评论(0) 推荐(0)
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmet 阅读全文
posted @ 2019-04-09 19:45 月半榨菜 阅读(85) 评论(0) 推荐(0)
摘要:题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 使用dp,当n时,选着竖着放一个,那么后面的可能性为f(n-1) 选择横着放一个,那么要占两个位置,后面的可能性为f(n-2) 故f(n)=f(n-1)+f(n 阅读全文
posted @ 2019-04-09 16:18 月半榨菜 阅读(63) 评论(0) 推荐(0)
摘要:题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。 f(n)=f(n-1)+f(n-2)+...+f(n-n) =f(0)+f(1)+...+f(n-1) f(n-1)=f(n-2)+f(n-3)+...+f(n-n)=f(0)+ 阅读全文
posted @ 2019-04-09 15:51 月半榨菜 阅读(94) 评论(0) 推荐(0)
摘要:题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。 方法一:利用ArrayList库函数 1 public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {//链表 my 2 ArrayList<I 阅读全文
posted @ 2019-04-09 11:45 月半榨菜 阅读(95) 评论(0) 推荐(0)
摘要:题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5 1 public ListNode deleteDuplication(ListNode pHead) 2 {//链 阅读全文
posted @ 2019-04-09 11:27 月半榨菜 阅读(124) 评论(0) 推荐(0)
摘要:题目描述 给定一个数组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]。不能使用除法。 方法一:逐个相乘,时间复杂度为O(n2) 1 public int[] multipl 阅读全文
posted @ 2019-04-08 20:31 月半榨菜 阅读(99) 评论(0) 推荐(0)
摘要:题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 方法一:从[0][0]逐个查找,时间复杂度为O(n2),空间复杂度为O(1) 方法二:从[0][ 阅读全文
posted @ 2019-04-08 19:44 月半榨菜 阅读(65) 评论(0) 推荐(0)
摘要:题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。 方法一:使用数组记录数 阅读全文
posted @ 2019-04-08 19:33 月半榨菜 阅读(85) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2019-04-07 13:46 月半榨菜 阅读(136) 评论(0) 推荐(0)
摘要:There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct 阅读全文
posted @ 2019-04-06 18:00 月半榨菜 阅读(129) 评论(0) 推荐(0)
摘要: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 adjacen 阅读全文
posted @ 2019-04-06 17:03 月半榨菜 阅读(93) 评论(0) 推荐(0)
摘要:Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitt 阅读全文
posted @ 2019-04-06 15:17 月半榨菜 阅读(104) 评论(0) 推荐(0)
摘要:You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you nee 阅读全文
posted @ 2019-04-06 13:25 月半榨菜 阅读(110) 评论(0) 推荐(0)
摘要:Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Note: There may be more than one LIS combination, it 阅读全文
posted @ 2019-04-06 12:07 月半榨菜 阅读(141) 评论(0) 推荐(0)
摘要:Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer feerepresent 阅读全文
posted @ 2019-04-05 17:41 月半榨菜 阅读(140) 评论(0) 推荐(0)
摘要:Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may comple 阅读全文
posted @ 2019-04-04 17:14 月半榨菜 阅读(114) 评论(0) 推荐(0)
摘要:Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may comple 阅读全文
posted @ 2019-04-02 21:42 月半榨菜 阅读(108) 评论(0) 推荐(0)
摘要:Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may comple 阅读全文
posted @ 2019-04-02 21:36 月半榨菜 阅读(126) 评论(0) 推荐(0)
摘要:Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: E 阅读全文
posted @ 2019-04-02 17:38 月半榨菜 阅读(85) 评论(0) 推荐(0)
摘要: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 example, given the fo 阅读全文
posted @ 2019-04-02 16:36 月半榨菜 阅读(83) 评论(0) 推荐(0)
摘要: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 adja 阅读全文
posted @ 2019-04-02 11:31 月半榨菜 阅读(86) 评论(0) 推荐(0)
摘要:Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjac 阅读全文
posted @ 2019-04-02 11:13 月半榨菜 阅读(119) 评论(0) 推荐(0)
摘要:Implement a trie with insert, search, and startsWith methods. Example: Note: You may assume that all inputs are consist of lowercase letters a-z. All 阅读全文
posted @ 2019-04-02 09:53 月半榨菜 阅读(113) 评论(0) 推荐(0)