原题链接在这里:https://leetcode.com/problems/reverse-string/

题目:

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"]

Example 2:

Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

题解:

Reverse string是常见题, string在Java中是primitive类型, 一旦生成不可改变.

But we have the array of char here. Then use two pointers. Remember to move pointer in while loop.

Time Complexity: O(s.length).

Space: O(1).

AC Java:

 class Solution {
public void reverseString(char[] s) {
int l = 0;
int r = s.length - 1; while(l < r){
char temp = s[l];
s[l] = s[r];
s[r] = temp; l++;
r--;
}
}
}

跟上Reverse String IIReverse Vowels of a String.

LeetCode Reverse String的更多相关文章

  1. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

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

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  3. [LeetCode] Reverse String II 翻转字符串之二

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

  4. LeetCode Reverse String II

    原题链接在这里:https://leetcode.com/problems/reverse-string-ii/#/description 题目: Given a string and an inte ...

  5. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  6. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

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

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

  8. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  9. LeetCode Reverse Words in a String III

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...

随机推荐

  1. iOS 状态栏隐藏显示

    在 info.plist 文件中 配置 View controller-based status bar appearance 对应的值为 NO 这样可以使用 application 对象来设置 状态 ...

  2. iOS - 模拟器

    模拟器分为ipad模拟器和iphone模拟器,尺寸都是固定的,就是320*480(iphone),640*960(iphone高清)1024*768(ipad),目前这个尺寸是不能调的.

  3. javascript设计模式与开发实践

    1. js面向对象6种形式(详情) <!DOCTYPE html> <html> <head lang="en"> <meta chars ...

  4. T-SQL 基础学习 03

    局部变量 在T-SQL中,局部变量的名称必须以标记@作为前缀 语法 DECLARE @变量名数据类型 局部变量的赋值 方法一 SET @变量名 = 值 方法二 SELECT @变量名 = 值 SET和 ...

  5. oj Rapid Typing

    import bs4 import requests import urllib2 import time import base64 session=requests.Session() respo ...

  6. Android常用元件

    本文来源于 http://blog.csdn.net/wxhlinux/article/details/8601170#comments 1.4  Android應用程式元件1.4.1  Activi ...

  7. sublime代码片段

    创建方法:Tools > New Snippet 这时你会看到如下示例代码: <snippet>      <content><![CDATA[ Hello, ${ ...

  8. dedecms 按权重排序不准或BUG的处理方法

    dede:list 的方法 1.找到"根目录\include\arc.listview.class.php"文件. 2.修改代码:在文件第727行处添加按weight排序判断代码( ...

  9. [数据库]cakephp操作ENUM、tinyint等类型的一点说明

    之前无法正常更新ENUM类型的数据,感觉是框架函数实现的bug. 问题很诡异,因为INIT的时候是可以成功写入的,没理由UPDATE的时候不成功. 前后琢磨了一下午,发现了一点蛛丝马迹才终于想通.问题 ...

  10. Maven2 根据项目生成模版项目,并使用该模板批量创建工程。

    Maven 3 创建自己的模版,并使用模版创建工程 1.建立样板Maven工程: myModel 2.进入 myModel 工程根目录执行:mvn archetype:create-from-proj ...