Given a sequence of words, check whether it forms a valid word square.

A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns).

Note:
The number of words given is at least 1 and does not exceed 500.
Word length will be at least 1 and does not exceed 500.
Each word contains only lowercase English alphabet a-z.

这题看起来简单但是其实很容易写错

本来想j 不从0开始,而是从 i+1开始检查,可以节省时间,但是一些为Null的情况会使讨论很复杂

比如

[
"abcd",
"bnrt",
"crm",
"dtz"
]
第三行检查到m就结束了,因为j<word.get(i).length(),所以第三行发现不了z的不同;第四行如果从index=4开始检查,就会漏检z的情况 综上,这种List<List<Integer>> 结构,两两比较,最好保证其中一个始终不为null,另一个为null就报错,这样可以省掉很多讨论情况
然后要保证一个始终不为null,就需要j<word.get(i).length(), 那么为了不漏检,j要从0开始
 public class Solution {
public boolean validWordSquare(List<String> words) {
if(words == null || words.size() == 0){
return true;
} for (int i=0; i<words.size(); i++) {
for (int j=0; j<words.get(i).length(); j++) {
if (words.size()<=j || words.get(j).length()<=i || words.get(i).charAt(j)!=words.get(j).charAt(i))
return false;
}
}
return true;
}
}
 

Leetcode: Valid Word Square的更多相关文章

  1. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  2. LeetCode 422. Valid Word Square

    原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whethe ...

  3. 【LeetCode】422. Valid Word Square 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...

  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] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  6. Leetcode: Valid Word Abbreviation

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

  7. LeetCode "Valid Perfect Square"

    Typical binary search.. but take care of data overflow if you are using C++ class Solution { public: ...

  8. 422. Valid Word Square

    似乎可以沿着对角线往右往下检查,也可以正常按题设检查. 我用的后者.. public class Solution { public boolean validWordSquare(List<S ...

  9. [LeetCode] 422. Valid Word Square_Easy

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

随机推荐

  1. 2016 ACM/ICPC Asia Regional Dalian Online(更新到五道题)

    1006 Football Games 这道题输入也很阴险!!! 这道题过题姿势最优雅的,不是if else if else if.那样很容易wa的. 如果没有平手选项, 赢得加一分的话, 可以用La ...

  2. CodeForces 540

    A. Combination Lock time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. bzoj 3791: 作业

    Description 众所周知,白神是具有神奇的能力的. 比如说,他对数学作业说一声“数”,数学作业就会出于畏惧而自己完成:对语文作业说一声“语”,语文作业就会出于畏惧而自己完成. 今天,语文老师和 ...

  4. C语言_第三章

    1.常量        1.整型常量        2.实型常量                1.十进制小数形式,由数字和小数点组成.                2.指数形式(以E或e代表以10 ...

  5. 不注册Activex 直接调用它

    此处的Activex是ATL方式的. [ComVisible(false)]    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnk ...

  6. BZOJ1493 [NOI2007]项链工厂

    未完待续... 终于改对了 热泪盈眶.jpg 错误原因:pushdown的时候没有判断是否有左右儿子,也没当x=0 return,于是出现一些奇怪的错误 #include<bits/stdc++ ...

  7. java分享第十一天(接口测试)

     HTTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求( post请求时有一个选项是form-data,或者raw,使用raw可以请求 ...

  8. 畅通工程续——E

    E. 畅通工程续 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让 ...

  9. Android中AsyncTask使用

    一.AsyncTask的作用: 代替Thread+Handler的组合,使创建异步任务变得简单. AsyncTask执行后台操作,并在用户界面上发布结果,而不必处理线程. 二.AsyncTask的定义 ...

  10. Python In Action:二、 最小的GUI程序:麻雀虽小,五脏俱全

    Python in Action第二个例子,倒是很简单,却是最基本的GUI程序框架,里面有最基本的实现GUI流程 import wx class MyApp(wx.App): def OnInit(s ...