标签:public inter style only integer strong col apple can
class Solution { public boolean validWordAbbreviation(String word, String abbr) { int pw = 0; int pa = 0; while (pw < word.length() && pa < abbr.length()) { // 注意API!不是abbr.charAt(pa).isLetter(),char没有方法,只有Character有static方法 if (Character.isLetter(abbr.charAt(pa))) { if (pw >= word.length() || word.charAt(pw) != abbr.charAt(pa)) { return false; } pw++; pa++; } else if (abbr.charAt(pa) == ‘0‘) { // 注意0不能算,"a" "01"这种case应输出false,具体当然和面试官沟通。 return false; } else { int number = 0; while (pa < abbr.length() && Character.isDigit(abbr.charAt(pa))) { number = number * 10 + abbr.charAt(pa) - ‘0‘; pa++; } pw += number; } } return pw == word.length() && pa == abbr.length(); } }
九章实现
public class Solution { public boolean validWordAbbreviation(String word, String abbr) { int i = 0, j = 0; while (i < word.length() && j < abbr.length()) { if (word.charAt(i) == abbr.charAt(j)) { i++; j++; } else if ((abbr.charAt(j) > ‘0‘) && (abbr.charAt(j) <= ‘9‘)) { //notice that 0 cannot be included int start = j; while (j < abbr.length() && Character.isDigit(abbr.charAt(j))) { j++; } i += Integer.valueOf(abbr.substring(start, j)); } else { return false; } } return (i == word.length()) && (j == abbr.length()); } }
leetcode408 - Valid Word Abbreviation - easy
标签:public inter style only integer strong col apple can
原文地址:https://www.cnblogs.com/jasminemzy/p/9612341.html