Tag: 算法

Leetcode 295. Find Median from Data Stream

No Picture

题目链接295. Find Median from Data Stream   在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。

Read More »

Leetcode 236. Lowest Common Ancestor of a Binary Tree

No Picture

题目链接 236. Lowest Common Ancestor of a Binary Tree   根据LCA的定义,二叉树中最小公共祖先就是两个节点p和q最近的共同祖先节点,LCA的定义没什么好解释的,主要是这道题的解法。

Read More »

Leetcode 516. Longest Palindromic Subsequence

No Picture

题目链接 Given a string s, find the longest palindromic subsequence’s length in s. You may assume that the maximum length of s is 1000.

Read More »

Leetcode 582. Kill Process

No Picture

Leetcode 582. Kill Process   好久没刷题,今天来一道比较简单的题目,如果此题作为一道面试题,可以延伸出树的遍历,栈和队列,hashmap,treemap等,还是比较能考验基础的面试题。

Read More »

Leetcode 114. Flatten Binary Tree to Linked List

No Picture

Leetcode 114. Flatten Binary Tree to Linked List   题目意思很简单,就是把一棵二叉数转换为链表,虽然题目中没说以什么样的形式转换,但看下样例就很容易看出来,是以先序遍历的次序转换成链表。这里链表节点还是treenode,只不过它的左节点为空而已。

Read More »

Leetcode 467. Unique Substrings in Wraparound String

No Picture

题目链接:Unique Substrings in Wraparound String 这里加段英文,不是为了凑字数,而是为了让别人搜索题目的时候能搜到我的博客。。 Consider the string s to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz”, so s will look like this: “…zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd….”.

Read More »

Leetcode 368. Largest Divisible Subset

No Picture

题目链接:368. Largest Divisible Subset Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0….

Read More »

求无序数组排序后相邻俩数最大差值(思路及详解)

No Picture

  前两天在一个学长面试的时候遇到这样一个题,这里稍微详细说下本文的标题。给你n个任意整数,求排序后相邻两个数之间的最大差值,这里n可能有10^5,整数为任意32位整型。要求求解算法的时间复杂度为O(n)。

Read More »

Leetcode 377. Combination Sum IV

No Picture

题目链接:Combination Sum IV Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Read More »

Leetcode 240. Search a 2D Matrix II

No Picture

题目链接: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 in ascending from left to right….

Read More »