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

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. day15 Python函数递归,轻易不要用递归,容易搞出来内存溢出

    古之欲明明德于天下者,先治其国:欲治其国者,先齐其家:欲齐其家者,先修其身:欲修其身者,先正其心:欲正其心者,先诚其意:欲诚其意者,先致其知,致知在格物.物格而后知至,知至而后意诚,意诚而后心正,心正 ...

  2. navicate使用小技巧

    以下效果是按住alt+按住鼠标左键,往左往右移动,即可

  3. pyspider安装完启动报错【connect to scheduler rpc error: error(111, 'Connection refused')】

    调研爬虫框架pyspider有一段时间了,感觉已经比较成熟,跟老板申请了一台机器,打算搭一套正式环境.然而在熟悉的安装步骤过后,启动画面却是这样的: 虽然心中有疑惑,但还是熟练的百度起来.一番搜索之后 ...

  4. centos 7 安装python3.5.1

    系统: [root@localhost ~]# cat /etc/centos-release CentOS Linux release 7.2.1511 (Core) 内核: [root@local ...

  5. linux 用xshell工具远程登录

    1.设置linux,获取ip,登录名.密码 2.xshell登录

  6. node.js 基础三 消息推送

  7. AS导入一个工程出现Error:please select Android SDK的错误

    导入一个新的工程出现:Error:please select Android SDK 的错误 现象描述:点击运行程序按钮,弹出一个“Edit Configure”的对话框,最下面报:Error:ple ...

  8. OpenStack报错:MessagingTimeout: Timed out waiting for a reply to message ID

    L3.agent中出现大量消息超时错误,对网络的操作各种异常. 报错如下: -- :: ERROR neutron.agent.l3.agent [req-db9207e6--4f23-8c19-0d ...

  9. linux shell的here document用法

    转载自: http://my.oschina.net/u/1032146/blog/146941 什么是Here Document?Here Document 是在Linux Shell 中的一种特殊 ...

  10. 【译】高级指南-深入JSX

    title: 高级指南-深入JSX date: 2017-4-5 17:13:09 --- 深入JSX 从根本上来讲,JSX 仅仅是提供 React.createElement(component, ...