publicclassSolution{publicstringReverseWords(strings){string[]array=s.Split(" ");stringnewString="";if(array.Length>0){for(intai=0;ai<array.Length;ai++){if(!string.IsNullOrEmpty(newString))newString=newString+=" ";char[]charArray=array[ai].ToCharArray();inti=0,j=charArray.Length-1;for...
publicstaticStringreverseWords(String s){ s = s.trim(); s=remove(s);char[] c = s.toCharArray();intpre=0;for(inti=0; i < c.length; i++) {if(c[i] ==' ') { reverse(c, pre, i -1); pre = i +1; }elseif(i == c.length -1) { reverse(c, pre, i); } }returnnew...
Input: "a good example" Output: "example good a" Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string. Note: A word is defined as a sequence of non-space characters. Input string may contain leading or trailing spaces. However, your ...
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 1 public class Solution_Reserse { 2 public String reverseWords(String str){ 3 String str_result = ""; 4 String str_word = ""; 5 char array_src[] = ...
题目: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". 比较...
For C programmers: Try to solve it in-place in O(1) space. public class Solution { public String reverseWords(String s) { if (s == null || s.length() == 0) { return ""; } String[] array = s.split(" "); StringBuilder sb = new StringBuilder(); ...
public static string ReverseWordsInString(string str) { string reverse = ReverseString(str); string temp = ""; foreach (string s in reverse.Split(' ')) { temp += ReverseString(s) + ' '; } return temp; } private static string ReverseString(string str) { char[] chars = str.ToCharA...
Yes. However, your reversed string should not contain leading or trailing spaces. How about multiple spaces between two words? Reduce them to a single space in the reversed string. fromcollectionsimportdequeclassSolution(object):defreverseWords(self,s):""":type s: str:rtype: str"""ifs==''...
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 注:该题目已有大神实现了,地址为 https://blog.csdn.net/lanxu_yy/article/details/38827845,但使用的是C++语言。
include <stdio.h>#include <string.h>int main(){char instr[100], outstr[100], stack[100];int i, n, k, j;printf ("Input a string : ");gets(instr);n = strlen(instr);for (i = n-1, j = 0; i >= 0; i--) {k = 0;while (instr[i] != ' ' && i >= 0...