java 来自https://leetcode.com/problems/single-number/#/description

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 来自https://leetcode.com/problems/single-number/#/description相关的知识,希望对你有一定的参考价值。

public class Solution {
    public int singleNumber(int[] nums) {
 //************ Better solution  using XOR 
        // 0 ^ N = N
        // N ^ N = 0
        
        int ans = 0;
        
        for(int n : nums){
            ans = ans ^ n;
        }
        
        return ans;
    }
}    
public class Solution {
    public int singleNumber(int[] nums) {
        
        Arrays.sort(nums);          //***
        
        int i =0;
        int j = 0;
        while (i< nums.length){
            if ( i < nums.length -1){
                j = i+1;
            }
            
            if (nums[i] != nums[j]){
                return nums[i];
            }else{
                i = i+2;
            }
        }
        return nums[0];
        
    }
}

以上是关于java 来自https://leetcode.com/problems/single-number/#/description的主要内容,如果未能解决你的问题,请参考以下文章

20-05-01

两数之和(LeetCode)

leetcode刷题两数之和

算法leetcode|剑指 Offer 27. 二叉树的镜像|226. 翻转二叉树(rust很强)

算法leetcode|剑指 Offer 27. 二叉树的镜像|226. 翻转二叉树(rust很强)

算法leetcode|剑指 Offer 27. 二叉树的镜像|226. 翻转二叉树(rust很强)