我正在尝试实现执行以下操作的算法:
如果存储在变量compHand
中的数字存在,其索引将存储在indexArray
中,并且该索引将添加到banIndex()
方法中,以便该索引永远不会再考虑进一步操作。
或
如果列表中任何两个数字的总和等于compHand
,则这些数字的索引将存储在indexArray
中,并将添加到banIndex()
中,以便它们永远不会被考虑进行任何进一步的操作。
实际上,算法工作正常,但是如果hashMap的最后value
为10,那么,10
会显示两次吗?它应该只显示一次。为什么?
例如:根据populateHash()
的算法的结果将是:5,6,7,7
它应该是:5,6,7
知道为什么会这样吗?
代码
public class Test {
static HashMap<Integer, Integer> h1 = new HashMap<Integer, Integer>();
public static void main(String[] args) {
int compHand = 10;
populateHash(Test.h1);
int iter = -1;
int [] indexArray = new int[(Test.h1.size())];
HashMap<Integer, Integer> bannedIndexHash = new HashMap<Integer, Integer>();
for (int i=1; i<=Test.h1.size(); i++) {
if (! isBannedIndex(bannedIndexHash, i)) {
if (i == Test.h1.size()) {
if (compHand == Test.h1.get(i)) {
indexArray[++iter] = i;
banIndex(bannedIndexHash, i);
}// end if
}else {
if (compHand == Test.h1.get(i)) {
indexArray[++iter] = i;
banIndex(bannedIndexHash, i);
}//end if
else {
for (int j=i+1; j<=Test.h1.size(); j++) {
if ( (! isBannedIndex(bannedIndexHash, i)) &&
(! isBannedIndex(bannedIndexHash, j)) ) {
if ( (compHand == (Test.h1.get(i)+Test.h1.get(j))) || (compHand == Test.h1.get(j)) ) {
if (compHand == (Test.h1.get(i)+Test.h1.get(j))) {
indexArray[++iter] = i;
indexArray[++iter] = j;
banIndex(bannedIndexHash, i);
banIndex(bannedIndexHash, j);
break;
}//end if
else {
if (compHand == Test.h1.get(j)) {
indexArray[++iter] = j;
banIndex(bannedIndexHash, j);
}// end if
}// end else
}// end if-condition ||
}// end if-condition &&
}// end for (j)
}// end else
}//end else
}// end ! isBannedIndex(bannedIndexHash, i)
}// end for(i)
if (iter > -1) {
System.out.println("iter > -1");
for (int i=0; i<indexArray.length; i++) {
System.out.println(indexArray[i]);
}
}
}
private static boolean isBannedIndex(HashMap<Integer, Integer> _bannedIndexHash, int index) {
// TODO Auto-generated method stub
//Log.i(TAG, "@isBannedIndex(): ");
if (!_bannedIndexHash.isEmpty()) {
for (int i=1; i<_bannedIndexHash.size(); i++)
if (index == _bannedIndexHash.get(i))
return true;
return false;
}else
return false;
}
private static void banIndex(HashMap<Integer, Integer> _bannedIndexHash, int index) {
// TODO Auto-generated method stub
//Log.i(TAG, "@banIndex(): ");
if (_bannedIndexHash != null)
_bannedIndexHash.put(_bannedIndexHash.size()+1, index);
}
private static void populateHash(HashMap<Integer, Integer> hash) {
// TODO Auto-generated method stub
hash.put(1, 1);
hash.put(2, 3);
hash.put(3, 1);
hash.put(4, 1);
hash.put(5, 10);
hash.put(6, 10);
hash.put(7, 10);
/*hash.put(8, 7);
hash.put(9, 1);
hash.put(10, 10);
hash.put(11, 5);
hash.put(12, 8);
hash.put(13, 1);
hash.put(14, 1);
hash.put(15, 6);
hash.put(16, 1);
hash.put(17, 1);
hash.put(18, 2);*/
}
}
答案 0 :(得分:2)
我重写了你的代码并丢弃了多余的if-else级联。
我的解决方案生成以下输出(由于预定义的数组长度,存在0个数字): iter&gt; -1 五 6 7 0 0 0 0
public class Test {
public static void main(final String[] args) {
HashMap<Integer, Integer> testHashes = new HashMap<Integer, Integer>();
int compHand = 10;
populateHash(testHashes);
int iter = -1;
int[] indexArray = new int[(testHashes.size())];
HashMap<Integer, Integer> bannedValues = new HashMap<Integer, Integer>();
for (int i = 1; i <= testHashes.size(); i++) {
if (!isBannedIndex(bannedValues, i)) {
if (compHand == testHashes.get(i)) {
indexArray[++iter] = i;
banIndex(bannedValues, i);
} else {
for (int j = i + 1; j <= testHashes.size(); j++) {
if (!isBannedIndex(bannedValues, j)) {
if(compHand == testHashes.get(i) + testHashes.get(j)) {
indexArray[++iter] = i;
indexArray[++iter] = j;
banIndex(bannedValues, i);
banIndex(bannedValues, j);
break;
} else {
if (compHand == testHashes.get(j)) {
indexArray[++iter] = j;
banIndex(bannedValues, j);
}
}
}
}
}
}
}
if (iter > -1) {
System.out.println("iter > -1");
for (int i = 0; i < indexArray.length; i++) {
System.out.println(indexArray[i]);
}
}
}
private static boolean isBannedIndex(
final HashMap<Integer, Integer> banned, final int index) {
return !banned.isEmpty() && banned.values().contains(index);
}
private static void banIndex(final HashMap<Integer, Integer> banned,
final int index) {
if (banned != null)
banned.put(banned.size() + 1, index);
}
private static void populateHash(final HashMap<Integer, Integer> hash) {
// TODO Auto-generated method stub
hash.put(1, 1);
hash.put(2, 3);
hash.put(3, 1);
hash.put(4, 1);
hash.put(5, 10);
hash.put(6, 10);
hash.put(7, 10);
/*
* hash.put(8, 7); hash.put(9, 1); hash.put(10, 10); hash.put(11, 5);
* hash.put(12, 8); hash.put(13, 1); hash.put(14, 1); hash.put(15, 6);
* hash.put(16, 1); hash.put(17, 1); hash.put(18, 2);
*/
}
}
编辑:另一个更严格的版本可能如下所示:
import java.util.ArrayList;
import java.util.List;
public class Test {
static List<Integer> resultIndexes = new ArrayList<Integer>();
public static void main(final String[] args) {
List<Integer> testHashes = new ArrayList<Integer>();
populateHash(testHashes);
Integer compHand = new Integer(10);
for(int i = 0; i < testHashes.size(); i++){
if(isBanned(i)){
continue;
}
Integer valueA = testHashes.get(i);
if(valueA.equals(compHand)){
resultIndexes.add(i);
} else {
Integer valueB = compHand - valueA;
int index = testHashes.indexOf(valueB);
if(!isBanned(index) && index > -1 && index != i){
resultIndexes.add(i);
resultIndexes.add(testHashes.indexOf(valueB));
}
}
}
for(int i : resultIndexes){
System.out.println("Index: "+i+"; Value: "+testHashes.get(i));
}
}
private static boolean isBanned(final Integer i){
return resultIndexes.contains(i);
}
private static void populateHash(final List<Integer> hashes) {
hashes.add(1);
hashes.add(3);
hashes.add(1);
hashes.add(1);
hashes.add(10);
hashes.add(10);
hashes.add(10);
hashes.add(7); hashes.add(1); hashes.add(10); hashes.add(5);
hashes.add(8); hashes.add(1); hashes.add(1); hashes.add(6);
hashes.add(1); hashes.add(1); hashes.add(2);
}
}
控制台日志: 指数:1;价值:3 指数:7;价值:7 指数:4;价值:10 指数:5;价值:10 指数:6;价值:10 指数:9;价值:10 指数:11;价值:8 指数:17;价值:2
答案 1 :(得分:1)
问题出在isBannedIndex
方法中。权利应该是for (int i=1; i<=_bannedIndexHash.size(); i++)
对代码进行一点优化(设置而不是禁止使用Map,并在第二步中对i进行冗余检查):
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class Main {
static HashMap<Integer, Integer> h1 = new HashMap<Integer, Integer>();
public static void main(String[] args) {
int compHand = 10;
populateHash(Main.h1);
int iter = -1;
int[] indexArray = new int[(Main.h1.size())];
Set<Integer> bannedIndex = new HashSet<Integer>();
for (int i = 1; i <= Main.h1.size(); i++) {
if (!isBannedIndex(bannedIndex, i)) {
if (i == Main.h1.size()) {
if (compHand == Main.h1.get(i)) {
indexArray[++iter] = i;
banIndex(bannedIndex, i);
}// end if
} else {
if (compHand == Main.h1.get(i)) {
indexArray[++iter] = i;
banIndex(bannedIndex, i);
}// end if
else {
for (int j = i + 1; j <= Main.h1.size(); j++) {
if (!isBannedIndex(bannedIndex, j)) {
if ((compHand == (Main.h1.get(i) + Main.h1
.get(j)))
|| (compHand == Main.h1.get(j))) {
if (compHand == (Main.h1.get(i) + Main.h1
.get(j))) {
indexArray[++iter] = i;
indexArray[++iter] = j;
banIndex(bannedIndex, i);
banIndex(bannedIndex, j);
break;
}// end if
else {
if (compHand == Main.h1.get(j)) {
indexArray[++iter] = j;
banIndex(bannedIndex, j);
}// end if
}// end else
}// end if-condition ||
}// end if-condition &&
}// end for (j)
}// end else
}// end else
}// end ! isBannedIndex(bannedIndexHash, i)
}// end for(i)
if (iter > -1) {
System.out.println("iter > -1");
for (int i = 0; i < indexArray.length; i++) {
System.out.println(indexArray[i]);
}
}
}
private static boolean isBannedIndex(Set<Integer> bannedIndex, int index) {
// TODO Auto-generated method stub
// Log.i(TAG, "@isBannedIndex(): ");
return bannedIndex.contains(index);
}
private static void banIndex(Set<Integer> bannedIndex, int index) {
// TODO Auto-generated method stub
// Log.i(TAG, "@banIndex(): ");
if (bannedIndex != null)
bannedIndex.add(index);
}
private static void populateHash(HashMap<Integer, Integer> hash) {
// TODO Auto-generated method stub
hash.put(1, 1);
hash.put(2, 3);
hash.put(3, 1);
hash.put(4, 1);
hash.put(5, 10);
hash.put(6, 10);
hash.put(7, 10);
/*
* hash.put(8, 7); hash.put(9, 1); hash.put(10, 10); hash.put(11, 5);
* hash.put(12, 8); hash.put(13, 1); hash.put(14, 1); hash.put(15, 6);
* hash.put(16, 1); hash.put(17, 1); hash.put(18, 2);
*/
}
}