JinFei's Blog

Thinking will not overcome fear but action will.

[LeetCode]33. Search in Rotated Sorted Array

二分查找,旋转数组

题目描述 Suppose 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]). You are given a target value to search...

[LeetCode]448. Find All Numbers Disappeared in an Array

排序

题目描述 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this ...

[LeetCode]94. Binary Tree Inorder Traversal

树的递归,非递归遍历

题目描述 Given a binary tree, return the inorder traversal of its nodes’ values. 递归解题思路 树的遍历 递归版本比较简单 先序遍历 -》 根左右 中序遍历 -》 左根右 后序遍历 -》 左右根 递归遍历的时候查看当前节点是否为空即可,如果不为空,按照上述规则进行遍历 递归版本 ...

[LeetCode]350. Intersection of Two Arrays II

数组的并集

题目描述 Given two arrays, write a function to compute their intersection. Example1: Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2,2] Example2: Input: nums1 = [4,9,5], nums2 = [9,4,...

[LeetCode]69. x 的平方根

二分搜索

题目描述 实现 int sqrt(int x) 函数。 计算并返回 x 的平方根,其中 x 是非负整数。 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 Example1: 输入: 4 输出: 2 Example2: 输入: 8 输出: 2 说明: 8 的平方根是 2.82842…, 由于返回类型是整数,小数部分将被舍去。 递归版本...

[LeetCode]46. Permutations

字符串的排列

题目描述 Given a collection of distinct integers, return all possible permutations. Example1: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]...

[LeetCode]56. Merge Intervals

合并数组

题目描述 Given a collection of intervals, merge all overlapping intervals. Example1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] a...

[LeetCode]39. Combination Sum

backtracking

题目描述 Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. The ...

[剑指Offer]删除链表中重复的结点

删除链表中的重复节点

题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5 解题思路 这道题的难点是,要把所有重复的指针都给删除,而不是保留一个(所以需要一个当前节点的头一个节点pre) 这里需要使用一个头指针,来表...

[剑指Offer]二叉搜索树的第k个结点

二叉搜索树、中序遍历

题目描述 给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8)中,按结点数值大小顺序第三小结点的值为4。 解题思路 首先建树 中序遍历 每次遍历后进入序列 得到的序列第k个即为所求 注意异常,比如k的size问题,空指针问题 /* struct TreeNode { int val; struct Tree...