题目描述

编写一个函数,其作用是将输入的字符串反转过来。

示例 1:

输入: "hello"
输出: "olleh"

示例 2:

输入: "A man, a plan, a canal: Panama"
输出: "amanaP :lanac a ,nalp a ,nam A"

思路

思路一:

逆序拼接字符串

思路二:

依次交换两边的值

思路三:

直接调用StringBuilder 的 reverse()

思路四:

用栈来实现反转

代码实现

package String;

import java.util.Stack;

/**
* 344. Reverse String(反转字符串)
* 编写一个函数,其作用是将输入的字符串反转过来。
*/
public class Solution344 {
public static void main(String[] args) {
Solution344 solution344 = new Solution344();
String s = "hello";
System.out.println(solution344.reverseString_4(s));
} /**
* 逆序拼接字符串
*
* @param s
* @return
*/
public String reverseString(String s) {
StringBuilder str = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
str.append(s.charAt(i));
}
return String.valueOf(str);
} /**
* 依次交换两边的值
*
* @param s
* @return
*/
public String reverseString_2(String s) {
char[] chars = s.toCharArray();
int i = 0;
int j = chars.length - 1;
while (i < j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
i++;
j--;
}
return new String(chars);
} /**
* 直接调用StringBuilder 的 reverse()
*
* @param s
* @return
*/
public String reverseString_3(String s) {
return new StringBuilder(s).reverse().toString();
} /**
* 用栈来实现反转
*
* @param s
* @return
*/
public String reverseString_4(String s) {
Stack<Character> stack = new Stack<>();
char[] chars = s.toCharArray();
String res = "";
for (int i = 0; i < chars.length; i++) {
stack.push(chars[i]);
}
for (int i = 0; i < chars.length; i++) {
res += stack.pop();
}
return res;
}
}

Leetcode#344. Reverse String(反转字符串)的更多相关文章

  1. LeetCode 344. Reverse String(反转字符串)

    题目描述 LeetCode 344. 反转字符串 请编写一个函数,其功能是将输入的字符串反转过来. 示例 输入: s = "hello" 返回: "olleh" ...

  2. [LeetCode] 344. Reverse String 翻转字符串

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

  3. 344 Reverse String 反转字符串

    请编写一个函数,其功能是将输入的字符串反转过来.示例:输入:s = "hello"返回:"olleh"详见:https://leetcode.com/probl ...

  4. Leetcode 344:Reverse String 反转字符串(python、java)

    Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...

  5. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  6. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  7. Leetcode 344 Reverse String 字符串处理

    题意:反转字符串,用好库函数. class Solution { public: string reverseString(string s) { reverse(s.begin(),s.end()) ...

  8. (字符串 数组 递归 双指针) leetcode 344. Reverse String

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

  9. LeetCode 344. Reverse String

    Problem: Write a function that takes a string as input and returns the string reversed. Example: Giv ...

随机推荐

  1. ElasticSearch(七):Java操作elasticsearch基于smartcn中文分词查询

    package com.gxy.ESChap01; import java.net.InetAddress; import org.elasticsearch.action.search.Search ...

  2. matplotlib绘图的基本操作

    转自:Laumians博客园 更简明易懂看Matplotlib Python 画图教程 (莫烦Python)_演讲•公开课_科技_bilibili_哔哩哔哩 https://www.bilibili. ...

  3. 我用Python实现了一个小说网站雏形

    前言 前段时间做了一个爬取妹子套图的小功能,小伙伴们似乎很有兴趣,为了还特意组建了一个Python兴趣学习小组,来一起学习.十个python九个爬,在大家的印象中好像Python只能做爬虫.然而并非如 ...

  4. 【翻译】WhatsApp 加密概述(技术白皮书)

    目录      简介      术语      客户端注册      会话初始化设置      接收会话设置      交换信息      传输媒体和附件      群组消息      通话设置   ...

  5. Linux下修改MySQL数据表中字段属性

    一.修改某个表的字段类型及指定为空或非空 alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空]; alter table 表名称 modify 字段名称 字段类 ...

  6. 如何在.net 4.0下安装TLS1.2的支持

    原始出处:www.cnblogs.com/Charltsing/p/Net4TLS12.html 作者QQ: 564955427 最近提交请求发生错误:不支持请求的协议,研究了一下TLS1.2,发现这 ...

  7. SpringMVC返回json数据的三种方式(转)

    原文:https://blog.csdn.net/shan9liang/article/details/42181345# 1.第一种方式是spring2时代的产物,也就是每个json视图contro ...

  8. thymeleaf循环

    th:each属性用于迭代循环,语法:th:each="obj,iterStat:${objList}"迭代对象可以是Java.util.List,java.util.Map,数组 ...

  9. 洛谷 p1090 合并果子

    https://www.luogu.org/problemnew/show/P1090 优先队列的经典题目 体现了stl的优越性 #include<bits/stdc++.h> using ...

  10. 使用chrome开发者工具中的network面板测量网站网络性能

    前面的话 Chrome 开发者工具是一套内置于Google Chrome中的Web开发和调试工具,可用来对网站进行迭代.调试和分析.使用 Network 面板测量网站网络性能.本文将详细介绍chrom ...