LeetCode 422. Valid Word Square
原题链接在这里:https://leetcode.com/problems/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
.
Example 1:
Input:
[
"abcd",
"bnrt",
"crmy",
"dtye"
] Output:
true Explanation:
The first row and first column both read "abcd".
The second row and second column both read "bnrt".
The third row and third column both read "crmy".
The fourth row and fourth column both read "dtye". Therefore, it is a valid word square.
Example 2:
Input:
[
"abcd",
"bnrt",
"crm",
"dt"
] Output:
true Explanation:
The first row and first column both read "abcd".
The second row and second column both read "bnrt".
The third row and third column both read "crm".
The fourth row and fourth column both read "dt". Therefore, it is a valid word square.
Example 3:
Input:
[
"ball",
"area",
"read",
"lady"
] Output:
false Explanation:
The third row reads "read" while the third column reads "lead". Therefore, it is NOT a valid word square.
题解:
检查是否关于对角线对称.
每一个char 都要检查一遍. 这里注意内层loop j 不是从 i 开始而是从0开始.
原本想只检查右上部分在左下部分是否有对称即可,但忽略了这里不一定size是对称的,如果左下有这个char而右上没有这个char就不能检测出false.
Time Complexity: O(m * n). m = words.size(). n 是最长string的length.
Space: O(1).
AC Java:
public class Solution {
public boolean validWordSquare(List<String> words) {
if(words == null || words.size() == 0){
return true;
}
int m = words.size();
for(int i = 0; i<m; i++){
for(int j = 0; j<words.get(i).length(); j++){
if(j >= m || i >= words.get(j).length() || words.get(i).charAt(j) != words.get(j).charAt(i)){
return false;
}
}
}
return true;
}
}
LeetCode 422. Valid Word Square的更多相关文章
- 【LeetCode】422. Valid Word Square 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...
- [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 ...
- 422. Valid Word Square
似乎可以沿着对角线往右往下检查,也可以正常按题设检查. 我用的后者.. public class Solution { public boolean validWordSquare(List<S ...
- [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: Valid Word Square
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] 408. Valid Word Abbreviation_Easy
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode] 367. Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- Leetcode 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- Redis的LRU算法
Redis的LRU算法 LRU算法背后的的思想在计算机科学中无处不在,它与程序的"局部性原理"很相似.在生产环境中,虽然有Redis内存使用告警,但是了解一下Redis的缓存使用策 ...
- 使用Fiddler抓包、wireshark抓包分析(三次握手、四次挥手深入理解)
==================Fiddler抓包================== Fiddler支持代理的功能,也就是说你所有的http请求都可以通过它来转发,Fiddler代理默认使用端口 ...
- 删除排序链表中的重复元素II --链表
题目 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例1 输入: ->->->->->-> 输出: ->-> ...
- qbittorrent搜索插件合集
qbittorrent搜索 qbittorrent搜索一个很有特色的功能: 这里收集整理了一些公开网站的插件(Plugins for Public sites),并连 源py文件一起分享. qbitt ...
- laravel5.4 orm with 用法
在laravel orm 中一个with 关联方法,需要在模板中先定义表与表之间的关系 /*一对多的关系 */ public function hasManyTemplate(){ return $t ...
- mysql 中的日期格式。date_format( ) 转换格式
date_format( ) 转换格式 : 格式 描述 %a 缩写星期名 %b 缩写月名 %c 月,数值 %D 带有英文前缀的月中的天 %d 月的天,数值(00-31) %e 月的天,数值(0-31) ...
- JXL 简单示例
JXL 1 Overview 1 Overview Home page: http://jexcelapi.sourceforge.net/ JXL 是一个开源的 Excel 开发库,支持 Excel ...
- Go的运算符
1 算术运算符 运算符 术语 示例 结果 + 加 10 + 5 15 - 减 10 - 5 5 * 乘 10 * 5 50 / 除 10 / 5 2 % 取模(取余) 10 % 3 1 ++ 后自增, ...
- Python3字典与集合
一.Python3字典 字典是另一种可变容器模型,且可存储任意类型对象字典的每个键值(key=>value)对用冒号":"分割,每个键值对之间用逗号"," ...
- Prometheus学习
简介 Prometheus 最初是 SoundCloud 构建的开源系统监控和报警工具,是一个独立的开源项目,于2016年加入了 CNCF 基金会,作为继 Kubernetes 之后的第二个托管项目. ...