LeetCode-917 Reverse Only Letters Solution (with Java)

1. Description:

Notes:

2. Examples:

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2018-11-11
 3  */
 4 class Solution {
 5     public String reverseOnlyLetters(String origin) {
 6         char[] str = origin.toCharArray();
 7         String temp = new StringBuffer(origin).reverse().toString();
 8         String temp1 = "";
 9 
10         for (int i = 0; i < temp.length(); i++) {
11             if (Character.isLetter(temp.charAt(i)))
12                 temp1 += temp.charAt(i);
13         }
14         int k = 0;
15         for (int i = 0; i < origin.length(); i++) {
16             if (!Character.isLetter(str[i]))
17                 continue;
18             else
19                 str[i] = temp1.charAt(k++);
20         }
21         return new String(str);
22     }
23 }    
原文地址:https://www.cnblogs.com/sheepcore/p/12396445.html