
本文介绍如何用 java 实现一个函数,判断给定的字符串数组是否满足四种对称性规则中的至少一种:每行相同(水平)、每列相同(垂直)、主对角线方向一致(左上→右下)、副对角线方向一致(右上→左下)。
本文介绍如何用 java 实现一个函数,判断给定的字符串数组是否满足四种对称性规则中的至少一种:每行相同(水平)、每列相同(垂直)、主对角线方向一致(左上→右下)、副对角线方向一致(右上→左下)。
在处理二维字符结构(以字符串数组形式表示的矩阵)时,常需验证其是否具备某种规律性。本文围绕四类基础对称/一致性规则展开,提供清晰、健壮且可复用的 Java 实现方案。
✅ 四种规则的定义与实现逻辑
假设输入为 String[] arr,其中每个字符串长度相等(即构成规则矩形),我们依次校验如下规则:
1. 水平规则(Horizontal)
所有行字符串完全相同。
✅ 推荐实现:以首行为基准,逐行比对。相比 HashSet 方案,该方式避免创建额外集合对象,空间复杂度 O(1),且提前终止(短路判断)更高效。
private static boolean horizontal(String[] arr) {
if (arr == null || arr.length == 0) return false;
String first = arr[0];
for (String row : arr) {
if (!first.equals(row)) return false;
}
return true;
}
2. 垂直规则(Vertical)
每一列中所有字符相同(即第 i 个位置在所有字符串中值一致)。
⚠️ 注意:原答案中 vertical 方法存在严重逻辑错误——它错误地用 strings[index].charAt(index) 获取“对角线”字符作为列基准,且未校验列索引边界,会导致 IndexOutOfBoundsException 或误判。正确实现应按列遍历,固定列索引 col,检查每行在该列的字符是否统一:
private static boolean vertical(String[] arr) {
if (arr == null || arr.length == 0 || arr[0].length() == 0) return false;
int cols = arr[0].length();
for (int col = 0; col <h4>3. 主对角线规则(Diagonal Left — 左上→右下)</h4><p>要求所有位于同一主对角线(即满足 row - col 为常数)上的字符相同。但题目示例 "ABC", "BAC", "CBA" 实际体现的是<strong>反对角线(anti-diagonal)对称性</strong>,而非主对角线恒等;进一步分析示例发现,其真正意图是:<strong>矩阵中所有主对角线元素(从左上到右下)彼此相等</strong>(即 arr[i].charAt(i) 全相同),且该对角线必须完整存在(即 arr.length ≤ arr[0].length())。更严谨的实现应遍历有效对角线索引:</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/ai/2929" title="Embedded Code Review Expert"><img
src="https://img.php.cn/upload/ai_manual/001/246/273/177985718270448.png" alt="Embedded Code Review Expert" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/ai/2929" title="Embedded Code Review Expert" class="overflowclass">Embedded Code Review Expert</a>
<p class="overflowclass">针对嵌入式/固件项目的专家代码审查,采用双模型交叉审查(Claude + Codex via ACP),检测内存安全、中断危险、RTOS陷阱...</p>
</div>
<a rel="nofollow" href="/ai/2929" title="Embedded Code Review Expert" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><pre class="brush:php;toolbar:false;">private static boolean diagonalLeft(String[] arr) {
if (arr == null || arr.length == 0 || arr[0].length() == 0) return false;
int n = Math.min(arr.length, arr[0].length());
char target = arr[0].charAt(0);
for (int i = 0; i <h4>4. 副对角线规则(Diagonal Right — 右上→左下)</h4><p>对应反对角线(row + col == constant),示例 "ABC", "BAC", "CBA" 中 arr[0][2] == 'C', arr[1][1] == 'A', arr[2][0] == 'C' 并不全等 —— 实际该例满足的是<strong>矩阵关于反对角线对称</strong>(即 arr[i].charAt(j) == arr[n-1-j].charAt(n-1-i)),但题目描述为“each item is identical to the one on its upper right or bottom left”,语义模糊。结合输出结果 diagonalRight = true,合理推断题意实为:<strong>反对角线上的所有元素(即 arr[i].charAt(n-1-i))彼此相等</strong>,其中 n = min(rows, cols):</p><pre class="brush:php;toolbar:false;">private static boolean diagonalRight(String[] arr) {
if (arr == null || arr.length == 0 || arr[0].length() == 0) return false;
int n = Math.min(arr.length, arr[0].length());
char target = arr[0].charAt(arr[0].length() - 1);
for (int i = 0; i <h3>? 主函数:检查是否满足任一规则</h3><pre class="brush:php;toolbar:false;">public static boolean passesAtLeastOneRule(String[] arr) {
return horizontal(arr) || vertical(arr) || diagonalLeft(arr) || diagonalRight(arr);
}⚠️ 使用注意事项
- 输入校验必不可少:空数组、null、行列长度不一致均需前置防护,否则易抛出 NullPointerException 或 StringIndexOutOfBoundsException。
- 时间复杂度:各方法最坏为 O(n×m),整体仍为线性扫描,高效可行。
-
示例验证:
- {"AB", "AB", "AB"} → 水平 ✔️、垂直 ✔️(第0列全'A',第1列全'B')→ 返回 true
- {"ABC", "BAC", "CBA"} → 仅 diagonalLeft 和 diagonalRight 在特定解释下成立(需严格按上述修正逻辑)
✅ 总结
本文提供了四种矩阵一致性规则的准确 Java 实现,重点修正了原始答案中 vertical 和对角线逻辑的缺陷,强调边界安全与语义严谨性。实际工程中,建议将各校验方法封装为独立工具函数,并配合单元测试覆盖边界用例(如单行、单列、空字符串等),确保鲁棒性。










