[Q4] 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)).

You may assume nums1 and nums2 cannot be both empty.

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 Solution: 按从小到大的顺序依次遍历两个数组,直至中间位置停止,此时: (1)当两个数组长度N1+N2为偶数时,取(中间数+前一数)/2
(2)当N1+N2为奇数时,取当前位置的数
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
""" i,j,idx = 0,0,0
m,n = len(nums1),len(nums2)
N = m+n
prv, cur = None, None while idx<N:
if i<m and j<n and nums1[i]<nums2[j]:
cur = nums1[i]
i = i+1
elif i<m and j<n and nums1[i]>=nums2[j]:
cur = nums2[j]
j = j+1
elif i>=m:
cur = nums2[j]
j = j+1
elif j>=n:
cur = nums1[i]
i = i+1
if N%2 == 0 and idx == N/2: # if m+n is even
return (cur+prv)/2
elif N%2 == 1 and idx == N//2: # floor division
return cur
prv = cur
idx = idx+1

[Q5]  找最长回文序列

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb" Solution:遍历中心,从中心依次向两端迭代
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
maxl = 0
i=0
left, right = 0,0
maxs = ""
while i<len(s): # center location
left = right = i
while left>=0 and right<len(s) and s[left]==s[right]:
left = left-1
right = right+1
tem = s[left+1:right]
if len(tem)>len(maxs):
maxs = tem
left, right = i,i+1
while left>=0 and right<len(s) and s[left]==s[right]:
left = left-1
right = right+1
tem = s[left+1:right]
if len(tem)>len(maxs):
maxs = tem
i = i+1
return maxs

[Q6] 解法很棒

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y I R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation: P I N
A L S I G
Y A H R
P I Solution: https://leetcode.com/problems/zigzag-conversion/discuss/3404/Python-O(n)-Solution-in-96ms-(99.43)
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
""" if len(s)<numRows or numRows==1:
return s idx = 0
step = 1
L = ['']*numRows
for x in s:
if idx==0:
step = 1
elif idx==numRows - 1:
step = -1
L[idx] += x
idx = idx+step
return ''.join(L)

重点:

(1)L的初始化,L为一个3行字符串数组

(2)引入Step,相当于把每一行的数直接压缩到同一行的唯一字符串内

(3)join函数:‘sep’.join(seq) 即将seq字符串去sep后连接

【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion的更多相关文章

  1. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

  2. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  3. 【leetcode刷题笔记】Median of Two Sorted Arrays

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  4. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  5. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  6. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  7. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  8. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  9. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

随机推荐

  1. 解决win7远程桌面连接时发生身份验证错误的方法

    远程桌面连接,是我们比较常用的一个功能了,但有时突然不能用了,以下是我遇到该问题,并解决该问题的方法.连接时报的是“发生身份验证错误,要求的函数不受支持”,解决之后细想一下,该问题好像是在我在电脑上安 ...

  2. 1260. [CQOI2007]涂色【区间DP】

    Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符串表示这个目标:RGBGR. 每次你可以把一段连续 ...

  3. Lambda表达式和For循环使用需要注意的一个地方

    一个需要注意的地方看下面的代码: using System; using System.Collections.Generic; using System.Linq; namespace MyCsSt ...

  4. criterions的选择

    criterions分为几类,其中有classification criterions与regression criterions.classification criterions是针对离散的,re ...

  5. Python之Pulsar框架使用

    本文内容主要包含Pulsar的介绍和安装.初步使用.应用.常见示例等. 一. 介绍和安装 Pulsar是Python事件驱动并发框架:Pulsar具有高扩展性.高可用性的框架,它能够基于事件驱动的开源 ...

  6. [原创]关于在VS解决方案下使用文件夹管理多个项目层次关系的说明

    由于所创建的应用项目或类库项目较多,于是将这些类库放到一个文件夹下.在VS解决方案下确实能看到一个文件夹下多个类库项目这种层次关系.如下图所示: 但打开“我的电脑”,看到的只有类库,并未看到维护层次关 ...

  7. package.xml

    package.xml 也是一个catkin的package必备文件, 它是这个软件包的描述文件, 在较早的ROS版本(rosbuild编译系统)中, 这个文件叫做 manifest.xml , 用于 ...

  8. 源码编译安装mysql-boost-5.7.16.tar.gz报错分析处理

    Plugin 'FEDERATED' is disabled.  mysqld: Table 'mysql.plugin' doesn't exist  [ERROR] Can't open the ...

  9. Msys/MinGW与Cygwin/gcc

    一. MinGW MinGW 官方网站为 http://www.mingw.org/ MinGW,即 Minimalist GNU For Windows(GCC compiler suite).它是 ...

  10. C#中 DateTime , DateTime2 ,DateTimeOffset 之间的小区别 (转载)

    闲来无事列了个表比对一下这3兄弟之间还是有一点差距的╮(╯_╰)╭   DateTime DateTime2 DateTimeOffset 日期范围 1753-01-01到 9999-12-31 00 ...