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. C# 默认以管理员权限运行程序

    /** * 当前用户是管理员的时候,直接启动应用程序 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 */ //获得当前登录的Windows用户标示 //URL:http://w ...

  2. Hibernate学习笔记4

    一.关于联合主键的映射测试实例 实体类: package com.***.comBineKey;public class Person { private Person_pk pk; private ...

  3. SQL Server 存储过程

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  4. phpcmsv9自定义sql语句查询模型实现

    在phpcmsv9中,自定义sql语句查询可不太好实现,传入sql语句查询很容易被内部转入生成一系列莫名其妙的sql语句,比如最佳前缀等等,直接造成sql语句查询错误,在此也提供两种解决办法,1修改底 ...

  5. Django入门实践(3)

    Django简单应用 前面简单示例说明了views和Template的工作过程,但是Django最核心的是App,涉及到App则会和Model(数据库)打交道.下面举的例子是创建一个简单应用wiki ...

  6. JAVA实现复制文件夹

    package com.filetest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...

  7. cocos2d-x test学习[1]

    controller.cpp std::function<TestScene*()> callback;//一个是返回值,一个是参数.返回值是TestScene*,参数是()里的东西 Co ...

  8. 解决svn working copy locked问题

    标题:working copy locked 提示:your working copy appears to be locked. run cleanup to amend the situation ...

  9. Clang比 gcc/g++更人性化代码出错提示的C/C++编译器

    编译器方面的几个命令 gcc/g++ 一. 常用编译命令选项 常用用法 gcc -Wall test.c -o test gcc编译过程 .c ->(-E)-> .i[中间文件] -> ...

  10. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...