周刷题第二期总结(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 ...
随机推荐
- 深入理解Java中的final关键字(转)
文章转自http://www.importnew.com/7553.html Java中的final关键字非常重要,它可以应用于类.方法以及变量.这篇文章中我将带你看看什么是final关键字?将变量, ...
- MATLAB——神经网络train函数
- 个人css编写规范
前言:最近在做微信小程序,因为公司小,就我一个人弄前端的东西,js和页面都是我来弄,结果那天后台的人看到我的js代码,说我的代码写得不规范,函数什么的都很乱,弄得我羞愧难当,幸亏没看我的css,其实我 ...
- centos7搭建elasticsearch
Elasticsearch:负责日志检索和分析,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等 Logstash:对日志进行收集.过 ...
- Crowdsourcing[智能辅助标注]
为了实现标注平台智能辅助标注的能力,即上传一个标注任务,开始不提供辅助任务,随着用户标注的进行,后台可以收集一部分的标记数据,然后开启模型训练,并接着提供模型服务功能.然后再收集数据,再不断的训练,然 ...
- Redis详解(六)------ RDB 持久化
前面我们说过,Redis 相对于 Memcache 等其他的缓存产品,有一个比较明显的优势就是 Redis 不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,has ...
- python---pandas.merge使用
merge 函数参数 ”’ merge: 合并数据集, 通过left, right确定连接字段,默认是两个数据集相同的字段 参数 说明 left 参与合并的左侧DataFrame right 参与合并 ...
- Luogu3067 平衡的奶牛群 Meet in the middle
题意:给出$N$个范围在$[1,10^8]$内的整数,问有多少种取数方案使得取出来的数能够分成两个和相等的集合.$N \leq 20$ 发现爆搜是$O(3^N)$的,所以考虑双向搜索. 先把前$3^\ ...
- CF1060D Social Circle 排序
题目传送门:http://codeforces.com/problemset/problem/1060/D 题意:有$N$个人,你要让他们坐成若干个圆环.他们每个人需要坐一把椅子,左手边至少要有$l_ ...
- 记一次用WPScan辅助渗透WordPress站点
记一次用WPScan辅助渗透WordPress站点 一.什么是WPScan? WPScan 是一个扫描 WordPress 漏洞的黑盒子扫描器,它可以为所有 Web 开发人员扫描 WordPress ...