344. Reverse String Java Solutions

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

Example:
Given s = "hello", return "olleh".

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public String reverseString(String s) {
 3         if(s == null || s.length() <=1) return s;
 4         StringBuffer res = new StringBuffer(s.length());
 5         for(int i = s.length()-1;i >=0 ;i--){
 6             res.append(s.charAt(i));
 7         }
 8         return res.toString();
 9     }
10 }
时间: 2024-11-05 16:28:14

344. Reverse String Java Solutions的相关文章

[LeetCode] 344 Reverse String &amp; 541 Reverse String II

原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse String II: https://leetcode.com/problems/reverse-string-ii/description/ 题目&解法: 1.Reverse String: Write a function that takes a string as input and returns

344. Reverse String(C++)

344. Reverse String Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 题目大意: 字符串倒置. 解题方法: 第一个字符与最后一个非空字符对换. 注意事项: 1.字符串最后一个字符是空字符. C++代码: 1.不良代码: 1 class Solution

Leetcode刷题记录[python]——344 Reverse String

一.前言 不是计算机专业出身,却有一颗程序猿的心. 昨日开始leetcode第一次刷题,选择了菜鸟方式,从AC率最高且难度为Easy的题开始,不管题是简单还是难,都想做个记录,既是方便以后回顾,又是以此作为一个激励,督促自己每天都能有所进步. 二.题344 Reverse String Write a function that takes a string as input and returns the string reversed. class Solution(object): def

leetCode 344. Reverse String 字符串

344. Reverse String Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 思路1: 使用一个新的string来存放结果. class Solution { public:     string reverseString(string s) {        

344. Reverse String【easy】

344. Reverse String[easy] Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 解法一: 1 class Solution { 2 public: 3 string reverseString(string s) { 4 int start = 0, e

【leetcode】344. Reverse String

problem 344. Reverse String 参考 1. Leetcode_344_Reverse String; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10419862.html

345. Reverse Vowels of a String Java Solutions

Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". Subscribe to see which compani

LeetCode之344. Reverse String

------------------------------- Java也可以实现一行代码反转字符串哦 AC代码如下: public class Solution { public String reverseString(String s) { return new StringBuffer(s).reverse().toString(); } } 题目来源: https://leetcode.com/problems/reverse-string/

LeetCode笔记:344. Reverse String

问题: Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 大意: 写一个函数获取输入的字符串然后返回反转后后的字符串. 比如: 给出s = "hello",返回"olleh" 思路: 思路很直接就想到,先把字符串拆分成一个个字符组成的