public static String trans(String s, int n) { if (s != null && (n >= 1 && n <= 500)) { char[] str = s.toCharArray(); s = ""; boolean containsBlank = false; Stack<String> stack = new Stack<>(); int k = 0; int j = 0; for (j = 0; j < str.length; j++) { if (str[j] != ' ' && (str[j] >= 'A' && str[j] <= 'Z')) { str[j] = (char) (str[j] + 32); } else if (str[j] != ' ' && (str[j] >= 'a' && str[j] <= 'z')) { str[j] = (char) (str[j] - 32); } if (str[j] == ' ') { stack.push(new String(str, k, j - k) + (k == 0? "" : " ")); k = j + 1; containsBlank = true; } } if (!containsBlank) stack.push(String.valueOf(str)); else stack.push(new String(str, k, j - k) + " "); while (stack.size() != 0) { s += stack.pop(); } } return s; } 算法太渣,刷题太少
点赞 评论

相关推荐

投递华为等公司10个岗位
点赞 评论 收藏
转发
牛客网
牛客企业服务