HJ001题解 | #字符串最后一个单词的长度#
//import java.io.*; import java.util.Scanner; public class Main { public static void main(String[] args) { //InputStream in = System.in; Scanner sc = new Scanner(System.in); String s = null; while (sc.hasNext()) { s = sc.nextLine(); //倒序双指针 int i = s.length() - 1; int j = i; while (j >= 0 && s.charAt(j) != ' ') { j--; } int len = i - j; System.out.println(len); } } }