LeetCode 422. Valid Word Square 验证单词平方
来源:互联网 发布:青牛软件 上市 编辑:程序博客网 时间:2024/05/16 13:48
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.
思路
1. 判断word[i][j]==word[j][i],注意对word[j][i]加保护,防止超出边界!!
class Solution {public: bool validWordSquare(vector<string>& words) { for(int i=0;i<words.size();i++){ for(int j=i+1;j<words[i].size();j++){ if(j>=words.size()||i>=words[j].size()||words[i][j]!=word[j][i]) //注意前面的两个边界条件是为了保证word[j][i]的存在,即:加保护!! return false; } } return true; } };
- LeetCode 422. Valid Word Square 验证单词平方
- [LeetCode]422. Valid Word Square
- Leetcode 367. Valid Perfect Square 平方数验证
- LeetCode: Valid Word Square
- [leetcode] 422. Valid Word Square 解题报告
- [Leetcode] 422. Valid Word Square 解题报告
- 422. Valid Word Square
- 422. Valid Word Square
- Leetcode 422 Valid Word Square
- LeetCode 425. Word Squares 单词平方
- LeetCode Valid Perfect Square(是否是平方数)
- Leetcode:367 Valid Perfect Square(是否为平方数)
- LeetCode:Valid Perfect Square
- 【Leetcode】valid perfect square
- [leetcode] Valid Perfect Square
- leetcode---Valid Perfect Square
- LeetCode -- Valid Perfect Square
- LEETCODE--Valid Perfect Square
- Xcode8 关闭不相关的log
- centOS6.5下安装node.js
- 23. Merge k Sorted Lists 典型的分治,另,合并两个List的好写法
- Android系统运动传感器
- Selenium中定位方式记录
- LeetCode 422. Valid Word Square 验证单词平方
- 动态SQL片段
- 2017年系统分析师架构师备考之路(一)
- 这是一份创业的感想,但也敲醒了很多人
- javascript Date format(js日期格式化)
- Spinner同一Item事件响应+默认第一次不触发事件
- 出现 java.lang.NoClassDefFoundError: 在Android 5.0以下
- Redis-cli 命令
- 七种跨域方法【2.document.domain篇】