Weekly Contest 126
前两周一直是一道,都不好意思写了。这周终于题目简单了,刷了三道。
1002. Find Common Characters
Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.
You may return the answer in any order.
Example 1:
Input: ["bella","label","roller"]
Output: ["e","l","l"]
Example 2:
Input: ["cool","lock","cook"]
Output: ["c","o"]
Note:
1 <= A.length <= 1001 <= A[i].length <= 100A[i][j]is a lowercase letter
题目大意:给你一个字符串数组,让你输出所有字符串都包含的小写字母,都出现了几次就输出几次。
思路:因为只有小写字母,所以可以将所以字符串字母的出现次数统计出来,然后直接遍历输出就好。就是代码有点难写,可能是我还没有领悟到更好的编写方式,浪费了我大量的时间(差不多一个多小时)。
代码:
class Solution {
public List<String> commonChars(String[] A) {
List<String> list = new ArrayList<>();
List<Map<Character, Integer> > maps = new ArrayList<>();
for(String str : A ) {
if( null != str ) {
int len = str.length();
Map<Character, Integer> map = new HashMap<>();
for(int i=0; i<len; i++) {
char ch = str.charAt(i);
Integer cnt = map.get(ch);
if( null == cnt ) cnt = Integer.valueOf(0);
cnt ++;
map.put(ch, cnt);
}
maps.add(map);
}
}
for(char ch = 'a'; ch<='z'; ch ++) {
int last = 105;
for(Map<Character, Integer> m : maps ) {
Integer cnt = m.get(ch);
if( null == cnt ) cnt = Integer.valueOf(0);
if( cnt < last ) last = cnt;
}
while( last != 105 && last > 0 ) {
list.add(""+ch);
last --;
}
}
return list;
}
}
1003. Check If Word Is Valid After Substitutions
We are given that the string "abc" is valid.
From any valid string V, we may split V into two pieces X and Y such that X + Y (X concatenated with Y) is equal to V. (X or Y may be empty.) Then, X + "abc" + Y is also valid.
If for example S = "abc", then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc". Examples of invalid strings are: "abccba", "ab", "cababc", "bac".
Return true if and only if the given string S is valid.
Example 1:
Input: "aabcbc"
Output: true
Explanation:
We start with the valid string "abc".
Then we can insert another "abc" between "a" and "bc", resulting in "a" + "abc" + "bc" which is "aabcbc".
Example 2:
Input: "abcabcababcc"
Output: true
Explanation:
"abcabcabc" is valid after consecutive insertings of "abc".
Then we can insert "abc" before the last letter, resulting in "abcabcab" + "abc" + "c" which is "abcabcababcc".
Example 3:
Input: "abccba"
Output: false
Example 4:
Input: "cababc"
Output: false
Note:
1 <= S.length <= 20000S[i]is'a','b', or'c'
题目大意:给出一个基础合法串 abc ,然后不断的将 abc 插入到不同的位置,也就形成了更多的合法串。比如:插入到第二个位置变成 aabcbc 这个也是一个合法串。
思路:最开始想到的就是,按照这么变的话,这些串肯定都是固定的,可以先将这些串都求出来然后再对比。后面一想这样可能耗时更长,于是放弃这个思路。然后仔细一想,如果一个串是合法的,那么我如果一直去掉 abc 串,那么后面肯定就剩下空串了。
代码:
class Solution {
public boolean isValid(String S) {
String valid = "abc";
while( S.lastIndexOf(valid)!=-1 ) {
int begin = S.indexOf(valid);
String s1 = S.substring(0, begin);
String s2 = S.substring(begin+3);
S = s1 + s2;
if( null == S || "".equals(S) ) {
return true;
}
}
return false;
}
}
1004. Max Consecutive Ones III
Given an array A of 0s and 1s, we may change up to K values from 0 to 1.
Return the length of the longest (contiguous) subarray that contains only 1s.
Example 1:
Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2
Output: 6
Explanation:
[1,1,1,0,0,1,1,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Example 2:
Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3
Output: 10
Explanation:
[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Note:
1 <= A.length <= 200000 <= K <= A.lengthA[i]is0or1
题目大意:给出一个只包含 0 和 1的数组,然后给出 一个数字K表示你可以将K个0变成1,让你求最长的连续1字串长度。
题目思路:尺取法,不解释。
class Solution {
public int longestOnes(int[] A, int K) {
int res = 0;
int len = A.length;
int end = 0, begin = 0;
for(int i=0; i<len; i++) {
if( A[i] == 0 ) {
if( K == 0 ) {
if( res < end-begin ) res = end-begin;
while( A[begin] == 1 ) begin ++;
begin ++;
} else K --;
}
end ++;
}
return res>end-begin?res:end-begin;
}
}
1000. Minimum Cost to Merge Stones
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones.
A move consists of merging exactly K consecutive piles into one pile, and the cost of this move is equal to the total number of stones in these K piles.
Find the minimum cost to merge all piles of stones into one pile. If it is impossible, return -1.
Example 1:
Input: stones = [3,2,4,1], K = 2
Output: 20
Explanation:
We start with [3, 2, 4, 1].
We merge [3, 2] for a cost of 5, and we are left with [5, 4, 1].
We merge [4, 1] for a cost of 5, and we are left with [5, 5].
We merge [5, 5] for a cost of 10, and we are left with [10].
The total cost was 20, and this is the minimum possible.
Example 2:
Input: stones = [3,2,4,1], K = 3
Output: -1
Explanation: After any merge operation, there are 2 piles left, and we can't merge anymore. So the task is impossible.
Example 3:
Input: stones = [3,5,1,2,6], K = 3
Output: 25
Explanation:
We start with [3, 5, 1, 2, 6].
We merge [5, 1, 2] for a cost of 8, and we are left with [3, 8, 6].
We merge [3, 8, 6] for a cost of 17, and we are left with [17].
The total cost was 25, and this is the minimum possible.
Note:
1 <= stones.length <= 302 <= K <= 301 <= stones[i] <= 100
其实就是石子归并,只不过合并的堆数从2变成了k。还是很简单的,可惜第一题费了我太多时间了,不然应该可以做出来。
class Solution {
public int mergeStones(int[] a, int K) {
int n = a.length;
if(n % (K-1) != 1 % (K-1)){
return -1;
}
int[] cum = new int[n+1];
for(int i = 0;i < n;i++){
cum[i+1] = cum[i] + a[i];
}
int[][] dp = new int[n][n];
for(int len = K;len <= n;len++){
for(int i = 0;i+len-1 < n;i++){
int j = i+len-1;
int cost = Integer.MAX_VALUE;
int ulen = (len-1)/(K-1)*(K-1)+1;
if(ulen % (K-1) == 1 %(K-1)){
ulen -= K-1;
}
int s = len % (K-1) == 1 % (K-1) ? len-(K-1) : len;
for(int k = i+1;k <= j;k++){
if((k-i-1)/(K-1) + (j-k+1-1)/(K-1) == (s-1)/(K-1)){
int c = 0;
if(i <= k-1)c += dp[i][k-1];
if(k <= j)c += dp[k][j];
cost = Math.min(cost, c);
}
}
if(len % (K-1) == 1 % (K-1)){
cost += cum[j+1] - cum[i];
}
dp[i][j] = cost;
}
}
return dp[0][n-1];
}
}
Weekly Contest 126的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- 一次Spring Bean初始化顺序问题排查记录
最近在使用Springboot的时候需要通过静态的方法获取到Spring容器托管的bean对象,参照一些博文里写的,新建了个类,并实现ApplicationContextAware接口.代码大致如下: ...
- 正则序RegExp
正则的理解 1.正则的懒惰性 每次在exec()中捕获的时候,只捕获第一次匹配的内容,而不往下不捕获了.我们把这叫正则的懒惰性,每一次捕获的开始位置都是从0开始 解决正则的懒惰性 修饰符g 正则 ...
- ApiKernel
using System; using System.Runtime.InteropServices; using System.Text; using HANDLE = System.IntPtr; ...
- C# 数字字符串前面不足位补零方法
; Console.WriteLine(i.ToString("D3")); Console.WriteLine(i.ToString(")); Console.Writ ...
- mysql 数据库的数据类型
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- 连接H3C交换机的Console口连不上
一.故障:使用Console线连接交换机的Console口连不上. 处理步骤: 1.先把Console线连接好,一端连接交换机的Console口,另一端连接电脑的USB口. 2.安装Console线驱 ...
- python 1-100的数相加的和
count = 1 sum = 0 while count <= 100: sum = sum + count count = count + 1 print(sum) 解释: count表示计 ...
- webpack(3)-管理资源
管理资源:(file-loader 和 url-loader 可以接收并加载任何文件,然后将其输出到构建目录) 加载css:style-loader.css-loader 以style的形式插入到he ...
- Yarn 组件的指挥部 – 调度器Scheduler
linux基础 为hadoop集群的搭建扫清了障碍,也为内存的管理,文件系统的管理扫清了障碍 接着到Hadoop的阶段,首先做集群的安装,深入到使用这两个核心的组件,分布式文件系统HDFS,解决大量数 ...