做了第一道后,看了下中间两道题目,没怎么看懂就先放着,做完最后一道,然后就没时间了。

1089. Duplicate Zeros

Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written.

Do the above modifications to the input array in place, do not return anything from your function.

Example 1:

Input: [1,0,2,3,0,4,5,0]
Output: null
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]

Example 2:

Input: [1,2,3]
Output: null
Explanation: After calling your function, the input array is modified to: [1,2,3]

Note:

  1. 1 <= arr.length <= 10000
  2. 0 <= arr[i] <= 9

题目大意:给你一个数组,让你改造这个数组,规则如下:1、数组长度不变。2、碰见0就将0重复一次,然后下一个数字往后移一位,查过长度的数字去掉。

解题思路:还是比较简单的,只要看懂题目,按照题目规则我们可以先将数组保存下来,然后直接遍历保存的数组,在原数组上直接修改。(应该还有不需要辅助数组的解法)

代码:

class Solution {public void duplicateZeros(int[] arr) {int[] a = arr.clone();int len = arr.length;int j = 0;for(int i=0; i<len && j<len; i++) {if( a[i] == 0 ) arr[j++] = 0;if( j == len ) break;arr[j++] = a[i];}}
}

View Code

1092. Shortest Common Supersequence

Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences.  If multiple answers exist, you may return any of them.

(A string S is a subsequence of string T if deleting some number of characters from T (possibly 0, and the characters are chosen anywherefrom T) results in the string S.)

Example 1:

Input: str1 = "abac", str2 = "cab"
Output: "cabac"
Explanation:
str1 = "abac" is a substring of "cabac" because we can delete the first "c".
str2 = "cab" is a substring of "cabac" because we can delete the last "ac".
The answer provided is the shortest such string that satisfies these properties.

Note:

  1. 1 <= str1.length, str2.length <= 1000
  2. str1 and str2 consist of lowercase English letters.

题目大意:题目很简单,就是求最短公共父串。即给你两个串str1和str2,让你寻找一个最短字符串str既包含str1也包含str2。当然这个str可能会有多个,输出其中一个就好。

解题思路:相当于是一个LCS变种吧。求出两个串的LCS,然后将两个串不在LCS中的字符在相应的LCS的“空隙”中输出。

代码:

    class Solution {public String shortestCommonSupersequence(String str1, String str2) {int m = str1.length();int n = str2.length();int dp[][] = new int[m + 1][n + 1];for (int i = 0; i <= m; i++) {for (int j = 0; j <= n; j++) {if (i == 0) {dp[i][j] = j;} else if (j == 0) {dp[i][j] = i;} else if (str1.charAt(i - 1) == str2.charAt(j - 1)) {dp[i][j] = 1 + dp[i - 1][j - 1];} else {dp[i][j] = 1 + Math.min(dp[i - 1][j], dp[i][j - 1]);}}}int index = dp[m][n];String str = "";int i = m, j = n;while (i > 0 && j > 0){if (str1.charAt(i - 1) == str2.charAt(j - 1)){str += (str1.charAt(i - 1));i--;j--;index--;}else if (dp[i - 1][j] > dp[i][j - 1]) {str += (str2.charAt(j - 1));j--;index--;} else {str += (str1.charAt(i - 1));i--;index--;}}while (i > 0) {str += (str1.charAt(i - 1));i--;index--;}while (j > 0) {str += (str2.charAt(j - 1));j--;index--;}str = reverse(str);return str;}String reverse(String input) {char[] temparray = input.toCharArray();int left, right = 0;right = temparray.length - 1;for (left = 0; left < right; left++, right--) {char temp = temparray[left];temparray[left] = temparray[right];temparray[right] = temp;}return String.valueOf(temparray);}}

View Code

转载于:https://www.cnblogs.com/Asimple/p/11077851.html

Weekly Contest 141相关推荐

  1. LeetCode Weekly Contest 25 之 545.Boundary of Binary Tree

    LeetCode Weekly Contest 25 赛题 本次周赛主要分为以下4道题: 507 Perfect Number (3分) 537 Complex Number Multiplicati ...

  2. LeetCode笔记:Weekly Contest 280

    LeetCode笔记:Weekly Contest 280 1. 题目一 1. 解题思路 2. 代码实现 2. 题目二 1. 解题思路 2. 代码实现 3. 题目三 1. 解题思路 2. 代码实现 4 ...

  3. LeetCode之Weekly Contest 90

    LeetCode第90场周赛记录 第一题:亲密字符串 问题: 给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回  ...

  4. LeetCode第187场周赛(Weekly Contest 187)解题报告

    差点又要掉分了,还好最后几分钟的时候,绝杀 AK.干巴爹!!! 第一题:思路 + 模拟暴力. 第二题:线性扫描. 第三题:双指针(滑动窗口) + 优先队列. 第四题:暴力每一行最小 k 个 + 优先队 ...

  5. 记LeetCode第143次周赛(Weekly Contest 143)

    上午打完LeetCode第143次周赛,发现很多不常用的知识点都比较生涩了,最后一个半小时也只ac了前两题.这一次的题目相对以往还是比较简单吧,但奈何就是迟迟没有在代码上有较满意的实现.学习果然是不进 ...

  6. LeetCode第176场周赛(Weekly Contest 176)解题报告

    又是一周掉分之旅,我发现,LeetCode周赛的数据好水,所以有的时候,实在没思路,先暴力解决试试(即使分析出时间复杂度会超时),比如第二题和第三题都可以暴力通过,GG思密达. 这周主要使用了数据结构 ...

  7. LeetCode Weekly Contest 27

    Reverse Words in a String III 简单暴力翻转 class Solution { public:string rev(string s){int len=s.length() ...

  8. LeetCode weekly contest 190 周赛

    5/23/20 第二次打周赛.全部通过AC.纪念一下.最后一题DP用的不是最优解,写了2次bug version. 可以去Github直接看我其他leetcode代码. Q1455_Check If ...

  9. LeetCode Weekly Contest 142

    又是两道题目,感觉rank要掉了呀~ 第一道看错题目了,然后浪费了很长时间,第三道很简单,思路也有,但是没时间了. 1093. Statistics from a Large Sample We sa ...

最新文章

  1. usb数据的接收和打印,除了问题,接收数据不全
  2. 图像的亮度和对比度区别
  3. linux系统清除日志,如何清除Linux系统日志
  4. Linux系统查看硬件相关信息
  5. Android之解决在非Activity中使用startActivity
  6. c++ 嵌套私有类_嵌套类和私有方法
  7. 51Nod 1530 稳定方块
  8. linux内核下载 编译
  9. presto 使用 部署_部署PrestoDB on Cassandra
  10. 2017-3-23校内训练
  11. 如何删除C++容器中的值
  12. linkedin 第三方网站登录(JavaScript SDK)
  13. 码云的注册与使用,很简单
  14. 马哥教育42期第五周作业
  15. python 包络线_如何简明易懂地说明数据包络线分析法(DEA)?
  16. 国标MPEG-PS实时流播放器开发(附例子)
  17. apache2.2配置https协议(key文件、crt文件、csr文件生成方法)
  18. WinSock网络编程基础(2)客户端
  19. 计算机怎样禁用中等加密算法,限制加密算法和协议 - Windows Server | Microsoft Docs...
  20. 机器视觉工程师(实习岗)面经

热门文章

  1. Hbase error: KeeperErrorCode = NoNode for /hbase/master错误
  2. 史上最全总结!爬虫常见加密解密算法
  3. PWN-PRACTICE-BUUCTF-29
  4. 【LeetCode 2】两数相加(链表)
  5. 【CodeForces - 1038B 】Non-Coprime Partition (构造,数组特征)
  6. 琴生不等式一般形式_001.二次函数、方程和不等式知识点
  7. http协议与服务器通信,iPhone应用用HTTP协议和服务器通信
  8. Android png模拟svg,Android 中使用svg图片
  9. 最简单的控制台登录小案例,适合初学者
  10. JQuery,ajax异步加载selectoption/option/select多选框: