Press "Enter" to skip to content

Huahua's Tech Road

花花酱 LeetCode 3. Longest Substring Without Repeating Characters

Problem

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

Solution: HashTable + Sliding Window

Using a hashtable to remember the last index of every char.  And keep tracking the starting point of a valid substring.

start = max(start, last[s[i]] + 1)

ans = max(ans, i – start + 1)

Time complexity: O(n)

Space complexity: O(128)

C++

Java

Python3

花花酱 LeetCode 668. Kth Smallest Number in Multiplication Table

Problem

Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number quickly from the multiplication table?

Given the height m and the length n of a m * n Multiplication Table, and a positive integer k, you need to return the k-th smallest number in this table.

Example 1:

Input: m = 3, n = 3, k = 5
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6
3	6	9

The 5-th smallest number is 3 (1, 2, 2, 3, 3).

Example 2:

Input: m = 2, n = 3, k = 6
Output: 
Explanation: 
The Multiplication Table:
1	2	3
2	4	6

The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6).

Note:

  1. The m and n will be in the range [1, 30000].
  2. The k will be in the range [1, m * n]

 

Solution 1: Nth element (MLE)

Time complexity: O(mn)

Space complexity: O(mn)

C++

Solution2 : Binary Search

Find first x such that there are k elements less or equal to x in the table.

Time complexity: O(m*log(m*n))

Space complexity: O(1)

C++

Java

Python3

Related Problems

[反面教材] 我是如何快速搭建一个公众号机器人的

声明:本文可以作为快速开发的经典反面教材,请勿学习,不然后果自负。

说来惭愧,公众号虽然开了有一年多,但没怎么发过东西。由于众所不知的原因,文章中不能有第三方视频/链接。要插入视频的话必须上传到腾讯视频,那真叫一个蛋腾,所以就放弃治疗了。真的对不住大家了!

直到昨天晚上,在手工编辑关键字自动回复的时候,无意中发现自动回复居然可以支持任意URL的超链接。这一下子激动得我就像发现了新大陆一样!赶紧加了几个按照leetcode题目编号自动回复视频/博客链接的关键字。我靠,这得手工编辑到什么时候啊?我怎么说也是一个堂堂正正的程序员好吗,怎么能干手工编辑这种事情呢(虽然我一直在干)?

自动回复,自动回复,自动回复!按照题目编号回复视频链接!说干就干!我路子野起来,连我的MacBook Pro都害怕。

其实流程还是比较清楚的,如下图:

自动回复流程图

数据源:我有一张Google Spreadsheet记录了所有的视频,网站链接。(就是上文提到的手工编辑的)【DONE】

数据源:http://t.cn/RTcVHnY

数据处理:这对于常年参加Kaggle比赛,总是1000名开外的我来说自然也不是什么难事。Google Spreadsheet发布成csv格式,这样处理起来方便些。写个python脚本从数据源生成一个JSON文件,key就是题目编号,value就是回复正文。【DONE】

数据处理

配置服务器:

首先你得有一台服务器。我博客的虚拟主机正好可以来拿当炮灰。【DONE】

官文的例子是用Python开发的,需要80/443端口,已经被Apache占用了【放弃治疗】

看来只能改用PHP了,我好歹也写过几百行PHP好吗?PHP:精通。一直保持在 echo “Hello” . ” World!”; 的巅峰水平,从未被超越过。【DONE】

我不是有WordPress在跑这么,直接用它不就完了么?找了几个插件,不是PHP版本/环境要求太高,就是2年多没更新了。还是自己写吧,但怎么插入自己的PHP代码呢?好像没那么简单。如果用SSH/FTP上传倒是可以,就是太麻烦了【放弃治疗】。

对了,其实我可以借壳上市。找一个不用的插件,把它的代码替换了不就行了么。再把readme.txt替换成之前生成的JSON文件。直接在WordPress中编辑就行了。【DONE】

服务器设定,指向那个炮灰插件。

实现业务逻辑:

官方的开发文档实在是无力吐槽,盲人摸象吧。接口测试功能倒是不错。

每次收到消息,读取readme.txt(此处应该有缓存),看看content是不是key,如果是的话就返回value。就是这么简单。【DONE】

服务器代码

夜深了,是时候和自己写的机器人进行促膝长谈了。【DONE】

最后的效果

打赏专用二维码

后记:

  1. 坑太多了,前前后后一共花了3个多小时。
  2. 数据源还有很多NaN正在手工编辑中。
  3. 大家千万不要学习我这种野路子风格,数据更新什么的还是需要手动,一点都不程序员。
  4. 准备加入更多功能,比如特辑/播放列表的关键字回复。
  5. 对我的机器人温柔一点。