【LeetCode】422. Valid Word Square 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
目录
题目地址:https://leetcode-cn.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.
题目大意
给你一个单词序列,判断其是否形成了一个有效的单词方块。
有效的单词方块是指此由单词序列组成的文字方块的 第 k 行 和 第 k 列 (0 ≤ k < max(行数, 列数)) 所显示的字符串完全相同。
解题方法
拼接出每一列的字符串
一种简单的做法,就是按照题目说的,拼接出每一列的字符串,然后判断是否和对应的行相等。
需要注意的是每行每列的字符串长度并不是固定的,在拼接每一列的字符串的时候要进行判断是否需要拼,防止字符串取字符时越界。
C++代码如下:
class Solution {
public:
bool validWordSquare(vector<string>& words) {
int maxCol = 0;
for (string& word : words) {
if (maxCol < word.size())
maxCol = word.size();
}
int total = maxCol > words.size() ? maxCol : words.size();
for (int col = 0; col < total; ++col) {
string curcol;
for (int row = 0; row < words.size(); ++row) {
if (words[row].size() > col) {
curcol += words[row][col];
}
}
if (curcol != words[col]) {
return false;
}
}
return true;
}
};
日期
2019 年 9 月 19 日 —— 举杯邀明月,对影成三人
【LeetCode】422. Valid Word Square 解题报告(C++)的更多相关文章
- LeetCode 422. Valid Word Square
原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whethe ...
- [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 ...
- 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...
- 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...
- 【LeetCode】290. Word Pattern 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
- 【LeetCode】916. Word Subsets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...
- 【LeetCode】127. Word Ladder 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-lad ...
- 【LeetCode】79. Word Search 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
随机推荐
- 【转】群体研究套路:开心果denovo+重测序+转录组+群体进化+选择位点
转自公众号Eric生信小班.学习群体遗传套路 中科院昆明动物园吴东东研究团队联合国外研究团队2019年在Genome Biology发表题为Whole genomes and transcriptom ...
- Linux修改默认挂载NFS协议版本
系统版本:CentOS Linux release 7.4.1708 (Core) $ vi /etc/nfsmount.conf # # /etc/nfsmount.conf - see nfsmo ...
- 1 — 第一个springboot
1.什么是springboot? 老规矩:百度百科一下 2.对springboot快速上手 1).第一种方式:通过官网来创建springboot项目 ---- 了解即可 这里面的创建方式不做过多说明, ...
- Vue2全家桶+Element搭建的PC端在线音乐网站
目录 1,前言 2,已有功能 3,使用 4,目录结构 5,页面效果 登录页 首页 排行榜 歌单列表 歌单详情 歌手列表 歌手详情 MV列表 MV详情 搜索页 播放器 1,前言 项目基于Vue2全家桶及 ...
- 容器之分类与各种测试(四)——unordered-multiset
unordered-multiset是不定序关联式容器,其底部是通过哈希表实现功能. (ps:黑色框就是bucket,白色框即为bucket上挂载的元素) 为了提高查找效率,bucket(篮子)的数量 ...
- ehcache详解
Ehcache是现在最流行的纯Java开 源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料以简单介绍和配置方法居多, 如果你有这方 ...
- OpenStack之九: 创建一个实例
官网地址 https://docs.openstack.org/install-guide/launch-instance-networks-provider.html #:导入变量 [root@co ...
- 【Linux】【Commands】systemd
1. 系统启动流程:POST --> Boot Sequeue(BIOS) --> Bootloader(MBR) --> Kernel(ramdisk) --> rootfs ...
- Appium获取toast消息(二)
刚接触appium进行移动端设备的UI自动化,在遇到toast消息的时候很是苦恼了一阵,最后通过强大的搜索引擎找到了个相对解决方法,废话不多说,直接贴代码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ...
- Java变量和常量
变量 变量要素包括:变量名,变量类型,作用域. 变量作用域:类变量(static),实例变量(没有static),局部变量(写在方法中) //类中可以定义属性(变量) static double sa ...