[英]Searching a value using for-loop and
問題是:
給定一個非空的整數數組,除了一個元素外,每個元素都會出現兩次。 找一個單一的。
輸入:[4,1,2,1,2]
產量:4
我的代碼是:
public static int singleNumber(int[] nums) {
int answer = 0;
for (int i =0; i<nums.length-1; i++) {
for(int j = i+1; j<nums.length; j++) {
if(nums[i] != nums[j]) {
answer = nums[i]; //this should be where I am wrong.
}
}
}
return answer;
}
我知道輸出是4然后現在它將被改為1.我試圖找出如何在找到后不改變找到的值。
邏輯是錯誤的 - 你的內部循環找到的每個數字都不是數組中唯一的數字。
我會保留一個Set
來跟蹤我遇到的數字。 第一次遇到數字時,將其添加到Set
。 第二次遇到它時,將其從Set
刪除。 一旦你完成了數組,你就會得到一個包含單個元素的Set
,這是你的答案:
public static int singleNumber(int[] nums) {
Set<Integer> unique = new HashSet<>();
for (int num : nums) {
// add returns true if num is indeed new to unique
if (!unique.add(num)) {
unique.remove(num);
}
}
return unique.iterator().next();
}
對於這個問題,我想對數字進行按位異或。 相等的數字將相互抵消,只有一個整數將是最終值。
public static int singleNumber(int[] nums) {
int answer = 0;
for (int i =0; i<nums.length; i++) {
answer = answer ^ nums[i];
}
return answer;
}
以下對您的方法的更改將給您預期的答案
public static int singleNumber(int[] nums) {
int temp = 0;
int answer = 0;
for (int i = 0; i < nums.length; i++) {
boolean flag = true;
temp = nums[i];
for (int j = 0; j < nums.length; j++) {
if (temp == nums[j]) {
if (i != j) {// if a match found then the loop will terminate
flag = false;
break;
}
}
}
if (flag == true) {
answer = temp;
}
}
return answer;
}
以下是使用Java 8中的Collectors.groupingBy
另一種解決方案:
public static int singleNumber(int[] nums) {
return Arrays.stream(nums).boxed()
.collect(Collectors.groupingBy(a -> a, Collectors.counting()))
.entrySet().stream().filter(e -> e.getValue() == 1).findFirst().get().getKey();
}
這個想法是:
注意我假設你的數組至少包含一個元素,否則你可以在搜索之前檢查長度,然后像這樣拋出一個異常:
public static int singleNumber(int[] nums) throws IllegalArgumentException{
if(nums.length == 0){
throw new IllegalArgumentException("empty array");
}
return Arrays.stream(nums).boxed()
.collect(Collectors.groupingBy(a -> a, Collectors.counting()))
.entrySet().stream().filter(e -> e.getValue() == 1).findFirst().get().getKey();
}
更深入,如果你想避免只有一次重復多次的情況,你可以使用:
public static int singleNumber(int[] nums) throws IllegalArgumentException {
if (nums.length == 0) {
throw new IllegalArgumentException("empty array");
}
Map<Integer, Long> grouping = Arrays.stream(nums).boxed()
.collect(Collectors.groupingBy(a -> a, Collectors.counting()));
if (grouping.values().stream().filter(c -> c == 1).count() > 1) {
throw new IllegalArgumentException("more than one element is repeated one time");
}
return grouping.entrySet().stream()
.filter(e -> e.getValue() == 1).findFirst().get().getKey();
}
這是一個使用ArrayList.indexOf和ArrayList.lastIndexOf的解決方案。 如果它們相同,那么你有答案。
public static int singleNumber(int[] nums) {
int answer = 0;
//ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(nums));
ArrayList al = new ArrayList();
for (int i =0; i < nums.length; i++) {
al.add(nums[i]);
}
for (int i =0; i < nums.length; i++) {
int test = nums[i];
if(al.indexOf(test) == al.lastIndexOf(test)){
answer = nums[i];
}
}
return answer;
}
嘗試這個:
int[] nums = new int[] {4,2,1,2,1};
int answer = 0;
for (int i =0; i<nums.length-1; i++) {
int times = 0;
int target = nums[i];
for(int j : nums) {
if(j == target) {
times++;
if(times == 2) {
break;
}
}
}
if(times == 1) {
answer = target;
break;
}
}
System.out.println(answer);
你必須進入每個數字並計算數組中的meny如果只有1你立即停止循環結束設置答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.