博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Weekly Contest 141
阅读量:4318 次
发布时间:2019-06-06

本文共 4118 字,大约阅读时间需要 13 分钟。

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

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: nullExplanation: 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: nullExplanation: 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
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

你可能感兴趣的文章
python3安装scrapy
查看>>
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>