422. 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 thekthrow 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 alphabeta-z.
Example 1:
Input:[  "abcd",  "bnrt",  "crm",  "dt"]   Output:true
Example 2:
Input:[  "ball",  "area",  "read",  "lady"] Output:false

双重循环 横纵判断遇到不等立即return false;


public boolean validWordSquare(Listwords) {
    for(int i = 0; i < words.size(); i++){
          for(int j = 0; j < words.get(i).length(); j++){
                if(j >= words.size() || i >= words.get(j).length()
                    || words.get(i).charAt(j) != words.get(j).charAt(i)){
                               return false;
                  }
          }
      }
      return true;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,353评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,484评论 0 23
  • 一、 1、请用Java写一个冒泡排序方法 【参考答案】 public static void Bubble(int...
    独云阅读 5,249评论 0 6
  • 前几个月,只要白胖子拿起手机玩一个阴阳师的游戏,我就不开心,不着急的事也喊他去做。而如果他在此期间忽视...
    于小勇呀阅读 1,092评论 0 0
  • 前几天我加入了学校的轮滑社团,未曾请示爸妈就自己在淘宝上买了一双轮滑鞋,昨天和爸妈打电话,爸爸说担心你这么大...
    会飞di猪猪阅读 1,514评论 0 0