感冒之后



睡了2天觉



现在痊愈了

重启刷题进程。。

Google的题,E难度。。

比较的方法很多,应该是为后面的题铺垫的。

题不难,做对不容易,edge cases很多,修修改改好多次,写完发现是一坨。

奉劝大家尽量多想edge cases,想不出来了再提交,别像我似的赶着投胎就提交了。

想完了先自己试试下面这些test cases...

"internationalization"
"i12iz4n"
"word"
"1or1"
"apple"
"a2e"
"a"
"2"
"b"
"1"
"a"
"01"
"hi"
"hi1"
"hi"
"2i"
public class Solution
{
public boolean validWordAbbreviation(String word, String abbr)
{
if(word.length() == 0 && abbr.length() == 0) return true;
if(word.length() == 0 || abbr.length() == 0) return false; int m = 0; int n = 0;
int digit = 0; while(m < word.length() && n < abbr.length())
{
if(abbr.charAt(n) >= '0' && abbr.charAt(n) <= '9')
{
if(abbr.charAt(n) == '0' && digit == 0) return false; // digit starts with 0
digit = 10*digit + abbr.charAt(n) - '0';
n++;
if(n == abbr.length()) // if abbr ends, word has to end at the same time
{
return m + digit == word.length();
} }
else
{
m += digit;
digit = 0;
if(m == word.length())
{ if(n == abbr.length()) return true; // both end else return false; // word ends, abbr not
}
if(m > word.length()) return false; // abbr is longer than word
if(word.charAt(m) != abbr.charAt(n)) return false; // match fails
else // go on...
{
m++;
n++;
} }
} //not even same length
return m >= word.length() && n >= abbr.length(); }
}

408. Valid Word Abbreviation的更多相关文章

  1. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  2. 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...

  3. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  4. Leetcode: Valid Word Abbreviation

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  5. [LeetCode] 408. Valid Word Abbreviation_Easy

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  6. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  7. [LeetCode] Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  8. LeetCode Word Abbreviation

    原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...

  9. [LeetCode] 527. Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

随机推荐

  1. framework 4.5.1安装时发生严重错误

    http://jingyan.baidu.com/article/a501d80c0a74b4ec630f5ee5.html http://jingyan.baidu.com/article/d807 ...

  2. 简单的背包变形HDU1203,HDU2955

    今天一直在写背包,不过中间停了一段时间在写shell. 一直在做01背包.今天做了这两题很相似的背包 首先是HDU1203 Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要 ...

  3. 读书笔记之 - javascript 设计模式 - 装饰者模式

    本章讨论的是一种为对象增添特性的技术,它并不使用创建新子类这种手段. 装饰者模式可以透明地把对象包装在具有同样接口的另一对象之中,这样一来,你可以给一些方法添加一些行为,然后将方法调用传递给原始对象. ...

  4. LINUX 压缩目录成一个压缩文件

    #!/bin/bash file =$(date +%y%m%d%H%M)logfile=/home/目录名/backup/file.log echo "------"$(date ...

  5. Gvim7.4简单配置

    今天下午小折腾了一会Gvim编辑器(7.4版,目前最新).看起来高端又没有代码提示,还能锻炼锻炼记忆. 修改了下默认启动配置<修改后如下图>: 打开编辑器: 编辑->启动设定-> ...

  6. Objective-C 入门(给新人的)

    http://www.hengxinsoft.com/2010/12/objective-c-%E5%85%A5%E9%97%A8%EF%BC%88%E7%BB%99%E6%96%B0%E4%BA%B ...

  7. bootstrap的栅格布局不支持IE8该如何解决

    用bootstrap的栅格布局在IE8上出现失效的情况,通常有两种解决方式 方法/步骤   方法一:引用第三方js,一个叫respond.js的东西,github上可以搜到   方法二:由于IE8不支 ...

  8. Delphi在win7/vista下写注册表等需要管理员权限的解决方案

    看到论坛好多人问win7下写注册表的问题,我结合自己的理解写了一点东西,首先声明一下,本人初学Delphi,水平有限,大家见笑了,有什么不对之处请老鸟多指点. [背景]win7/Vista提供的UAC ...

  9. 使用Aspose.Word的基础知识整理

    var doc = new Document(path);     doc.AcceptAllRevisions();//接受所有修订

  10. Linux下修改MySQL编码的方法

    有时候因为编码需要修改mysql的编码,windows下修改有图文界面简单一些,linux大家就可以参考下面的方法   默认登录mysql之后可以通过SHOW VARIABLES语句查看系统变量及其值 ...