题解 | #反转字符串#

反转字符串

http://www.nowcoder.com/practice/c3a6afee325e472386a1c4eb1ef987f3

package org.example.test;

/**
 * 采用双指针,首尾指针互相交换数据,直到相遇
 */
public class ReverseStringTest {
    public static void main(String[] args) {
        System.out.println(solve("abc"));
    }
    public static String solve (String str) {
        // write code here
        char[] tmp = str.toCharArray();
        int a=0;
        int b=tmp.length-1;
        for (;a<b;a++, b--){
            char t = tmp[a];
            tmp[a]=tmp[b];
            tmp[b]=t;
        }
        return new String(tmp);
    }
}
算法 文章被收录于专栏

数据结构和算法

全部评论

相关推荐

点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务