周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)
这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标。
Lonest Substring Without Repeating Characters:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", 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.
首先这是个中等难度的题,拿到这个题我最先想到的是做一根指针在头部,当遇到第二个与头部指针指向的字符相同的时候就将指针向前拨。当时我想尽可能将问题简化成两个相同字符之间的不一样字符长度的问题。后来仔细想了一下发现,大方向倒不是有很大的错误,但是边界条件很多,包括连续遇到两个相同字符时候需要做的处理,没有两个相同字符需要做的处理等。试了一下自己的想法,写了代码下面贴上ac代码:
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
longest = head = 0
visited = [False for _ in xrange(256)]
for index, char in enumerate(s):
if visited[ord(char)]:
while s[head] != char:
visited[ord(s[head])] = False
head += 1
head += 1
else:
visited[ord(char)] = True longest = max(longest, index - head + 1)
return longest
下面还是再次讲解一下正确的解题的思路。
1. 首先比较难懂的就是初始化了一个ascii表256个字符的东西,这个东西保证每个可能会出现的字符都可以被记载上,一旦出现就将其置为True。当然你也可以使用字典实现这个功能,这里就不赘述了。
2. 然后开启循环,依次对字符串进行遍历。
3. 看下当前char对应的ascii表里的数字为止的索引有没有被出现过。
4. 如果有出现过,那么开启循环并判断头部位置的字符是否等于循环的字符。这里提一下为什么要做这个判断。因为在判断字符串的时候可能出现连续的两个相同的字符,比如'bba','bba'进入循环的时候head地方和循环到的char将会是相等的所以直接让head+1 这个时候head是1 index是1 然后做longest计算的时候就可以得到正确的1位的答案。
5. 如果有第二次出现的字符,且head对应位置的字符与char不相等是什么情况呢。例如'bapoiubcbd' 当遇到第二个b的时候会进入循环,这个时候head指针指向b于是head开始指向a。到这里为止都很正常,但是马上紧接着出现了第三个b这个时候我们在此进入循环,这个时候我们要做的是将指针一口气移动到第二个b的位置,并且清楚中间所有出现过的字符,为什么?开始介绍这个道题的时候就已经说了,这道理自习分析一下,其实就是在找两个相同字符之间的最大长度(除开边界条件抽象此题的话)。所以在第三个b出现的时候,应该移动指针到第二个b处。并且将中间的过掉的字符都重新置为没有访问过,这样在下面才可以进行第二次计算longest的长度,用来和出现过的最长longest比较。
这道题其他就没有什么好说的了。
Median of Two Sorted Arrays:
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]
nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
此题是一个求中位数的问题,我拿到题的时候看了一下难度hard有点奇怪,正常解不是特别复杂,但是题目里要求了时间复杂度,可能这才是hard的关键吧。我拿到这题的时候思考了一下,因为两个给予的数组都是排过序的,但是我没想太多。其实如果用python自带排序函数sorted的话,可以将两个数组合并,然后进行排序。之后转化成了一个求中位数问题,区分一下奇偶,最后可以ac。可见sorted函数的排序非常高效。
我看网上也有说用找kth来解决的,我看了找kth的代码觉得没必要这样做?因为这题降低了难度在的是给出了两个有序数组。这样的话我们可以使用归并排序O(n)的算法复杂性将其合并为一个有序数组,然后再通过奇偶输出不同的中位数方法来解决这个问题。
class Solution(object):
def merge_sort(self, num1, num2):
m = n = 0
c = []
while m < len(num1) and n < len(num2):
if num1[m] < num2[n]:
c.append(num1[m])
m += 1
else:
c.append(num2[n])
n += 1
if m == len(num1):
for i in num2[n:]:
c.append(i)
else:
for i in num1[m:]:
c.append(i) return c def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
nums3 = self.merge_sort(nums1, nums2)
if len(nums3) % 2 == 1:
middle = nums3[len(nums3)/2]
else:
middle = (nums3[len(nums3)/2] + nums3[len(nums3)/2-1])/2.0 return middle
以上。
周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)的更多相关文章
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- 【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 刷题3. Longest Substring Without Repeating Characters
一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- leetcode-【中等题】3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
随机推荐
- MetaMask/metamask-extension/mascara 的运行实现
https://github.com/MetaMask/metamask-extension/tree/develop/mascara 找了很多个实例,基本上很少是不使用线上钱包的,只有metamas ...
- python3 day01 大纲
1. 简介python 龟叔 89年 人工智能 2. 特点 优点: 简单, 明确, 优雅,跨平台 缺点: 慢 解释型编程语言 分类: 解释型: 一行一行的把代码进行翻译. 执行效率比较低 优势: 跨平 ...
- node.js如何将远程的文件下载到本地、解压、读取
其实要解决的问题,很简单,获取远程文件,然后解压到本地读取. 在vscode中通过node.js来实现是比较方便的,相比之前的zip.js,我觉得我还是比较喜欢node.js实现方式. test.js ...
- 最小生成树(图论)--3366lg【模版】
题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<= ...
- matlab sign函数用法及实例
在MATLAB科学计算过程当中,我们经常需要对我们的计算公式或者计算结果检验其符号,,sign函数就给我们提供了这种方便,下面就通过实例介绍一下matlab sign函数 的用法,希望能够给您带来帮助 ...
- MATLAB常用快捷键命令总结
1. 在命令窗口(Command Window)中: 1)[↑.↓]——切换到之前.之后运行过的命令,可以重复按多次来达到你想要的命令: 2)[Tab]——自动补全.在command窗口,输入一个命令 ...
- Django rest framework集成微博第三方登录
Django restframework 集成第三方登录(微博.微信.QQ等) 友情链接 python-social-auth-app官方文档 微博开放者平台 QQ开放者平台 准备工作 1.注册微博开 ...
- ESP32 电容式触摸按键设计
手指和电容器接触时,相当于增加了电容,电容增加量与总电容的商就是电容的变化幅值,如果这个幅值超过门限,就认为触摸按键被激发了:
- Omi框架学习之旅 - 生命周期 及原理说明
生命周期 name avatars company constructor 构造函数 new的时候 install 初始化安装,这可以拿到用户传进的data进行处理 实例化 installed 安装完 ...
- 分析网络流量Capsa笔记
Capsa是一款网络分析仪,允许您监控网络流量,解决网络问题并分析数据包.通过提供生动的图表,通过设计良好的GUI提供丰富的统计信息和实时警报,Capsa可让IT管理员实时识别,诊断和解决有线和无线网 ...