编程题及解题思路(2,原串翻转问题)

题目描述

请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量)。

给定一个string iniString,请返回一个string,为翻转后的字符串。保证字符串的长度小于等于5000。

测试样例:
"This is nowcoder"
返回:"redocwon si sihT"
public static String reverseString(String iniString) {
        // write code here
         String Str = "";
        for (int i=iniString.length()-1;i>=0; i--) {
            
            Str=Str+iniString.charAt(i);
        }
        return Str;
    }

解题思路

这道题第一时间想到的就是遍历呗

将后边的值放到一个新数组中去然后toString就好了嘛。但结果却是打印出一串数组有逗号有括号的

于是就直接用字符串+号拼接吧,代码很简单没啥需要解释的。

效率:运行时间:219ms  占用空间30776k

这里说个题外话,也算科普一下,上个代码吧!

public static void reverseString2() {
        String Str=null;
        String Str1=Str+"大家好!";
        String Str2="我是一只猪!";
        String Str3=Str+Str2;
        System.err.print(Str1+"\\\\"+Str3);
    }

这个打印出来的结果是什么???

不妨猜测下

意外不?

这曾经是华为的一道面试题

String 类型为null的时候,进行拼接运算,会把null转为String类型去拼接,它会做一个转型再执行拼接

先写到这,我再看看这道题还有什么其他解题思路。

好了,上代码

public static String reverseString3(String iniString) {
        // write code here
         StringBuilder str = new StringBuilder(iniString);
            return str.reverse().toString();
    }

刚才说数组集合toString后有逗号括号啥的,这不包括StringBuilder和StringBuffer这两兄弟,他们都重写了toString方法打印的是其本身。

我便看了下这个类,发现他有一个reverse()方法直接解决问题。

效率:运行时间:38ms  占用空间10052k

这里看下源码怎么干的


/**
* Causes this character sequence to be replaced by the reverse of * the sequence. If there are any surrogate pairs included in the * sequence, these are treated as single characters for the * reverse operation. Thus, the order of the high-low surrogates * is never reversed. * * Let <i>n</i> be the character length of this character sequence * (not the length in {@code char} values) just prior to * execution of the {@code reverse} method. Then the * character at index <i>k</i> in the new character sequence is * equal to the character at index <i>n-k-1</i> in the old * character sequence. * * <p>Note that the reverse operation may result in producing * surrogate pairs that were unpaired low-surrogates and * high-surrogates before the operation. For example, reversing * "\u005CuDC00\u005CuD800" produces "\u005CuD800\u005CuDC00" which is * a valid surrogate pair. * * @return a reference to this object. */ public AbstractStringBuilder reverse() { boolean hasSurrogates = false; int n = count - 1; for (int j = (n-1) >> 1; j >= 0; j--) { int k = n - j; char cj = value[j]; char ck = value[k]; value[j] = ck; value[k] = cj; if (Character.isSurrogate(cj) || Character.isSurrogate(ck)) { hasSurrogates = true; } } if (hasSurrogates) { reverseAllValidSurrogatePairs(); } return this; }

分析一下 

count 就是长度,我们调用length()返回的便是count

for循环将字符串前后颠倒,关于Character类中的isSurrogate方法我没看懂,,源码粘出来

 /**
     * Determines if the given {@code char} value is a Unicode
     * <i>surrogate code unit</i>.
     *
     * <p>Such values do not represent characters by themselves,
     * but are used in the representation of
     * <a href="#supplementary">supplementary characters</a>
     * in the UTF-16 encoding.
     *
     * <p>A char value is a surrogate code unit if and only if it is either
     * a {@linkplain #isLowSurrogate(char) low-surrogate code unit} or
     * a {@linkplain #isHighSurrogate(char) high-surrogate code unit}.
     *
     * @param  ch the {@code char} value to be tested.
     * @return {@code true} if the {@code char} value is between
     *         {@link #MIN_SURROGATE} and
     *         {@link #MAX_SURROGATE} inclusive;
     *         {@code false} otherwise.
     * @since  1.7
     */
    public static boolean isSurrogate(char ch) {
        return ch >= MIN_SURROGATE && ch < (MAX_SURROGATE + 1);
    }

我用图花一下reverse的执行流程-----灵魂画手墨子猪

能力有限,希望有大佬讲解。原创转载请声明。

 

posted @ 2019-08-27 16:36  上官墨子猪  阅读(182)  评论(0编辑  收藏  举报