1.2原串翻转

题目描述

请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量)。

给定一个string iniString,请返回一个string,为翻转后的字符串。保证字符串的长度小于等于5000。

测试样例

"This is nowcoder"

返回:"redocwon si sihT"

解法

//注意不分配额外空间

//①C++

//1.2原串翻转

 1 class Reverse {
 2 public:
 3     string reverseString(string iniString) {
 4         // write code here
 5         int strLen = iniString.size();
 6         if(strLen < 0)
 7         {
 8             return NULL;
 9         }
10         int left = 0;
11         int right = strLen-1;
12         char ch;
13         for (; left <right;)
14         {
15             ch = iniString[left];
16             iniString[left] = iniString[right];
17             iniString[right] = ch;
18             ++left;
19             --right;
20         }
21         return iniString;
22     }
23 };

//Java

 1 import java.util.*;
 2 public class Reverse
 3 {
 4     public String reverseString(String iniString)
 5     {
 6         StringBuilder sb = new StringBuilder(iniString);
 7         int low = 0;
 8         int high = iniString.length() - 1;
 9         char temp = ‘\0‘;
10
11         while(low < high)
12         {
13             temp = sb.charAt(low);
14             sb.setCharAt(low, sb.charAt(high));
15             sb.setCharAt(high, temp);
16             low++;
17             high--;
18         }
19         return sb.toString();
20     }
21 }

//C++ 超精简版

1 class Reverse {
2 public:
3     string reverseString(string iniString)
4     {
5             string temp(iniString.crbegin(),iniString.crend());
6             return temp;
7     }
8 }; 

来自为知笔记(Wiz)

时间: 2024-08-14 05:39:08

1.2原串翻转的相关文章

编程题及解题思路(2,原串翻转问题)

题目描述 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量). 给定一个string iniString,请返回一个string,为翻转后的字符串.保证字符串的长度小于等于5000. 测试样例: "This is nowcoder" 返回:"redocwon si sihT" public static String reverseString(String iniString) { // write code he

原串翻转

题目描述 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量). 给定一个string iniString,请返回一个string,为翻转后的字符串.保证字符串的长度小于等于5000. 测试样例: "This is nowcoder" 返回:"redocwon si sihT" 分析:其实就是对称位置上的元素交换!! AC代码如下: class Reverse { public: string reverseStrin

【2】原串翻转

题目: 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量). 给定一个string iniString,请返回一个string,为翻转后的字符串.保证字符串的长度小于等于5000. 测试样例: "This is nowcoder" 返回:"redocwon si sihT" 代码实现: 方法一:双指针思想,前后指针所指向的字符交换,直至两指针相遇,交换完毕. import java.util.*; public cl

1.2字符串 原串翻转

1 class Reverse { 2 public: 3 string reverseString(string iniString) { 4 // write code here 5 string::iterator b=iniString.begin(); 6 string::iterator e=iniString.end(); 7 e--; 8 while(b<e) 9 { 10 swap(*b++,*e--); 11 } 12 return iniString; 13 } 14 };

删除子串,只要是原串中有相同的子串就删掉所有子串。子串至少两个字符。

//删除子串,只要是原串中有相同的子串就删掉所有子串.子串至少两个字符. #include<stdio.h> #include<string.h> char* delete_string(char s[],char a[]); int main() { char s[255]; char data[255][255];//存放相同的子串 int i,j,k; int length; int row = 0,column;//data[][]的行标和列标 gets(s); lengt

杭电1062-字符串翻转

这是天津大学2015考研的编程题 Problem Description Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them. Input The input contains several test cases. The first

KMP求模式串在原串中出现的次数

#include <stdio.h> #include <string.h> #define maxn 10010 int next[maxn]; char str[maxn], buf[maxn * 100]; void getNext() { int i = 0, j = -1; next[i] = j; while(str[i]) { if(j == -1 || str[i] == str[j]) { ++i; ++j; if(str[i] == str[j]) next[i

原串与其逆序串 对应位置且 连续最大长度

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; int main() { char s[1000]; char t[1000]; int len; int i, j; memset(s, '\0', sizeof(s)); memset(t, '\0', sizeof(t)); while(gets(s)!

C#查找子串在原串中出现次数

提供的是一种思路,和具体语言无关. string test = "good good study day day up"; string r = test.Replace("oo",""); int num = (test.Length - r.Length) /"oo".length; Console.WriteLine(num);