博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
344. Reverse String
阅读量:2351 次
发布时间:2019-05-10

本文共 830 字,大约阅读时间需要 2 分钟。

题目

Write a function that reverses a string. The input string is given as an array of characters char[].

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

You may assume all the characters consist of printable ascii characters.

Example 1:

Input: [“h”,“e”,“l”,“l”,“o”]

Output: [“o”,“l”,“l”,“e”,“h”]

我的想法

典型的双指针题~

class Solution {
public char[] reverseString(char[] s) {
if(s.length <= 0 || s == null) return null; if(s.length == 1) return s; int head = 0, tail = s.length - 1; while(head <= tail){
char temp = s[tail]; s[tail] = s[head]; s[head] = temp; head++; tail--; } return s; }}

解答

这道题没啥好说的,如果现实中真的需要反转的话,直接用StringBuilder中的reverse()方法吧

转载地址:http://vfqvb.baihongyu.com/

你可能感兴趣的文章
Apache Kylin 2.3 JDBC Java API 示例
查看>>
An internal error occurred during: "Initializing Java Tooling". java.lang.NullPointerException
查看>>
ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
查看>>
IntelliJ IDEA 2018 基本配置
查看>>
Spring+Mybatis+多数据源(MySQL+Oracle)
查看>>
Mybatis读取Oracle数据库Blob字段,输出原文件
查看>>
信用卡反欺诈
查看>>
线性回归
查看>>
浏览器以只读方式打开PDF
查看>>
CDH和HDP下载地址
查看>>
MysqlDataTruncation: Data truncation: Incorrect string value: '\xF0\x9D\x90\xB6"#...' for column
查看>>
.MysqlDataTruncation: Data truncation: Data too long for column 'content' at row 1
查看>>
com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1146177 > 1048576).
查看>>
Elasticsearch 7.x生产配置
查看>>
AccessDeniedException: /opt/elasticsearch-7.0.0/config/elasticsearch.keystore
查看>>
bootstrap-table 父子表 联动表 完整例子
查看>>
Spring Cloud 2.x完整入门Demo样例(Greenwich版本)
查看>>
Spring Cloud 2.x学习笔记:2、feign改进(Greenwich版本)
查看>>
SpringCloud 2.x学习笔记:3、Hystrix(Greenwich版本)
查看>>
SpringCloud 2.x学习笔记:4、Zuul(Greenwich版本)
查看>>