【LeetCode】10.Array and String —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"]

按题目要求,定义了两个指针,算法简单,无须过多赘述,直接上代码了。
 1 class Solution {
 2 public:
 3 void reverseString(vector<char>& s) {
 4     if(s.size()==0) return ;
 5     int length = s.size();//获取数组长度
 6     char *front = &s[0]; //获取数组第一个元素的地址
 7     char *tail = &s[0];
 8     for (int i = 0,j=length-1; i <=length/2,j>= length / 2;i++,j--)
 9     {
10         swap(front[i], tail[j]);
11     }
12     front = NULL;
13     tail = NULL;
14 }
15 };
 

原文地址:https://www.cnblogs.com/hu-19941213/p/10977564.html

时间: 2024-10-25 08:08:20

【LeetCode】10.Array and String —Reverse String 字符数组逆置的相关文章

leetcode 186. Reverse Words in a String II 旋转字符数组 ---------- java

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces and the words are always separated by a single space. For example,Given s =

LeetCode解题思路:344. Reverse String

Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 题意:翻转字符串,从头到尾翻转. 基本思路: 1.如果对指针操作还不熟练,可以通过另分配n个字节的内存来进行翻转.即将字符串从后向前读,然后从前向后保存在新内存中,再将指针指向新内存. 代码如下:(如果不把strlen(s)保

099、Java中String类之字符数组与字符串的转换

01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public static void main(String args[]) { String str = "hello"; // 定义字符串 char[] data = str.toCharArray(); // 将字符串变为字符数组 for (int x = 0; x < data.leng

JavaSE8基础 String String.valueOf 将字符数组转成字符串

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku.t01; public class Demo03 { public static void main(String[] args) { char[] c = { '给', '最', '苦' }; String str = String.valueOf(c); System.

求字符数组逆序数(poj1007)

int InversionNumber(char* s,int len) { int ans=0;  //s逆序数 int A,C,G;  //各个字母出现次数,T是最大的,无需计算T出现次数 A=C=G=0; for(int i=len-1;i>=0;i--) { switch(s[i]) { case 'A':A++;break;  //A是最小的,无逆序数 case 'C': { C++; ans+=A;  //当前C后面出现A的次数就是这个C的逆序数 break; } case 'G':

[C/C++] String Reverse 字符串 反转

#include <iostream> #include <string> #include <algorithm> #include <cstring> inline void STL_Reverse(std::string& str) // 反转string字符串 包装STL的reverse() 可以inline { reverse(str.begin(), str.end()); // STL 反转函数 reverse() 的实现 /* tem

C++中字符数组与string的相互转换

字符数组转化成string类型char ch [] = "ABCDEFG";string str(ch);//也可string str = ch;或者char ch [] = "ABCDEFG";string str;str = ch;//在原有基础上添加可以用str += ch; 将string类型转换为字符数组char buf[10];string str("ABCDEFG");length = str.copy(buf, 9);buf[le

【c++基础】字符数组和string相互转换

字符数组转化成string类型char ch [] = "ABCDEFG";string str(ch);//也可string str = ch;或者char ch [] = "ABCDEFG";string str;str = ch;//在原有基础上添加可以用str += ch; 将string类型转换为字符数组char buf[10];string str("ABCDEFG");length = str.copy(buf, 9);buf[le

C与C++中的字符数组char*和字符串string的相互转化

1. 将字符数组char[]转化为字符串string char ch [] = "ABCDEFG"; string str(ch);//也可string str = ch; //或者 char ch [] = "ABCDEFG"; string str; str = ch;//在原有基础上添加可以用str += ch; 2. 将字符串string转化为字符数组char[] char buf[10]; string str("ABCDEFG");