Practice30:一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。

这篇博客介绍了两种方法来找出整型数组中仅出现一次的两个数字。S1 使用哈希映射,遍历数组并记录每个数字出现的次数,然后找出出现一次的数字。S2 利用异或操作的性质,将数组中所有元素异或,结果是两个出现一次的数字异或,通过位运算找到这两个数字。这两种方法都是高效的解决方案。

Practice30:

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。

S1

class Solution {
public:
    void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
        map<int,int> mp;
        for(int val:data){
            ++mp[val];
        }
        bool first = true;
        for(auto k:mp){
            if(k.second==1){
                if(first){
                    *num1 = k.first;
                    first = false;
                }
                else{
                    *num2 = k.first;
                }
            }
        }
    }
};

S2

牛客解题之一

异或运算:如果a、b两个值不相同,则异或结果为1。如果a、b两个值相同,异或结果为0。

  • n^0 = n;
  • n^n = 0;
  • n^n^m = n^(n^m) 满***换律

所以,我们可以让数组中的每一个数异或一下,最后会得到一个结果ret,就是两个出现一次的数字的异或结果这个结果肯定是由两个不同数字异或而来,因此我们找ret二进制中为1的位置i,因为1一定是由0,1异或而来,因此要求得两个数中,一定有一个数的二进制中的第i个位置为1, 一个为0.

如何找到位置i?可用i = ret ^ (-ret)
因为计算机用补码存取二进制数,而负数的补码为反码+1,举个例子
假如ret = 1110 , -ret = 0010 , 所以 i = 0010
所以,再异或一遍即可得到答案。

class Solution {
public:
    void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
        int ret = 0;
        for (const int k : data) ret ^= k;
        ret &= (-ret);
        *num1 = 0, *num2 = 0;
        for (const int k : data) {
            if (k & ret) *num1 ^= k;
            else *num2 ^= k;
        }
    }
};

 

冒泡排序法每次只比较相邻两个元素,升序排序时,如果前面元素大于后面元素则交换,第一轮会将最大元素放到末尾,第二轮将次大元素放至倒数第二个位置,依次类推,最终得到升序序列。以下是几种使用 Java 语言运用冒泡排序算法对整数组进行升序排序的实现示例: 示例 1: ```java package come.base; public class maopaopaixu { public static void main(String[] args) { int [] nums = {12,23,7,58,29,44}; System.out.println("排序前:"); for (int num : nums) { System.out.print(num + " "); } for (int i = 0; i < nums.length - 1; i++) { for (int j = 0; j < nums.length - 1 - i; j++) { if (nums[j] > nums[j + 1]) { int temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } System.out.println(); System.out.println("排序后:"); for (int num : nums) { System.out.print(num + " "); } } } ``` 示例 2: ```java package com.company; import java.util.Arrays; public class Demo { public static void main(String[] args) { int [] arr = {3,5,18,4,6}; bubbleSort(arr); System.out.println(Arrays.toString(arr)); } public static void bubbleSort(int [] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } } } ``` 示例 3: ```java public class practice { public static void main(String[] args) { int[] arr = {2, 5, 3, 6, 4, 7}; bubbleSort(arr); System.out.println(Arrays.toString(arr)); } private static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } } } ``` 示例 4: ```java public class WER { public static void main(String[] args) { int[] arr = {3, 34, 43, 45, 54, 12}; bubbleSort(arr); System.out.println(Arrays.toString(arr)); } public static void bubbleSort(int[] arr1) { for (int a = 0; a < arr1.length; a++) { for (int b = arr1.length - 1; b > a; b--) { if (arr1[b - 1] > arr1[b]) { int tmp = arr1[b - 1]; arr1[b - 1] = arr1[b]; arr1[b] = tmp; } } } } } ``` 示例 5: ```java import java.util.Arrays; public class Test46 { public static void main(String[] args) { int[] arr = {2,34,1,4,0,642}; bubbleSort(arr); System.out.println(Arrays.toString(arr)); } public static void bubbleSort(int[] arr1) { for (int a = 0; a < arr1.length; a++) { for (int b = arr1.length - 1; b > a; b--) { if (arr1[b - 1] > arr1[b]) { int tmp = arr1[b - 1]; arr1[b - 1] = arr1[b]; arr1[b] = tmp; } } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值