Leetcode: Valid Word Square
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的更多相关文章
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- LeetCode 422. Valid Word Square
原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whethe ...
- 【LeetCode】422. Valid Word Square 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- Leetcode: Valid Word Abbreviation
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- LeetCode "Valid Perfect Square"
Typical binary search.. but take care of data overflow if you are using C++ class Solution { public: ...
- 422. Valid Word Square
似乎可以沿着对角线往右往下检查,也可以正常按题设检查. 我用的后者.. public class Solution { public boolean validWordSquare(List<S ...
- [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 ...
随机推荐
- wpf,离线状态下部分功能不可用。
离线状态下,设置按钮的不可用.通过改变资源字典的值. App.xaml 文件下添加如下 xmlns:sys="clr-namespace:System;assembly=mscorlib&q ...
- Android之UI编程(二):表格布局
表格布局(TableLayout)继承了LinearLayout,它的本质依然是线性布局管理器,表TableLayout采用行.列的形式来管理UI组件,它并不需要明确地声明暴行多少行.多少列,而是通过 ...
- C++: virtual inheritance and Cross Delegation
Link1: Give an example Note: I think the Storable::Write method should also be pure virtual. http:// ...
- Eclipse,到了说再见的时候了——Android Studio最全解析
转自:http://blog.jobbole.com/77635/ 去年的Google大会上,Google带给我们一个小玩具——Android Studio,说它是玩具,是因为它确实比较菜,界面过时, ...
- 【编程篇】C++11系列之——临时对象分析
/*C++中返回一个对象时的实现及传说中的右值——临时对象*/ 如下代码: /**********************************************/ class CStuden ...
- bigint数据类型
尽管int依然是SQL Server 2000中最主要的整数数据类型,但是SQL Server 2000还是新增加了整数数据类型bigint,它应用于整数超过int数据范围的场合. int数据类型所表 ...
- NOSQL场景梳理
Redis 场景:缓存,Session,消息发布订阅,产品属性分析,订单购买等强事务,计数等 Memcached 场景:读密集,写一般的缓存,Session MongoDB 场景:数据显示,查 ...
- 02.JavaScript基础下
运算符 算术:+ 加.- 减.* 乘./ 除.% 取模 实例:隔行变色.秒转时间 赋值:=.+=.-=.*=./=.%= 关系:<.>.<=.>=.==.===.!=(不等). ...
- 20145205 实验一 Java开发环境的熟悉
实验内容 命令行下Java程序开发 IDEA下Java程序开发.调试 练习(通过命令行和Eclipse两种方式实现,在Eclipse下练习调试程序) 实现凯撒密码,并进行测试 实验要求 使用JDK编译 ...
- java按值传递理解
Java没有引用传递只有按值传递,没有引用传递只有按值传递,值传递. 通过下面代码解释: public class Test { public static void main(String[] ar ...