作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:https://leetcode.com/problems/string-compression/description/ 题目描述 Given an array of characters, compress it in-place. The length after compression must…
CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的最小长度. 数据范围 \(0 \le |s| \le 8,000\) 时空范围: 2sec 512mb 时空范围让我们基本可以\(O(N^2)\)做了 先考虑如果原串的每一个子串都求出了它的压缩后长度存在了\(cnt[i][j]\)里,我们就可以很方便的做DP了 令\(dp[i]\)表示长为\(i\)的串的最…
题目标签:String 这一题需要3个pointers: anchor:标记下一个需要存入的char read:找到下一个不同的char write:标记需要存入的位置 让 read指针 去找到下一个char,找到后先把 anchor 位置上的 char 存入 write 位置, 然后把 char 重复的次数 转化为 char 存入下一个位置,最后重新设定 anchor = read + 1. Java Solution: Runtime beats 26.91% 完成日期:10/09/2018…
下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { while (p(v[del]) && del < max_index) del++; if (del >= max_index) break; if (rel < del) rel = del; while (!p(v[rel]) && rel <=…
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并未修改原始vector的元素. 代码如下: class Solution { public: int bitsOfNumber(int n) { int cnt = 0; if (n <= 1) return 0; //若为1,则不增加 while(n) //统计位数,如12就是2位 { cnt++…
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址https://leetcode.com/problems/triangle/description/ 题目描述: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac…
problem 443. String Compression Input ["a","a","b","b","c","c","c"] Output ["a","a","b","b","c","c"] Expected ["] a: Giv…
Question 443. String Compression Solution 题目大意:把一个有序数组压缩, 思路:遍历数组 Java实现: public int compress(char[] chars) { if (chars.length == 0) return 0; StringBuilder sb = new StringBuilder(); char cur = chars[0]; int sum = 1; for (int i = 1; i <= chars.length…
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/#/description 题目描述: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], t…
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积累可以对以后在对算法中优化带来好处.Ok,今天是我做的第一题Add Two Sum. 题目要求 Given an array of integers, find two numbers such that they add up to a specific target number. The fu…