1.2---翻转字符串(CC150)

import java.util.*;

public class Reverse {
    public String reverseString(String iniString) {
        // write code here
        StringBuffer tmp = new StringBuffer(iniString);
        tmp = tmp.reverse();
        return tmp.toString();
    }
}
时间: 2024-08-09 02:20:02

1.2---翻转字符串(CC150)的相关文章

Reverse Words in a String(翻转字符串)

给定一个字符串,逐个翻转字符串中的每个单词. Given s = "the sky is blue",return "blue is sky the". 单词的构成:无空格字母构成一个单词 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括 如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个 思路: 1.定义一个新的字符串str 接收原字符串删除首尾空字符后的字符串 trim(): 2.定义字符串tmp 存储遍历字符串时的每个单词:

【算法】字符串问题——翻转字符串

/** * 翻转字符串' * 给定一个字符类型的数组chas,请在单词间作逆序调整.只要做到单词顺序逆序即可. * 例如,如果看成字符串"dog loves pig",则调整为"pig loves dog" * 过程:先整体逆序,在局部单词逆序 */ public static void rotateWord(char[] chas) { if (chas == null || chas.length <= 0) { return; } reverse(cha

[CareerCup] 1.2 Reverse String 翻转字符串

1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string. 这道题让我们用C++或C语言来翻转一个字符串,不算一道难题,在之前那道Reverse Words in a String 翻转字符串中的单词中用到了这个函数,跟那道题比起来,这题算简单的了.C语言的版本要比C++的稍微复杂一些,应为string类集成了很多有用的功能,比如得到字符串的长度,用下标

实现翻转字符串的方法,包含水平翻转(支持中文)

面试的时候或者实际业务操作时会有需要对字符串进行翻转的要求,如:abc,翻转后为:cba 等等. 在实现基本的翻转基础上再升级一下:增加对字符串做水平翻转,如: "你好啊, 今天去哪里 abc!", 则结果为 : "abc! 今天去哪里 你好啊," 1 /** 2 * 翻转字符串,如abc你好,则cba好你. --//支持中文, 3 * 4 * @param $str 需要翻转的字符串 5 * @param bool $word 是否要水平翻转,默认为否 6 * @

翻转字符串中单词的顺序

问题:翻转字符串中的单词顺序,如"hello world"变成"world hello".要求使用常量空间. c++代码如下: void reverse(string &s, int start, int end){ int len=end+start; int center=len/2; for(int i=start;i<center;i++){ swap(s[i],s[len-1-i]); } } void reverseWords(string

JAVA用标准库自己写一个字符串翻转方法,翻转字符串中字母非单词

例如输入:I love programming 输出:I evol gnimmargorp 算法思路就是:根据空格提取每一个单词,存放在一个buffer里进行翻转处理,再添加到新的字符串.最后新的字符串就完成整个方法过程. public class ReserveString { public String reserve(String sentence){ String backS = new String(); StringBuffer temp = new StringBuffer();

lintcode 容易题:Reverse Words in a String 翻转字符串

题目: 翻转字符串 给定一个字符串,逐个翻转字符串中的每个单词. 样例 给出s = "the sky is blue",返回"blue is sky the" 说明 单词的构成:无空格字母构成一个单词 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括 如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个 解题: 这个题目方法很多的 1.整体反转,对每个单词再反转,但是中间的多个空格还有单独处理 2.把后面的单词,拿到前面去.参考程序

[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:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o

[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

151 Reverse Words in a String 翻转字符串里的单词

给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用O(1) 时间复杂度的原地解法.说明:    什么构成一个词?    一系列非空格字符组成一个词.    输入字符串是否可以包含前导或尾随空格?    是.但是,您的反转字符串不应包含前导或尾随空格.    两个单词之间多空格怎么样?    将它们缩小到反转字符串中的单个空格.详见:https://leetc