剑指offer
文章平均质量分 65
Bryan要加油
这个作者很懒,什么都没留下…
展开
-
剑指 Offer 15. 二进制中1的个数[Easy](Leetcode每日一题-2021.06.23)
Problem请实现一个函数,输入一个整数(以二进制串形式),输出该数二进制表示中 1 的个数。例如,把 9 表示成二进制是 1001,有 2 位是 1。因此,如果输入 9,则该函数输出 2。提示:输入必须是长度为 32 的 二进制串 。Example1输入:00000000000000000000000000001011输出:3解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 ‘1’。Example2输入:000000000原创 2021-06-23 21:50:49 · 299 阅读 · 0 评论 -
剑指 Offer 11. 旋转数组的最小数字&154. Find Minimum in Rotated Sorted Array II(Leetcode每日一题-2020.07.22)
ProblemSuppose 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]).Find the minimum element.The array may contain duplicates.Example1Input: [1,3,5]Output: 1E原创 2020-07-22 21:12:41 · 175 阅读 · 0 评论 -
剑指 Offer 09. 用两个栈实现队列(Leetcode每日一题-2020.06.30)
Problem用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )Example1输入:[“CQueue”,“appendTail”,“deleteHead”,“deleteHead”][[],[3],[],[]]输出:[null,null,3,-1]Example2输入:[“CQueue”,“deleteHead”原创 2020-06-30 14:01:41 · 265 阅读 · 0 评论 -
面试题46. 把数字翻译成字符串(Leetcode每日一题-2020.06.09)
Problem给定一个数字,我们按照如下规则把它翻译为字符串:0 翻译成 “a” ,1 翻译成 “b”,……,11 翻译成 “l”,……,25 翻译成 “z”。一个数字可能有多个翻译。请编程实现一个函数,用来计算一个数字有多少种不同的翻译方法。Example输入: 12258输出: 5解释: 12258有5种不同的翻译,分别是"bccfi", “bwfi”, “bczi”, “mcfi"和"mzi”Solution动态规划,本题与91. Decode Ways类似。class Sol原创 2020-06-09 22:27:52 · 381 阅读 · 0 评论 -
54. Spiral Matrix&面试题29. 顺时针打印矩阵(Leetcode每日一题-2020.06.05)
ProblemGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example1Input:[[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]Example2Input:[[1, 2, 3, 4],[5, 6, 7, 8],[9,10,11原创 2020-06-07 15:09:12 · 368 阅读 · 0 评论 -
面试题64. 求1+2+…+n(Leetcode每日一题-2020.06.02)
Problem求 1+2+…+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。Example1输入: n = 3输出: 6Example2输入: n = 9输出: 45Constraints1 <= n <= 10000Solution逻辑短路+递归class Solution {public: int sumNums(int n) { n >= 1 &a原创 2020-06-02 18:36:37 · 184 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal && 面试题07(Leetcode每日一题-2020.05.22)
ProblemGiven preorder and inorder traversal of a tree, construct the binary tree.ExampleFor example, givenpreorder = [3,9,20,15,7]inorder = [9,3,15,20,7]return the following binary treeSolut...原创 2020-03-05 21:04:39 · 196 阅读 · 0 评论 -
53. Maximum Subarray&面试题42. 连续子数组的最大和(Leetcode每日一题-2020.05.03)
ProblemGiven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example1Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Exp...原创 2020-05-03 21:13:15 · 178 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal && 面试题32 - II. 从上到下打印二叉树 II(Leetcode每日一题-2020.05.13)
ProblemGiven a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).ExampleGiven binary tree [3,9,20,null,null,15,7],return its level order...原创 2020-02-26 23:38:37 · 150 阅读 · 0 评论 -
面试题 02.02. Kth Node From End of List LCCI&面试题22. 链表中倒数第k个节点
ProblemImplement an algorithm to find the kth to last element of a singly linked list. Return the value of the element.Note:k is always valid.This problem is slightly different from the original ...原创 2020-03-23 22:47:35 · 183 阅读 · 0 评论 -
138. Copy List with Random Pointer&面试题35. 复杂链表的复制
ProblemA linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.The Linked List is represent...原创 2020-05-06 20:01:48 · 155 阅读 · 0 评论 -
160. Intersection of Two Linked Lists&面试题52. 两个链表的第一个公共节点&面试题 02.07. Intersection of Two Linked List
ProblemWrite a program to find the node at which the intersection of two singly linked lists begins.Example1A and B intersect at c1Example2A and B has not intersection.Solution/** * Definiti...原创 2020-05-05 22:14:44 · 241 阅读 · 0 评论 -
206. Reverse Linked List(Leetcode每日一题-2020.03.02)
ProblemReverse a singly linked list.ExampleInput: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLSolutionSolution1三指针迭代/** * Definition for singly-linked list....原创 2020-03-02 20:19:24 · 148 阅读 · 0 评论 -
面试题56 - I. 数组中数字出现的次数(Leetcode每日一题-2020.04.28)
Problem一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。Example1输入:nums = [4,1,4,6]输出:[1,6] 或 [6,1]Example2输入:nums = [1,2,10,4,1,4,3,3]输出:[2,10] 或 [10,2]Solutiona...原创 2020-04-28 22:31:57 · 233 阅读 · 0 评论 -
面试题51. 数组中的逆序对(Leetcode每日一题-2020.04.24)
占坑归并排序原创 2020-04-24 21:14:29 · 280 阅读 · 0 评论 -
面试题13. 机器人的运动范围(Leetcode每日一题-2020.04.08)
Problem地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外),也不能进入行坐标和列坐标的数位之和大于k的格子。例如,当k为18时,机器人能够进入方格 [35, 37] ,因为3+5+3+7=18。但它不能进入方格 [35, 38],因为3+5+3+8=19。请问该机...原创 2020-04-08 23:31:30 · 348 阅读 · 0 评论 -
47. Permutations II && 面试题 08.08. Permutation II LCCI && 面试题38. 字符串的排列
Problem47Given a collection of numbers that might contain duplicates, return all possible unique permutations.ExampleInput: [1,1,2]Output:[[1,1,2],[1,2,1],[2,1,1]]面试题08.08Write a method ...原创 2020-03-29 17:21:57 · 246 阅读 · 0 评论 -
面试题40. 最小的k个数(Leetcode每日一题-2020.03.20)
Problem输入整数数组 arr ,找出其中最小的 k 个数。例如,输入4、5、1、6、2、7、3、8这8个数字,则最小的4个数字是1、2、3、4。限制:0 <= k <= arr.length <= 100000 <= arr[i] <= 10000Example1输入:arr = [3,2,1], k = 2输出:[1,2] 或者 [2,1]...原创 2020-03-20 19:27:57 · 201 阅读 · 0 评论 -
面试题57 - II. 和为s的连续正数序列(Leetcode每日一题-2020.03.06)
Problem输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。限制:1 <= target <= 10^5Example1输入:target = 9输出:[[2,3,4],[4,5]]Example2输入:target = 15输出:[[1,2,3,4,5...原创 2020-03-06 19:19:26 · 172 阅读 · 0 评论 -
面试题26. 树的子结构
Problem输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)B是A的子结构, 即 A中有出现和B相同的结构和节点值。Example1输入:A = [1,2,3], B = [3,1]输出:falseExample2输入:A = [3,4,5,1,2], B = [4,1]输出:trueSolution这道题一开始没想明白,其实还是常规的...原创 2020-03-05 20:37:30 · 222 阅读 · 1 评论 -
226. Invert Binary Tree && 面试题27. 二叉树的镜像
ProblemInvert a binary tree.ExampleInputOutputSolutionSolution1/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *...原创 2020-02-27 20:48:40 · 166 阅读 · 0 评论 -
101. Symmetric Tree && 面试题28. 对称的二叉树(Leetcode每日一题-2020.05.31)
ProblemGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).Example1For example, this binary tree [1,2,2,3,4,4,3] is symmetric:Example2But the following ...原创 2020-02-27 20:36:19 · 169 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree && 面试题55 - I. 二叉树的深度 LCOF
ProblemGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Note: A leaf is a node with no c...原创 2020-02-26 23:04:33 · 172 阅读 · 0 评论 -
110. Balanced Binary Tree && 面试题 04.04. Check Balance LCCI && 面试题55 - II. 平衡二叉树
ProblemImplement 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 nev...原创 2020-02-25 23:23:14 · 245 阅读 · 0 评论 -
235. Lowest Common Ancestor of a Binary Search Tree&&面试题68 - I. 二叉搜索树的最近公共祖先
ProblemGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined b...原创 2020-02-24 23:16:08 · 191 阅读 · 0 评论 -
141. Linked List Cycle & 142. Linked List Cycle II
步骤1:判定是否有环慢指针sp,每次前进一步快指针fp,每次前进两步如果慢指针与快指针在前进n次之后指向相同的位置,则存在环;否则,如果fp->next为NULL或fp->next->next为NULL,则不存在环。步骤2:如果存在环,返回环的入口结点 设链表的头结点为X,还的入口结点为Y,快慢指针第一次相遇的点为Z。XY之间的长度为a,YZ之间的长度为b,ZY之间的长度为c。 第原创 2016-02-17 10:55:50 · 375 阅读 · 0 评论 -
82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1-原创 2015-06-29 21:01:20 · 860 阅读 · 0 评论 -
字符串的排列
题目描述输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。 输入描述: 输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。解答求字符串的全排列,偷懒用了库函数class Solution {public: vector原创 2016-02-29 21:22:52 · 255 阅读 · 0 评论 -
138. Copy List with Random Pointer&复杂链表的复制
题目描述输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点)。解答暴力法 可以把复制过程分为两步: 1. 复制原始链表上的每个结点,并用next连接起来 2. 设置每个结点的random指针。 假设原始链表中的某个结点N的random指向结点S,由于S的位置在链表中可能在N的前面,也可能在N的后面,所以要定位S的位置需要从原始链表的原创 2016-02-29 16:17:52 · 457 阅读 · 0 评论 -
295. Find Median from Data Stream&数据流中的中位数
题目描述如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。解答 如上图所示,如果数据在容器中已经排序,那么中位数可以由和指向的数得到。如果容器中数据的个数是奇数,那么和 指向同一个数据。 注意到,整个容器被分隔成了两部分。位于容器左边部分的数据比右边的数原创 2016-03-14 09:25:14 · 2105 阅读 · 0 评论 -
113.Path Sum II&二叉树中和为某一值的路径
程序员面试题精选100题(04)-二元树中和为某一值的所有路径[数据结构] /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x原创 2014-03-19 09:57:49 · 672 阅读 · 0 评论 -
栈的压入、弹出序列
题目描述输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。解答 以4,5,3,2,1为例,分析压栈和弹出的过程。第一个希望被弹出的数字是4,因此4需要先压入到辅助栈中。压入栈的顺序由压栈原创 2016-02-29 09:32:01 · 377 阅读 · 0 评论 -
153. Find Minimum in Rotated Sorted Array & 旋转数组的最小数字
题目描述把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减序列的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。解答 最直观的解法从头到尾遍历整个数组,找到最小元素。这个解法的时间复杂度是O(n)。 注意到,旋转之后的数组实际上可以划分为两个排序的子数组,且前面的子数组的元素都大于或者等于后原创 2016-02-24 20:15:26 · 490 阅读 · 0 评论 -
206. Reverse Linked List&反转链表
Reverse a singly linked list./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solu原创 2015-05-05 11:48:02 · 561 阅读 · 0 评论 -
8. String to Integer (atoi)
mplement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cas原创 2015-06-04 11:14:29 · 591 阅读 · 0 评论 -
105\106. Construct Binary Tree from Inorder and Postorder\Preorder Traversal&重建二叉树
ProblemGiven inorder and postorder traversal of a tree, construct the binary tree. 剑指offer中是给出二叉树的前序和中序遍历的结果,重建二叉树。Solution两题的思路是一样的。这里给出剑指offer中的分析。 在二叉树的前序遍历序列中,第一个数字总是树的根节点的值。在中序遍历序列中,根节点的值在序列的中原创 2016-05-04 10:25:55 · 384 阅读 · 0 评论 -
50.Pow(x, n)&数值的整数次方
Implement pow(x, n).分治法class Solution {public: double pow(double x, int n) { if(n < 0) return 1.0/power(x,-n); else return power(x,n); }原创 2014-03-12 16:42:46 · 668 阅读 · 0 评论 -
263. Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2015-11-29 22:37:49 · 596 阅读 · 0 评论 -
74.Search a 2D Matrix&240. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each原创 2015-06-27 13:58:20 · 557 阅读 · 0 评论 -
53.Maximum Subarray&连续子数组的最大和
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has原创 2015-06-25 09:04:37 · 1387 阅读 · 0 评论