leetcode 6 Z 字形变换
这是一道思考题,比较简单,但是我没有想明白为啥程序输出和预期一致判定为wa…
这道题是一道经典的dp,给定一个字符串 s,找到 s 中最长的回文子串。用dp[i][j]表示字符串s从i位到j位是回文子串。然后当每次赋值dp[i][j]=1的时候,记录回文子串的最大长度以及index。
这道题很有意思,题目是给定两个大小为 m 和 n 的正序(从小到大)数组 nums1
和 nums2
。请你找出并返回这两个正序数组的中位数,要求设计一个时间复杂度为 O(log (m+n)) 的算法。常规做法就是先合并两个数组,然后排序取中位数即可,但是这样的时间复杂度是O(nlog (m+n))。 O(log (m+n)) 很明显是二分的思路,如果可以想到对k进行二分的话题目就变简单了,可惜第一次思考的时候局限在对数组进行二分。在考虑到对k进行二分之后,还需要注意很多的边界条件。
这道题其实不难,就是有些绕。题目给了一个数字序列的规律,要求输出数字序列第i位数字。第一次没注意输出了第i个数字wa了。eg: …12345678910…,第10位数字是1。解题过程就两点:1是找到第i位对应的子串,2是找到子串中对应的数字并输出。
这是一道枚举题,第一次思考的时候一直陷在如何在三次称量之内找到假币,但是这道题已经明确了三次称量必定找到假币,所以呢,只需要枚举就可以了。假设一枚硬币是假的或轻或重,然后判断是否符合三个条件。需要注意的是,最后输出的是第几枚硬币是轻的还是重的,刚开始没有注意到这个细节wa了好几次。下面看下详细题目。
Description: Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:
Description:There is a board with 100 * 100 grids as shown below. The left-top gird is denoted as (1, 1) and the right-bottom grid is (100, 100).
Description: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2
Hangover: How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We’re assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 +
1/3 =
5/6 card lengths. In general you can make n cards overhang by 1/2 +
1/3 +
1/4 +
… +
1/(n +
1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n +
1). This is illustrated in the figure below.