LeetCode Weekly Contest 56 解题报告
来源:互联网 发布:网络考试平台 编辑:程序博客网 时间:2024/06/07 07:01
LeetCode Weekly Contest 56 解题报告
如前面几篇博客中说的那样, 我想换些形式来写这东西。(毕竟有一定的任务性质, 还是有点枯燥的) 这种以Contest的形式早就想过了, 只不过之前Contest的题没有完整做出过 Orz….. 总算这次的题略水一点。就先做一次合集吧。
T717 以及 T443
由于这两题都是easy, 所以放一起作为一个板块写。
We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).
Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
Note:
1 <= len(bits) <= 1000
.
bits[i]
is always0
or1
.
这个编码是一个长度为2
的哈夫曼编码。所以可以从头开始直接匹配, 不存在有多种匹配的可能性。
所以很容易得到时间复杂度为 0
,则匹配为 0
, 如果是1
,则一次匹配两个字符。
AC代码如下:
class Solution {public: bool isOneBitCharacter(vector<int>& bits) { int i = 0; bool isOne; while (i < bits.size()) { if (bits[i] == 1) { isOne = false; i+= 2; } else { isOne = true; i++; } } return isOne; }};
Given an array of characters, compress it in-place.
The length after compression must always be smaller than or equal to the original array.
Every element of the array should be a character (not int) of length 1.
After you are done modifying the input array in-place, return the new length of the array.
题目还附了许多样例, 建议直接去看一遍题:https://leetcode.com/problems/string-compression/description/
题目大意就是说把一个字符串进行压缩, 压缩的主体是连续出现的字符。 把连续出现的字符用字符和出现的次数来表示。
这题是一道基础题吧。 就是简单的编程实现功能即可。 并不需要什么算法。需要注意的是:
1. 出现的次数可能是多位数, 不局限于个位数, 因此需要实现一个itoa()
函数。
2. 最后一次要注意判断压缩以及进行压缩。
AC代码如下:
class Solution {public: string itoa(int n) { if (n == 0) return "0"; string ans = ""; while (n != 0) { ans.insert(ans.begin(), '0' + n % 10); n/= 10; } return ans; } void Compress(vector<char> &chars, int &j, char x, int nums) { string times = itoa(nums); chars[j] = x; ++j; for (int i = 0; i < times.size(); ++i) { chars[j] = times[i]; ++j; } } int compress(vector<char>& chars) { int count = 1, j = 0; for (int i = 1; i < chars.size(); ++i) { while (i < chars.size() && chars[i] == chars[i - 1]) { count++; i++; } if (i >= chars.size()) break; if (count > 1) { Compress(chars, j, chars[i - 1], count); } else { chars[j] = chars[i - 1]; j++; } count = 1; } if (count > 1) { Compress(chars, j, chars[chars.size() - 1], count); } else { chars[j] = chars[chars.size() - 1]; j++; } return j; }};
T718
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.
Example 1:
Input:
A:[1,2,3,2,1]
B:[3,2,1,4,7]
Output:3
Explanation:
The repeated subarray with maximum length is[3, 2, 1]
.Note:
1.1 <= len(A), len(B) <= 1000
2.0 <= A[i], B[i] < 100
题目大意
这道题是找出两个串的最长公共子串。从题目描述也能看出, 这题和著名的DP
Count[i][j] = A[i] == B[j] ? Conut[i - 1][j - 1] + 1 : 0
这是典型的DP。 思路就不多解释了。 如果不理解请先去了解
这里需要注意到, 根据这个状态转移方程, 我们可以对状态进行压缩, 以节省空间复杂度。可以看到, 每一层的迭代只和其上一层有关。因此我们最多只需保留两层的状态, 反复迭代即可。 关键在于, 如果我们调整
j
的搜索顺序, 改为从后向前搜索, 那么可以完美的压缩一维, 即只用保留当前这一层的状态。(剧透: 这题不状态压缩会
以下是AC代码, 基本就是上面状态转移方程的代码实现, 重点是循环的顺序
class Solution {public: int findLength(vector<int>& A, vector<int>& B) { vector<int> count(B.size(), 0); int maximum = 0; for (int i = 0; i < A.size(); ++i) { for (int j = B.size() - 1; j >= 0; --j) { if (A[i] != B[j]) { count[j] = 0; } else { if (i == 0 || j == 0) count[j] = 1; else count[j] = count[j - 1] + 1; } if (count[j] > maximum) maximum = count[j]; } } return maximum; }};
T719
题目描述
Given an integer array, return the k-th smallest
distance
among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.
Input:
nums = [1,3,1]
k = 1
Output:0
Explanation:
Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.Note:
1.2 <= len(nums) <= 10000.
2.0 <= nums[i] < 1000000.
3.1 <= k <= len(nums) * (len(nums) - 1) / 2
.
题目分析
emmm,这题的数据范围我觉得比较有意思。 由于是[1,10000]
,一般而言, 用
再来看题意。 首先这种数对的数量是
既然用了
桶的范围是[1, 1000000]
, 小于复杂度
(交上去是过了,不过耗时肯定在正态分布之外。。)
以下是AC代码。 整个代码非常短, 只有10来行。 完全看不出这是一道Hard。
class Solution {public: int smallestDistancePair(vector<int>& nums, int k) { vector<int> count(1000000, 0); for (int i = 0; i < nums.size() - 1; ++i) { for (int j = i + 1; j < nums.size(); ++j) { count[abs(nums[i] - nums[j])]++; } } int sum = 0; for(int i = 0; i < 10000000; ++i) { sum+= count[i]; if (sum >= k) return i; } }};
Contest 总结
这次的Contest我觉得是我做过的最简单的一次。题目分布是 2Easy, 1Medium, 1Hard. 容易在于, Hard的数据量卡的不严, 一个比较trival的解法也能蹭过;而Medium是一个朴素DP, 只是考察了一个状态压缩。从时间上来说, 我觉得每道题的分配时间理想情况下应大概为: 10min, 15min, 15min, 10min 中间两题时间略多是因为一个是要对字符串进行处理, 稍不留神可能会错; 另一个是考虑到要推出状态转移, 并且要进行状态压缩。
当然实际做的时候哪有那么理想。。。我觉得大概90min做完就差不多吧。
(残念: 这次因为睡晚了只在Contest最后半小时开始, 到最后只A了前两题Orz。。。)
The End.
- LeetCode Weekly Contest 56 解题报告
- LeetCode Weekly Contest 26解题思路
- LeetCode Weekly Contest 27解题思路
- LeetCode Weekly Contest 28解题思路
- LeetCode Weekly Contest 29解题思路
- LeetCode Weekly Contest 30解题思路
- LeetCode Weekly Contest 31解题思路
- LeetCode Weekly Contest 32解题思路
- LeetCode Weekly Contest 33解题思路
- LeetCode Weekly Contest 34解题思路
- LeetCode Weekly Contest 35解题思路
- LeetCode Weekly Contest 36解题思路
- LeetCode Weekly Contest 37解题思路
- LeetCode Weekly Contest 38解题思路
- LeetCode Weekly Contest 39解题思路
- LeetCode Weekly Contest 40解题思路
- LeetCode Weekly Contest 41解题思路
- LeetCode Weekly Contest 42解题思路
- Python---注释
- Tarjan模板
- 爬虫
- Linux下发送与接收组播数据(socket编程)
- 盖伦蹲草丛
- LeetCode Weekly Contest 56 解题报告
- 2017118每日练习
- Code forces 887A Div.64解题报告
- #javaGUI#基于swing包的java用户界面设置入门,布局篇
- LeetCode 403. Frog Jump 题解
- C语言学习笔记(九)——IDE
- Vim 使用特殊符号快速删除指定字符,文件加密,粘贴代码缩进解决
- 自己写了一个flume小文件合并脚本,解决hdfs小文件过多问题
- win10下配置Tensorflow1.4