Java字符串--给定一个字符串,将其整个字符串反转

题目描述:给定一个字符串,每个单词以空格字符分开,将其整个字符串反转.

  

 方法一:使用Java纯工具类以及一些特定的API来解决这个问题:

 1 /**
 2  * 〈一句话功能简述〉<br> 
 3  * 〈字符串反转算法〉
 4  *  利用Java提供的特性
 5  * @author wangkai_wb
 6  * @create 2020/6/19
 7  * @since 1.0.0
 8  * 解题思路:利用Java语言提供的特性,比如先通过String的split()方法拆分,
 9  * 然后通过集合工具类Collection.reverse()方法,最后再返回字符串
10  */
11 public class StringReverse {
12     public static void main(String[] args) {
13         String str = "the sky is blue";
14         System.out.println("原字符串:"+str);
15         //使用\\s+正则来以空格拆分字符串
16         String[] strs = str.split("\\s+");
17         //使用工具类Arrays.asList()将其转换为list集合
18         List<String> stringList = Arrays.asList(strs);
19         //使用Collections.reverse()方法反转内容
20         Collections.reverse(stringList);
21         //使用StringJoiner来拼接反转后的字符串即可
22         str = String.join(" ",stringList);
23         //打印str
24         System.out.println("反转后的字符串:"+str);
25     }
26 }

测试结果:

 方法二:何用双指针解法,一个指针负责循环遍历,另一个指针负责条件处理.

 1 /**
 2  * 〈一句话功能简述〉<br> 
 3  * 〈字符串反转算法〉
 4  *
 5  * @author wangkai_wb
 6  * @create 2020/6/19
 7  * @since 1.0.0
 8  * 解题思路:使用双指针的核心思想:一个指针负责循环遍历,另一个指针负责条件处理
 9  */
10 public class StringReverse1 {
11     public static void main(String[] args) {
12         String str = "the sky is blue";
13         System.out.println("原字符串:"+str);
14         //定义左右指针,右指针不动,左指针向左移动取单词
15         int right = str.length() -1;
16         int left = right;
17         //存放反转后的字符串
18         StringBuilder stringBuilder = new StringBuilder();
19         while (left >= 0){
20             //查找第一次出现的空格
21             while (left>=0 && str.charAt(left) !=' ')
22                 left --;
23             //将单词方到stringBuilder对象中
24             stringBuilder.append(str.substring(left+1,right+1)+" ");
25             //跳动单词之间的空格
26             while (left >= 0 && str.charAt(left) ==' ')
27                 left --;
28             //right指向下个单词的词尾,左指针继续前进
29             right = left;
30         }
31         //去掉末尾的空格
32         System.out.println("反转后的字符串:"+stringBuilder.toString().trim());
33     }
34 }

测试结果:

 方法三:使用双端队列实现

 1 /**
 2  * 〈一句话功能简述〉<br> 
 3  * 〈字符串反转算法〉
 4  *
 5  * @author wangkai_wb
 6  * @create 2020/6/19
 7  * @since 1.0.0
 8  * 解题思路:因为双端队列可以支持从队列头部插入的方法,
 9  * 所以我们可以将字符串中的单词一个一个进行处理,
10  * 然后将每一个单词push到队列的头部,再将队列转成字符串即可.
11  */
12 public class StringReverse2 {
13     public static void main(String[] args) {
14         String str = "the sky is blue";
15         System.out.println("原字符串:"+str);
16         int left = 0;
17         int right = str.length() -1;
18         //构建双端队列
19         Deque<String> deque = new ArrayDeque<>();
20         StringBuilder word = new StringBuilder();
21         while (left <= right){
22             char charStr = str.charAt(left);
23             if ((word.length() !=0) && (charStr ==' ')){
24                 //将单词push到队列的头部
25                 deque.offerFirst(word.toString());
26                 word.setLength(0);
27 
28             }else if (charStr != ' '){
29                 word.append(charStr);
30             }
31             ++left;
32         }
33         deque.offerFirst(word.toString());
34         System.out.println("反转后的字符串:"+String.join(" ",deque));
35     }
36 }

测试结果:

猜你喜欢

转载自www.cnblogs.com/wk-missQ1/p/13162061.html
今日推荐