原文链接: https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci 英文原文Given two (singly) linked lists, determine if the two lists intersect. Return the inter­ secting node. Note that the intersection is defined based on reference, not …
分类详情
简单 分类
原文链接: https://leetcode-cn.com/problems/min-stack-lcci 英文原文How would you design a stack which, in addition to push and pop, has a function min which returns the minimum element? Push, pop and min should all operate in 0(1) time. Example: MinStack minS…
原文链接: https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci 英文原文Implement a MyQueue class which implements a queue using two stacks. Example: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // return 1 queue.po…
原文链接: https://leetcode-cn.com/problems/three-in-one-lcci 英文原文Describe how you could use a single array to implement three stacks. You should implement push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum) methods. stackNum is the index…
原文链接: https://leetcode-cn.com/problems/minimum-height-tree-lcci 英文原文Given a sorted (increasing order) array with unique integer elements, write an algo­rithm to create a binary search tree with minimal height. Example: Given sorted array: [-10,-3…
原文链接: https://leetcode-cn.com/problems/check-balance-lcci 英文原文Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node…
原文链接: https://leetcode-cn.com/problems/insert-into-bits-lcci 英文原文You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits …
原文链接: https://leetcode-cn.com/problems/exchange-lcci 英文原文Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g., bit 0 and bit 1 are swapped, bit 2 and bit 3 are swapped, and so on). Example1: Input: num = …
原文链接: https://leetcode-cn.com/problems/convert-integer-lcci 英文原文Write a function to determine the number of bits you would need to flip to convert integer A to integer B. Example1: Input: A = 29 (0b11101), B = 15 (0b01111) Output: 2 Example2: Input: …
原文链接: https://leetcode-cn.com/problems/palindrome-permutation-lcci 英文原文Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rear…
原文链接: https://leetcode-cn.com/problems/three-steps-problem-lcci 英文原文A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the s…
原文链接: https://leetcode-cn.com/problems/color-fill-lcci 英文原文Implement the "paint fill" function that one might see on many image editing programs. That is, given a screen (represented by a two-dimensional array of colors), a point, and a new color, fi…
原文链接: https://leetcode-cn.com/problems/reverse-bits-lcci 英文原文You have an integer and you can flip exactly one bit from a 0 to a 1. Write code to find the length of the longest sequence of 1s you could create. Example 1: Input: num = 1775(110111011112…
原文链接: https://leetcode-cn.com/problems/magic-index-lcci 英文原文A magic index in an array A[0...n-1] is defined to be an index such that A[i] = i. Given a sorted array of integers, write a method to find a magic index, if one exists, in array A. If not, …
原文链接: https://leetcode-cn.com/problems/sparse-array-search-lcci 英文原文Given a sorted array of strings that is interspersed with empty strings, write a method to find the location of a given string. Example1: Input: words = ["at", "", "", "", "ball", ""…
原文链接: https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof 中文题目用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 ) 示例 1: 输入: ["CQueue","appendTail","deleteHead","deleteH…
原文链接: https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof 中文题目写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下: F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), 其中 N > 1. 斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。 答案需要取模 1e9+7(1000000007),如…
原文链接: https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof 中文题目找出数组中重复的数字。 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。 示例 1: 输入: [2, 3, 1, 0, 2, 5, 3] 输出:2 或 3 限制: 2 <= n <= 100000 通过…
原文链接: https://leetcode-cn.com/problems/qing-wa-tiao-tai-jie-wen-ti-lcof 中文题目一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。 答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。 示例 1: 输入:n = 2 输出:2 示例 2: 输入:n = 7 输出:21 示例 3: 输入:n = 0 输出:1 提示: 0 <= n …
原文链接: https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof 中文题目把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。例如,数组 [3,4,5,1,2] 为 [1,2,3,4,5] 的一次旋转,该数组的最小值为1。 示例 1: 输入:[3,4…
原文链接: https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof 中文题目请实现一个函数,把字符串 s 中的每个空格替换成"%20"。 示例 1: 输入:s = "We are happy." 输出:"We%20are%20happy." 限制: 0 <= s 的长度 <= 10000 通过代码 高赞题解方法一:遍历添加在 Python 和 Java 等语言中,字符串都被设计成「不可变」的类型,即无法直接修改字符串的某一位字符,需要新建一个字…
原文链接: https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof 中文题目输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 限制: 0 <= 链表长度 <= 10000 通过代码 高赞题解方法一:递归法解题思路: 利用递归: 先走至链表末端,回溯时依次将节点值加入列表 ,这样就可以实现链表值的倒序输出。 Python …
原文链接: https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof 中文题目输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。 示例1: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 限制: 0 <= 链表长度 <= 1000 注意:本题与主站 21 题相同:https://leetcode-cn.com/problems/merge-two-s…
原文链接: https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof 中文题目请完成一个函数,输入一个二叉树,该函数输出它的镜像。 例如输入: 4 / \ 2 7 / \ / \ 1 3 6 9 镜像输出: 4 / \ 7 2 / \ / \ 9 6 3 1 示例 1: 输入:root = [4,2,7,1,3,6,9] 输出:[4,7,2,9,6,3,1] 限制: 0 <= 节点个数 <= 1000 注意:本题与主站 226 …
原文链接: https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof 中文题目请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / \ 2 2 \ \ 3 3 示例 1: 输入:root = [1…
原文链接: https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof 中文题目输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数在数组的前半部分,所有偶数在数组的后半部分。 示例: 输入:nums = [1,2,3,4] 输出:[1,3,2,4] 注:[3,1,2,4] 也是正确的答案之一。 提示: 0 <= nums.length <= …
原文链接: https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof 中文题目输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。 示例 1: 输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出:[1,2,3,6,9,8,7,4,5] 示例 2: 输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] 输出:[1,2,3,4,8,12,11,1…
原文链接: https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof 中文题目输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。 例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6。这个链表的倒数第 3 个节点是值为 4 的节点。 示例: 给定一个链表: 1->2->3->4->5, 和 k = 2. …
原文链接: https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof 中文题目输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。 示例 1: 输入: n = 1 输出: [1,2,3,4,5,6,7,8,9] 说明: 用返回一个整数列表来代替打印 n 为正整数 通过代码 高赞题解解题思路:题目要求打印 “从 $1$ 至最大的 $n…
原文链接: https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof 中文题目定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 限制: 0 <= 节点个数 <= 5000 注意:本题与主站 206 题相同:https://leetcode-cn.com/problems/reverse-linked-list/ …