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 ...
随机推荐
- Codeforces Round #363 (Div. 2)
A题 http://codeforces.com/problemset/problem/699/A 非常的水,两个相向而行,且间距最小的点,搜一遍就是答案了. #include <cstdio& ...
- 浏览器-05 HTML和CSS解析1
一个浏览器内核几个主要部分,HTML/CSS解析器,网络处理,JavaScript引擎,2D/3D图形引擎,多媒体支持等; HTML 解析和 DOM 网页基本结构 一个网页(Page),每个Page都 ...
- 转-临界区对象TCriticalSection与TRTLCriticalSection的区别
TRTLCriticalSection是一个结构体,在windows单元中定义: 是InitializeCriticalSection, EnterCriticalSection, LeaveCrit ...
- Linux下定时任务配置-crontab
实际中经常有一些任务需要定期执行,人工操作比较麻烦,如果定时执行将会省去很多人力,还可以在一些资源占用不多的时间段执行,linux下crontab命令就实现了这一便捷的功能,实现脚本的自动化运行. 常 ...
- PE530 : GCD of Divisors
\[\begin{eqnarray*}ans&=&\sum_{i=1}^nf(i)\\&=&\sum_{i=1}^n\sum_{d|i}\gcd(d,\frac{i}{ ...
- Mac下抓包
Wireshark针对UNIX Like系统的GUI发行版界面采用的是X Window(1987年更改X版本到X11).Mac OS X在Mountain Lion之后放弃X11,取而代之的是开源的X ...
- jpa语句报 org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [2] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position
@Query("SELECT area from Area AS area WHERE area.state=0 AND area.name like %?1% ")Area fi ...
- Daily Scrum02 12.09
今天星期一,各们课程要结课了,同时也是众多大作业要提交的时间, 但是我们仍然要继续坚持! 引用拿破仑将军的一句话, 最困难之日便是离成功不远之时! Member 任务进度 下一步工作 吴文会 寻找美术 ...
- antlr.collections.AST.getLine()I异常
antlr.collections.AST.getLine()I异常 Struts+hibernate+spring项目经常遇到问题 因为Struts自带的antlr-2.7.2.jar,比H ...
- C fgetc
格式:int fgetc(FILE *stream); 这个函数的返回值,是返回所读取的一个字节.如果读到文件末尾或者读取出错时返回EOF. 位于stdio.h中.从流中读取字符,即从stream所指 ...