感冒之后



睡了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. DevExpress XtraGrid RepositoryItemCheckEdit 复选框多选的解决方法

    1. RepositoryItemCheckEdit默认有三种状态,选中状态.未选中状态和半选中状态(半选中状态通常用在TreeList中如果父节点下的子节点有选中的有未选中的,则父节点状态为半选中状 ...

  2. 【AngularJS】——0.分析

    [引导分析]1.什么是AngularJS? 2.为什么要使用它? 3.应用场合? 4.基本思想? 5.四大核心特征? 6.优缺点是什么? 1.定义:AngularJS是一个用于设计动态web应用的前端 ...

  3. 寒假的ACM训练(一)

    今天开始ACM训练,选择了刘汝佳的<挑战编程>,暂时算是开始了. 测评的网址: http://www.programming-challenges.com 第一个题目是水题啦.3n+1. ...

  4. C++的显示转换

    利用显示转换使得我们可以很容易发现它们,因为通过名字就能找到:  static_cast 用于“良性”和“适度良性”转换,包括不用强制转换  const_cast  对“const”和“volatil ...

  5. WPF RadioButton & CheckBox Style

    <Style TargetType="CheckBox"> <Setter Property="Template"> <Sette ...

  6. JavaScript学习心得(七)

    一 创建事件监听器 开发人员往往使用事件和元素组合来命名事件处理函数. 创建事件监听器方法: 嵌入式事件处理器即将JavaScript函数赋值给HTML元素属性(不推荐使用:污染HTML:无法应用渐进 ...

  7. hive源代码解析之一hive主函数入口

    hive其实做的就是解析一条sql然后形成到mapreduce任务,就是一个代码解释器.hive源代码本身就可以分为ql/metasotre/service/serde 这几块:其中 对于Hive来说 ...

  8. wpf数据自动树结构

    在项目中,时常会遇到存在上下级关系的数据.在呈现方面,按照传统方法,不得不组装TreeNode之后添加到TreeView 中,已实现树形数据的显示.如果项目中需要多处使用树,毫无疑问这将存在巨大的代码 ...

  9. norflash移植及uboot 保存环境变量实验

    一.实验环境 实验板:TQ2440开发板 SDRAM:64M norflash:EN29LV160AB(2M) nandflash:(256M) 二.移植 本文不详谈从smdk2410移植到TQ244 ...

  10. 从UI Automation看Windows平台自动化测试原理

    前言 楼主在2013年初研究Android自动化测试的时候,就分享了几篇文章 Android ViewTree and DecorView Android自动化追本溯源系列(1): 获取页面元素 An ...