From d0478198a90c199ff8c2ebbc227ae021675563fb Mon Sep 17 00:00:00 2001 From: asahi Date: Thu, 23 Oct 2025 23:09:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202025-10-23=E6=89=93=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 58.length-of-last-word.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 58.length-of-last-word.java diff --git a/58.length-of-last-word.java b/58.length-of-last-word.java new file mode 100644 index 0000000..e410fdc --- /dev/null +++ b/58.length-of-last-word.java @@ -0,0 +1,30 @@ +/* + * @lc app=leetcode id=58 lang=java + * + * [58] Length of Last Word + */ + +// @lc code=start +class Solution { + public int lengthOfLastWord(String s) { + if(s == null || s.isBlank()) { + return 0; + } + int i = s.length() - 1; + while(i >= 0 && s.charAt(i) == ' ') { + i--; + } + int r = 0; + while(i >= 0) { + if(s.charAt(i) != ' ') { + r++; + i--; + } else { + break; + } + } + return r; + } +} +// @lc code=end +