日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Weekly Contest 141

發(fā)布時(shí)間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Weekly Contest 141 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

做了第一道后,看了下中間兩道題目,沒(méi)怎么看懂就先放著,做完最后一道,然后就沒(méi)時(shí)間了。

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 <= arr.length <= 10000
  • 0 <= arr[i] <= 9
  • 題目大意:給你一個(gè)數(shù)組,讓你改造這個(gè)數(shù)組,規(guī)則如下:1、數(shù)組長(zhǎng)度不變。2、碰見(jiàn)0就將0重復(fù)一次,然后下一個(gè)數(shù)字往后移一位,查過(guò)長(zhǎng)度的數(shù)字去掉。

    解題思路:還是比較簡(jiǎn)單的,只要看懂題目,按照題目規(guī)則我們可以先將數(shù)組保存下來(lái),然后直接遍歷保存的數(shù)組,在原數(shù)組上直接修改。(應(yīng)該還有不需要輔助數(shù)組的解法)

    代碼:

    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 <= str1.length, str2.length <= 1000
  • str1?and?str2?consist of lowercase English letters.
  • 題目大意:題目很簡(jiǎn)單,就是求最短公共父串。即給你兩個(gè)串str1和str2,讓你尋找一個(gè)最短字符串str既包含str1也包含str2。當(dāng)然這個(gè)str可能會(huì)有多個(gè),輸出其中一個(gè)就好。

    解題思路:相當(dāng)于是一個(gè)LCS變種吧。求出兩個(gè)串的LCS,然后將兩個(gè)串不在LCS中的字符在相應(yīng)的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

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/Asimple/p/11077851.html

    總結(jié)

    以上是生活随笔為你收集整理的Weekly Contest 141的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。