题目:

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

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

思路:

  • 题意:反转字符串
  • 不用考虑为空的情况,这种情况sb.toString()也是null,倒叙遍历,StringBuffer相加

代码:

public class Solution {
    public String reverseString(String s) {
        StringBuffer sb = new StringBuffer();
        int n = s.length();
        for(int i = n-1;i >= 0;i--){
            sb.append(s.charAt(i));
        }
        return sb.toString();
    }
}

LeetCode(69)-Reverse String的更多相关文章

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

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

  2. LeetCode 541. Reverse String II (反转字符串 II)

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

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

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

  4. Leetcode#344. Reverse String(反转字符串)

    题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...

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

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

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

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

  7. Leetcode 344 Reverse String 字符串处理

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

  8. LeetCode 344. Reverse String

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

  9. Python [Leetcode 344]Reverse String

    题目描述: Write a function that takes a string as input and returns the string reversed. Example:Given s ...

随机推荐

  1. 6.5、Android Studio的Android Device Monitor

    Android Device Monitor是一个独立的工具,可以对Android应用进行调试和分析.Android Device Monitor无需安装整合在一个IDE中,比如像Android St ...

  2. linux中查看现在使用的shell是ksh还是bash?以及怎样修改?

    查看系统支持的shell: cat  /etc/shells 查看现在使用的shell:  修改默认shell: 另外,修改了系统默认shell之后不会立即生效,之后再次登录系统修改的shell才会生 ...

  3. Objc中处理数组越界的一种办法

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Objc的数组如果在访问时索引非法,则会抛出NSRangeEx ...

  4. 程序员必须搞清的概念-equals和=和hashcode的区别

    1. 首先equals()和hashcode的介绍 equals 方法在非空对象引用上实现相等关系: * 自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true. * 对称性:对于 ...

  5. Hibernate 缓存机制全面讲解

    简介 为了降低应用程序访问我们的数据的时候的频率,提高数据读取的速率.比如计算机中为了缓解CPU和内存之间速度差异而引入的缓存是一样的道理.Hibernate同样对缓存进行了支持,使得程序的运行效率得 ...

  6. Java并发——线程池原理

    "池"技术对我们来说是非常熟悉的一个概念,它的引入是为了在某些场景下提高系统某些关键节点性能,最典型的例子就是数据库连接池,JDBC是一种服务供应接口(SPI),具体的数据库连接实 ...

  7. Java之泛型编程

    1.概念 泛型就是参数化类型.泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐式的,提高代码的重用率. 2.案例 1)先看下面案例: //不适用泛型编程 Apple app0=ne ...

  8. UNIX环境高级编程——线程同步之读写锁以及属性

    读写锁和互斥量(互斥锁)很类似,是另一种线程同步机制,但不属于POSIX标准,可以用来同步同一进程中的各个线程.当然如果一个读写锁存放在多个进程共享的某个内存区中,那么还可以用来进行进程间的同步, 互 ...

  9. 存储那些事儿(三):OpenStack的块存储Cinder与商业存储的融合

    OpenStack是一个美国国家航空航天局和Rackspace合作研发的云端运算‎软件,以Apache许可证授权,并且是一个自由软件和开放源代码项目.OpenStack是IaaS(基础设施即服务)‎软 ...

  10. 网站开发进阶(三十五)JSP页面中的pageEncoding和contentType两种属性

    JSP页面中的pageEncoding和contentType两种属性 本文介绍了在JSP页面中经常用的两种属性,分别是pageEncoding和contentType,希望对你有帮助,一起来看. 关 ...