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 }
  • 相关阅读:
    HTML5之Canvas绘图——使用Canvas绘制图形的基本教程
    搭建EF6.0+MVC4搭建框架——之路由配置
    搭建EF6.0+MVC4搭建框架遇到的问题及解决方案
    Sql2008 全文索引应用(错误7625)
    Excel 导入并导出结果集
    SqlParameter设定的value值为0时、调用的存储过程获取到的值却为null解决方法
    JavaScript中知而不全的this (转)
    JavaScript 类定义常用方法(转)
    NPOI大数据量多个sheet导出源码(原)
    网页右下角固定图片(不随滚动条滑动,始终在固定位置)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7685031.html
Copyright © 2011-2022 走看看