【2】原串翻转

题目:

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

代码实现:

方法一:双指针思想,前后指针所指向的字符交换,直至两指针相遇,交换完毕。

import java.util.*;

public class Reverse {
    public String reverseString(String iniString) {
       if (iniString == null || iniString.length() <= 1){
           return iniString;
       }

        int index1 = 0;
        int index2 = iniString.length() - 1;
        char[] arrs = iniString.toCharArray();

        while(index1 < index2){
            char temp = arrs[index1];
            arrs[index1] = arrs[index2];
            arrs[index2] = temp;

            index1++;
            index2--;
        }

        return new String(arrs);

    }
}

方法二:反转即从字符串最后一个字符输出即可。

import java.util.*;

public class Reverse {
    public String reverseString(String iniString) {
       if (iniString == null || iniString.length() <= 1){
           return iniString;
       }

        StringBuilder sb = new StringBuilder(5000);
        for (int i = iniString.length() - 1; i >= 0; i--){
            sb.append(iniString.charAt(i));
        }

        return sb.toString();

    }
}

方法一:双指针思想,前后指针所指向的字符交换,直至两指针相遇,交换完毕。

时间: 2024-08-04 04:07:51

【2】原串翻转的相关文章

1.2原串翻转

题目描述 请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量). 给定一个string iniString,请返回一个string,为翻转后的字符串.保证字符串的长度小于等于5000. 测试样例 "This is nowcoder" 返回:"redocwon si sihT" 解法 //注意不分配额外空间 //①C++ //1.2原串翻转 1 class Reverse { 2 public: 3 string rev

编程题及解题思路(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

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);