题解 | #反转字符串#
反转字符串
https://www.nowcoder.com/practice/c3a6afee325e472386a1c4eb1ef987f3
import java.util.*; public class Solution { /** * 反转字符串 * @param str string字符串 * @return string字符串 */ public String solve (String str) { //如果为空返回"" // write code here if(str==null||str==""){ return ""; } //用栈的反转来找出反转字符串 int len=str.length(); Stack<Character> stack=new Stack<>(); for(int i=0;i<len;i++){ stack.add(str.charAt(i)); } String returnStr=""; while(!stack.isEmpty()){ returnStr+=stack.pop(); } return returnStr; } }