You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中二维数组三列场景下重复扇区数的交换实现问询

多列二维数组每行去重交换的VB.NET实现

需求背景

要求二维数组的每行、每列的扇区数仅出现一次。比如下面的两列数组中,第二行的数字3重复出现,不符合要求:

1   2
3   3
4   1
2   4

两列数组的解决代码

我已经写了一段VB.NET代码解决两列数组的重复问题:

For a As Integer = 0 To AR.GetLength(0) - 1
    If AR(a, 3) > 0 Then
        If AR(a, 1) = AR(a, 3) Then
            Dim Swap_Sector = AR(a, 3)
            Dim Swap_Number = AR(a, 4)
            If a < AR.GetLength(0) - 1 Then
                AR(a, 3) = AR(a + 1, 3)
                AR(a, 4) = AR(a + 1, 4)
                AR(a + 1, 3) = Swap_Sector
                AR(a + 1, 4) = Swap_Number
            Else
                AR(a, 3) = AR(a - 1, 3)
                AR(a, 4) = AR(a - 1, 4)
                AR(a - 1, 3) = Swap_Sector
                AR(a - 1, 4) = Swap_Number
                       
            End If
        End If
    End If
 Next a

执行这段代码后,重复问题解决,结果如下:

1    3
3    2
4    1
2    4

三列数组的问题

现在需要适配三列数组的情况,比如下面的数组:

1     3     4
3     2     3
4     1     2
2     4     1

第二行里数字3出现了两次,我需要把这个重复的3和第四行的1交换,该怎么实现?

三列及通用列数的解决代码

要适配任意列数的数组,核心思路是:先找到当前行里的重复值位置,再找其他行里合适的交换值(保证交换后两行都没重复,同时不破坏列的唯一性),最后执行交换。下面是调整后的VB.NET代码:

' 假设AR是你的二维数组,行、列索引从0开始(可根据实际结构调整)
Dim rowCount As Integer = AR.GetLength(0)
Dim colCount As Integer = AR.GetLength(1)

For currentRow As Integer = 0 To rowCount - 1
    ' 记录当前行每个值出现的位置
    Dim valuePositions As New Dictionary(Of Integer, List(Of Integer))()
    For col As Integer = 0 To colCount - 1
        Dim currentVal As Integer = AR(currentRow, col)
        If Not valuePositions.ContainsKey(currentVal) Then
            valuePositions.Add(currentVal, New List(Of Integer)())
        End If
        valuePositions(currentVal).Add(col)
    Next

    ' 遍历找重复值
    For Each kvp In valuePositions
        ' 如果当前值出现多次,说明有重复
        If kvp.Value.Count > 1 Then
            ' 取第二个重复的位置(比如示例中第二行第三列,索引2)
            Dim duplicateCol As Integer = kvp.Value(1)
            Dim duplicateVal As Integer = kvp.Key

            ' 找可以交换的目标行(这里以第四行为例,也可以扩展为自动查找符合条件的行)
            For targetRow As Integer = 0 To rowCount - 1
                If targetRow = currentRow Then Continue For

                Dim targetVal As Integer = AR(targetRow, duplicateCol)
                ' 检查交换后当前行是否无重复
                Dim currentRowOk As Boolean = Not valuePositions.ContainsKey(targetVal)
                ' 检查交换后目标行是否无重复
                Dim targetRowVals As New HashSet(Of Integer)()
                For col As Integer = 0 To colCount - 1
                    targetRowVals.Add(If(col = duplicateCol, duplicateVal, AR(targetRow, col)))
                Next
                Dim targetRowOk As Boolean = targetRowVals.Count = colCount

                ' 符合条件就交换
                If currentRowOk AndAlso targetRowOk Then
                    Dim temp As Integer = AR(currentRow, duplicateCol)
                    AR(currentRow, duplicateCol) = AR(targetRow, duplicateCol)
                    AR(targetRow, duplicateCol) = temp
                    Exit For ' 交换完成就退出循环
                End If
            Next
        End If
    Next
Next

针对示例的执行结果

用这段代码处理你给出的三列数组,第二行第三列的3会和第四行第三列的1交换,得到结果:

1     3     4
3     2     1
4     1     2
2     4     3

内容的提问来源于stack exchange,提问作者Johny Roosen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 00:40:32