这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标。

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)的更多相关文章

  1. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  2. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  3. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

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

  4. 刷题3. Longest Substring Without Repeating Characters

    一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...

  5. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

  6. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

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

  7. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  8. leetcode-【中等题】3. Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  9. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

随机推荐

  1. ECS简述

    一.查看ECS实例 使用场景 •实例的日常维护 •实例运行状态和详细信息的查看 二.启动ECS实例 使用场景 •实例停止运行之后的再次启动 三.停止.重启ECS实例 使用场景 •实例运行的服务暂停时停 ...

  2. powerDesigner创建类及数据模型

    1.创建类图 / 2.创建类图的属性 3.创建一个类,这个是创建后的对应关系,可以给其他的地方引用 3.创建类时候的属性 name 和 code 取消关联 4.创建两个类的关系 这里有个科普知识(ht ...

  3. P1006 传纸条-洛谷luogu-dp动态规划

    题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个mm行nn列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运 ...

  4. MATLAB——神经网络构造线性层函数linearlayer

    % example5_7.m x=-:; y=*x-; randn(); % 设置种子,便于重复执行 y=y+randn(,length(y))*1.5; % 加入噪声的直线 plot(x,y,'o' ...

  5. ubuntu14.04下播放器SMplayer的安装

    1. Mplayer 与 SMplayer的区别 虽然MPlayer播放器是人类史上最强大的播放器(参数超过千个),但是其默认编译没有界面,所以写参数时间甚至比看片时间还长.虽然编译时候可以选择--e ...

  6. linux gcc nginx

    1.安装GCC[root@rekfan.com opt]# rpm -ivh cpp-4.1.2-48.el5.i386.rpm[root@rekfan.com opt]# rpm -ivh kern ...

  7. 汇编 STOSB, STOSW, STOSD指令

    知识点: 汇编指令 STOSB STOSW STOSD 一.汇编指令STOSB 004113AC 8DBD B4FEFFFF LEA EDI,DWORD PTR SS:[EBP-14C] 004113 ...

  8. Verilog对数据进行四舍五入(round)与饱和(saturation)截位

    转自https://www.cnblogs.com/liujinggang/p/10549095.html 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件 ...

  9. [已解决]An unhandled exception occurred while processing the request.

    An unhandled exception occurred while processing the request. InvalidOperationException: The layout ...

  10. Bash : 冒泡排序

    冒泡排序是非常基础的排序算法,本文我们看看在 Bash 脚本中如何写冒泡排序.本文的演示环境为 ubuntu 16.04. 冒泡排序的简要描述如下: 通过连续的比较对数组中的元素进行排序 比较两个相邻 ...