美文网首页程序员
344. Reverse String

344. Reverse String

作者: namelessEcho | 来源:发表于2017-09-21 16:56 被阅读0次

charAt()返回这个位置上的code point 注意unicode码的code point 可以由不同编码集的多个code uints 构成,使用setCharAt()改变StringBuilder上对应位置的code point。String在内存中都是UNICODE code point 的表示形式,只有当需要保存或者传输时才会变换成不同的编码集。所以内存中String里的code point 都是四个字节。

PS:char 类型在java中表示为两个字节

关于java的字符串编码,java的字符串编码方式是UTF-16(心疼,当时也没有预见到世界上的文字UTF-16没有办法完全表示吧),说道UTF-16,为了表示超出范围的字符,UTF-16使用了高低代理对(Surrogate Pairs)来进行折中。
length()方法返回的是UTF-16下String的code point 数。

class Solution {
    public String reverseString(String s) {
        StringBuilder sb = new StringBuilder(s);
        int lo = 0;
        int hi =sb.length()-1;
        while(lo<=hi)
        {
            char ch = sb.charAt(lo);
            sb.setCharAt(lo,sb.charAt(hi));
            sb.setCharAt(hi,ch);
            hi--;
            lo++;
        }
        return sb.toString();
    }
}

相关文章

  • 2019-04-08

    LeetCode 344. Reverse String Description Write a function...

  • 344. Reverse String

    344. Reverse String Easy Write a function that reverses a...

  • 344. 反转字符串

    344. 反转字符串[https://leetcode.cn/problems/reverse-string/] ...

  • 344. Reverse String

    344. Reverse String Python: 最Pythonic的解法咯 Discuss有人问如下解法为...

  • String

    唐博主刷了String,我那周太忙了,需要慢慢自己补啦。344. Reverse String : Easy3....

  • 344. Reverse String

    https://leetcode.com/problems/reverse-string/description/...

  • 344. Reverse String

    Leetcode Day 1 344 Reverse String 题目:Write a function tha...

  • 344. Reverse String

    Problem Write a function that takes a string as input and...

  • 344. Reverse String

    Write a function that takes a string as input and returns...

  • 344. Reverse String

    1.描述 Write a function that takes a string as input and re...

网友评论

    本文标题:344. Reverse String

    本文链接:https://www.haomeiwen.com/subject/achtextx.html