解题思路

来源:互联网 发布:计算机 数学 知乎 编辑:程序博客网 时间:2024/05/03 02:31

3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

为每个字母出现的位置建表,记录某个字母上次出现的位置。

建立数组char_position,记录每个不同的char在字符串s中出现的位置,令c = s[i],则i - s[i]就是以该字母为结尾的最长无重复字符串的长度。比较该长度和记录的最大长度, 并进行相应的更新。依次扫描字符串的每个字母,最终得到无重复字母的子串的最大长度,即为结果。
复杂度为O(n)


4. 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)).

使用一个更一般的方法解决该问题 —— 二分法查找两个有序数组中第k个最小的数。

设len(nums1) < len(nums2),0-indexed,
i1=min{len(nums1)1,k/2}, i2=ki1

if nums1[i1] < nums2[i2]         we can safely discard nums1[0..i1] (why?),    k = k - i1, recursively find k-th smallest.else if nums1[i1] > nums2[i2]    discard nums2[0..i2],    k = k - i2, recursively find k-th smalles.else (nums1[i1] == nums2[i2])    the k-th smallest number is founded.

该算法复杂度为O(logk)

75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

见Dutch national flag problem,使用Dijkstra’s 3-way partition algorithm.

179. Largest Number

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

自定义排序关系(对应Java中的 customized comparator/comparable).
将数组中的每个数都转为一个字符串,得到字符数组A。
对于字符串s1和s2,令:
若s1 + s2 < s2 + s1,则s1 < s2。
(+代表字符串连接)
该可知关系为全序关系。用该关系对字符数组A排序,将A中元素由大到小依次连接即可。

209. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum s. If there isn’t one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.

More practice:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).

Note: subsequence, subset, substring, subarray.
其中,substring, subarray必须是连续的(contiguous).
另附:

1、consistent是指行为/态度的“始终如一”,一般翻译成“一贯(如此)的”。与之相对的是朝三暮四、出尔反尔之类的词语。
2、consecutive是指空间(位置/排列)上的“连续”和“贯通”,它强调的是“连”,与之相对的是间断、断裂。如某条曲线是连续的,某个文章段落是连贯的。
3、continuous是指时间/过程上的“继续”和“承接”,强调中间的“内容或实体”不发生中断或缺失(但是在时间/过程上可以暂时中止/停顿,后续承接着往下发展),是在原有状态下的“继起”和“延续”。如电视连续剧是指在剧情上承接,但可以中途停播然后续播,文章或故事也先写一部分再于后面其它时间续完(这时候最先的文章末尾常有to be continued 或 to be completed字样)。
4、contiguous跟continuous只有g和n一个字母的差别,但它们的区别还是很明显的。contiguous是指地域上的相连、相邻、交界,是“毗邻”的意思,如广东和广西在地域上就是contiguous的,Excel中的单元格之间也是contiguous的。
5、adjacent是指“挨个、紧挨着”的意思,强调空间位置上的挨在一起且中间没有间隔物。比如隔壁的2个房间就是adjacent的(可以认为墙壁隔开了彼此),两个人并排站一起或者某2个人之间没有其它人也是用adjacent(可以认为是空隙隔开了彼此)。adjacent和contiguous的最大区别就在于:adjacent不要求拼凑性地将边界进行贴合,中间是可以有空隙的;contiguous则是没有任何空隙或间隔物的。
6、successive是succession的形容词形式,是指“一个接一个的”、“前后相继的/前仆后继的”、“后面的、接替的”。它强调的是“接”和“替”:因为要“接上”,所以相邻的两个对象是连续的(未断开);因为要“顶替”,所以后者要在前者死亡、离任、离开之后才能代替前者的位置。

使用lo和hi来表示subarray的最小最大index,并使用currentSum和minLen分别记录目前subarray的总和,以及目前满足条件的subarray的最小长度。

外层循环:

  • hi每次增1并更新currentSum;
  • 内层循环:while (currentSum - nums[lo] >= s)时,lo增1并更新currentSum;
  • 若minLen > hi - lo,minLen = hi - lo;
while hi < the length of nums    currentSum = currentSum + nums[hi]    hi = hi + 1    while currentSum - nums[lo] >= s        currentSum = currentSum - nums[lo]        lo = lo - 1    end    if minLen > hi - lo        minLen = hi - lo    end ifend
0 0
原创粉丝点击