Pylon's Blog


  • 首页

  • 分类

  • 归档

  • 标签

  • 关于作者

  • 搜索

Leetcode-5 Longest palindromic substring

发表于 2019-08-08 | 分类于 Fundamental , Algorithm

题目描述

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

1
2
3
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

1
2
Input: "cbbd"
Output: "bb"
阅读全文 »

Leetcode-739 Daily Temperatures

发表于 2019-08-04 | 分类于 Fundamental , Algorithm

Description

Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.

For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Note: The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].

分析

给定一个数组代表每天的气温,查找多少天之后天气比今天更暖和。首先,想到了暴力算法,如下,

  1. 遍历 T
  2. 对于第 i 个元素,另 j = i,从 i 开始往后寻找比 T[i] 大的元素,如果找到则 j - i 为第 i 号元素的目标值
  3. 重复步骤 2 直到最后一位

给定数组为降序排列时,暴力算法复杂度为 O(n^2),数组的长度在 [1, 30000],暴力算法可能会超时。迫于没有想到更好的解题思路,只能对暴力算法先做优化,如果数组是升序的,算法复杂都是 O(n),所以很乐观,主要考虑优化降序的部分。

考虑如下情况,数组先增后降,

下降转折点到末尾数组元素单调递减,因此每个元素后面都不会有大于他们的元素

如果单调递减后有转折点,那么单调递减的部分元素可能会存在

Leetcode-1143 Longest Common Subsequence

发表于 2019-08-02 | 分类于 Fundamental , Algorithm

Description

Given two strings text1 and text2, return the length of their longest common subsequence.

A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, “ace” is a subsequence of “abcde” while “aec” is not). A common subsequence of two strings is a subsequence that is common to both strings.

If there is no common subsequence, return 0.

阅读全文 »

Leetcode-202 Happy Number

发表于 2019-07-31 | 分类于 Fundamental , Algorithm

Description

Write an algorithm to determine if a number is “happy”.

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

阅读全文 »

Leetcode-898 Add to Array-Form of Integer

发表于 2019-07-23 | 分类于 Fundamental , Algorithm

Description

For a non-negative integer X, the array-form of X is an array of its digits in left to right order. For example, if X = 1231, then the array form is [1,2,3,1].

Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.

阅读全文 »

Leetcode-771 Jewels and Stones

发表于 2019-07-22 | 分类于 Fundamental , Algorithm

Description

You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

阅读全文 »

Leetcode-617 Merge Two Binary Trees

发表于 2019-07-22 | 分类于 Fundamental , Algorithm

Description

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

阅读全文 »

Leetcode-121 Best Time to Buy and Sell Stock

发表于 2019-07-12 | 分类于 Fundamental , Algorithm

Description

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

阅读全文 »

Leetcode-435 non-overlapping intervals

发表于 2019-07-10 | 分类于 Fundamental , Algorithm

Description

Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Note:

  1. You may assume the interval’s end point is always bigger than its start point.
  2. Intervals like [1,2] and [2,3] have borders “touching” but they don’t overlap each other.
阅读全文 »

Leetcode-101 —— Symmetric Tree

发表于 2019-07-10 | 分类于 Fundamental , Algorithm

Description

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 symmetric:

1
2
3
4
5
    1
/ \
2 2
/ \ / \
3 4 4 3
阅读全文 »
1…345…9
Pylon, Syncher

Pylon, Syncher

85 日志
14 分类
86 标签
GitHub GMail LeetCode
友情链接
  • 东阳兄
© 2023 Pylon, Syncher
由 Hexo 强力驱动
Hosted by GitHub && Coding.net
主题 - NexT.Mist