我有一個給定的矩陣 m ,它的維度是 n × n 并且它包含整數。我需要將只出現一次的數字復制到一個名為 a 的新陣列中。
我認為邏輯是對矩陣中的每個數字都有一個 for 回圈,并將其與其他每個數字進行比較,但我不知道如何用代碼實際做到這一點。
我只能使用回圈(沒有地圖之類的),這就是我想出的:
public static void Page111Ex14(int[][] m) {
int previous = 0, h = 0;
int[] a = new int[m.length*m[0].length];
for (int i = 0; i < m.length; i ) {
for (int j = 0; j < m[0].length; j ) {
previous = m[i][j];
if (m[i][j] != previous) {
a[h] = m[i][j];
h ;
}
}
}
不過這可能是不正確的。
uj5u.com熱心網友回復:
這是您可以只使用 HashMap 解決的問題之一,它只是為您完成您的作業。您遍歷二維陣列,使用 HashMap 存盤每個元素及其出現次數,然后遍歷 HashMap 并將所有出現次數為 1 的元素添加到串列中。然后將此串列轉換為陣列,這是您需要回傳的內容。
這具有 O(n*n) 復雜度,其中 n 是方陣 m 的一維。
import java.util.*;
import java.io.*;
class GetSingleOccurence
{
static int[] singleOccurence(int[][] m)
{
// work with a list so that we can append to it
List<Integer> aList = new ArrayList<Integer>();
HashMap<Integer, Integer> hm = new HashMap<>();
for (int row = 0; row < m.length; row ) {
for (int col = 0; col < m[row].length; col ) {
if (hm.containsKey(m[row][col]))
hm.put(m[row][col], 1 hm.get(m[row][col]));
else
hm.put(m[row][col], 1);
}
}
for (Map.Entry entry : hm.entrySet())
{
if (Integer.parseInt(String.valueOf(entry.getValue())) == 1)
a.add(Integer.parseInt(String.valueOf(entry.getKey())));
}
// return a as an array
return a.toArray(new int[a.size()]);
}
public static void main(String args[])
{
// A 2D may of integers with some duplicates
int[][] m = { { 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10 },
{ 11, 12, 12, 14, 15 },
{ 16, 17, 18, 18, 20 },
{ 21, 22, 23, 24, 25 } };
a = singleOccurence(m);
}
}
uj5u.com熱心網友回復:
再回圈一遍,看看有沒有重復的。假設您可以使用標簽,答案可能看起來有點像這樣:
public static int[] getSingleInstanceArrayFromMatrix(int[][] m) {
int[] a = new int[m.length * m[0].length];
// Main loop.
for (int x = 0; x < m.length; x ) {
for (int y = 0; y < m[0].length; y ) {
// Gets the current number in the matrix.
int currentNumber = m[x][y];
// Boolean to check if the variable appears more than once.
boolean isSingle = true;
// Looping again through the array.
checkLoop:
for (int i = 0; i < m.length; i ) {
for (int j = 0; j < m[0].length; j ) {
// Assuring we are not talking about the same number in the same matrix position.
if (i != x || j != y) {
// If it is equal to our current number, we can update the variable and break.
if (m[i][j] == currentNumber) {
isSingle = false;
break checkLoop;
}
}
}
}
if (isSingle) {
a[x * y] = currentNumber;
}
}
}
return a;
}
不確定它是否最有效,但我認為它會起作用。如果沒有 Lists 之類的幫助,要形成最終的陣列有點困難。由于未分配的值將默認為 0,因此如果您查找回傳的陣列,則不會檢測到任何實際的零(即根據矩陣“應該”存在)。但如果有這樣的限制,我想它并不是至關重要的。
uj5u.com熱心網友回復:
使用布爾陣列boolean[] dups來跟蹤重復數字可能會更好,因此在第一遍期間,填充此中間陣列并計算單打數。
然后創建適當大小的結果陣列,如果該陣列不為空,則在第二次迭代dups中將標記為單數的值復制到結果陣列中。
public static int[] getSingles(int[][] arr) {
int n = arr.length;
int m = arr[0].length;
boolean[] dups = new boolean[n * m];
int singles = 0;
for (int i = 0; i < dups.length; i ) {
if (dups[i]) continue; // skip the value known to be a duplicate
int curr = arr[i / m][i % m];
boolean dup = false;
for (int j = i 1; j < dups.length; j ) {
if (curr == arr[j / m][j % m]) {
dup = true;
dups[j] = true;
}
}
if (dup) {
dups[i] = true;
} else {
singles ;
}
}
// debugging log
System.out.println("singles = " singles "; " Arrays.toString(dups));
int[] res = new int[singles];
if (singles > 0) {
for (int i = 0, j = 0; i < dups.length; i ) {
if (!dups[i]) {
res[j ] = arr[i / m][i % m];
}
}
}
return res;
}
測驗:
int[][] mat = {
{2, 2, 3, 3},
{4, 2, 0, 3},
{5, 4, 2, 1}
};
System.out.println(Arrays.toString(getSingles(mat)));
輸出(包括除錯日志):
singles = 3; [true, true, true, true, true, true, false, true, false, true, true, false]
[0, 5, 1]
uj5u.com熱心網友回復:
您對 的使用previous只是一個即將出現的想法。洗掉它,并填充一維a。使用兩個嵌套的 for 回圈查找重復項需要n 4 個步驟。但是,如果您對陣列進行排序a- 對值進行排序- 成本為n2 log n2,您可以更快地找到重復項。
Arrays.sort(a);
int previous = a[0];
for (int h = 1; h < a.length; h) {
if (a[h] == previous)...
previous = a[h];
...
看起來這個解決方案幾乎已經在課堂上得到了處理。
uj5u.com熱心網友回復:
看起來不太好:
previous = m[i][j];
if (m[i][j] != previous) {
a[h] = m[i][j];
h ;
}
你分配m[i][j]給previous然后你檢查是否if (m[i][j] != previous)?
對于數字可以來自的范圍,任務是否有任何限制?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/402082.html
