zoukankan      html  css  js  c++  java
  • LeetCode Split Concatenated Strings

    原题链接在这里:https://leetcode.com/problems/split-concatenated-strings/description/

    题目:

    Given a list of strings, you could concatenate these strings together into a loop, where for each string you could choose to reverse it or not. Among all the possible loops, you need to find the lexicographically biggest string after cutting the loop, which will make the looped string into a regular one.

    Specifically, to find the lexicographically biggest string, you need to experience two phases:

    1. Concatenate all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given.
    2. Cut and make one breakpoint in any place of the loop, which will make the looped string into a regular one starting from the character at the cutpoint.

    And your job is to find the lexicographically biggest one among all the possible regular strings.

    Example:

    Input: "abc", "xyz"
    Output: "zyxcba"
    Explanation: You can get the looped string "-abcxyz-", "-abczyx-", "-cbaxyz-", "-cbazyx-", 
    where '-' represents the looped status.
    The answer string came from the fourth looped one,
    where you could cut from the middle character 'a' and get "zyxcba".

    Note:

    1. The input strings will only contain lowercase letters.
    2. The total length of all the strings will not over 1,000.

    题解:

    每个词或正或反连城环,在中间切一刀变成线,找字母顺序最大的线.

    除了切开那点所在字符串s之外,其他的字符串都是自己排成字母顺序最大.

    切开的那个字符串 或正或反都有可能. 在或正或反的每一个位置切开连上看得到的结果是否更大维护最大值给res.

    Time Complexity: O(k*n^2). k是字符串的平均长度. n = strs.length.

    Space: O(k*n).

    AC Java:

     1 class Solution {
     2     public String splitLoopedString(String[] strs) {
     3         for(int i = 0; i<strs.length; i++){
     4             String reverse = new StringBuilder(strs[i]).reverse().toString();
     5             if(strs[i].compareTo(reverse) < 0){
     6                 strs[i] = reverse;
     7             }
     8         }
     9         
    10         int len = strs.length;
    11         String res = "";
    12         for(int i = 0; i<len; i++){
    13             String reverse = new StringBuilder(strs[i]).reverse().toString();
    14             for(String s : new String[]{strs[i], reverse}){
    15                 for(int k = 0; k<s.length(); k++){
    16                     StringBuilder sb = new StringBuilder(s.substring(k));
    17                     
    18                     for(int j = i+1; j<len; j++){
    19                         sb.append(strs[j]);
    20                     }
    21                     for(int j = 0; j<i; j++){
    22                         sb.append(strs[j]);
    23                     }
    24                     
    25                     sb.append(s.substring(0, k));
    26                     if(sb.toString().compareTo(res) > 0){
    27                         res = sb.toString();
    28                     }
    29                 }
    30             }
    31         }
    32         return res;
    33     }
    34 }
  • 相关阅读:
    Java 1.7.0_21b11 Code Execution
    nginx NULLByte 任意代码执行漏洞
    nginx ‘ngx_http_close_connection()’远程整数溢出漏洞
    WordPress WP Super Cache插件任意代码执行漏洞
    memcached 远程拒绝服务漏洞
    原环套原环
    要去哈尔滨了
    母亲节就要到了,你忘了吗?
    对于流媒体服务的一点概念
    有了螃蟹让心情好一点
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7685031.html
Copyright © 2011-2022 走看看