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

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. MP实战系列(五)之封装方法讲解

    mybatis plus封装的方法怎么用?以及它们对应的sql是那些sql?及其什么情况用? 这些需要说下,以下我将会将我常用的说下,不是常用的,可能提以下或者不提. 根据主键查询 UserEntit ...

  2. webpack简单的打包体验

    怎么使用webpack 进行打包 需要使用管理员权限进行安装 npm install webpack -g npm install webpack-cli -g 创建站点 mkdir webpack ...

  3. centos7 rpm 安装mysql

    1.wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar 2.tar ...

  4. centos时区设置

    [root@ logs]# tzselect Please identify a location so that time zone rules can be set correctly.Pleas ...

  5. [转]VC++宏与预处理使用方法总结

    原文链接:VC 宏与预处理使用方法总结 原文链接:VC预处理指令与宏定义的妙用

  6. labellmg使用方法

    https://www.cnblogs.com/Terrypython/p/9577657.html

  7. OpenStack keystone节点搭建(官方2018年4月份文档)

    参考文档:https://docs.openstack.org/install-guide/common/conventions.html https://docs.openstack.org/mit ...

  8. Winform 基础一 panel

    一 居上.居中.居下 二 添加子控件 三 适应不同分辨率 四 内容超出,显示滚动条 一 .居上.居中.居下 二.添加子页面 Form7 childFrm = new Form7(); childFrm ...

  9. 【Codeforces 1120C】Compress String

    Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...

  10. 开发板测试-GPRS

    注意事项: 经过测试,为了使STM32和Air202的串口稳定通信,需要更改 以前电路感觉应该是三极管控制极4.7K电阻太大,又因为开关速度快,然后开关的时候导致不足以让三极管处于全部导通状态,所以电 ...