1816. 截断句子
遍历
class Solution { public String truncateSentence(String s, int k) { String ans=""; int n = s.length(); for(int i=0;i < n;i++) { if(s.charAt(i)==' ') { k--; if(k == 0) { break; } } ans+=s.charAt(i); } return ans; } }
2023-12-02 22:28:22
class Solution { public String truncateSentence(String s, int k) { String ans=""; int n = s.length(); for(int i=0;i < n;i++) { if(s.charAt(i)==' ') { k--; if(k == 0) { break; } } ans+=s.charAt(i); } return ans; } }