力扣hot100做题整理91-100

1.只出现一次的数字

异或"相同为0,不同为1

java 复制代码
class Solution {
    public int singleNumber(int[] nums) {
        // 两个相同,异或后为0
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            res  = res ^ nums[i];
        }
        return res;
    }
}

2.多数元素

采用摩尔投票的方式

或者采用hash表

java 复制代码
class Solution {
    public int majorityElement(int[] nums) {
        // 摩尔投票
        int res = 0;
        int count = 0;
        for (int num : nums) {
            if (count == 0) {
                res = num;
            }
            if (num == res) {
                count++;
            } else {
                count--;
            }
        }
        return res;
    }
}

3.颜色分类

遍历两边数组,统计

java 复制代码
class Solution {
    public void sortColors(int[] nums) {
        int[] count = new int[3];
        for (int num : nums) {
            count[num]++;
        }
        int index = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < count[i]; j++) {
                nums[index++] = i;
            }
        }

    }
}

4.寻找重复数

直接排序找

java 复制代码
class Solution {
    public int findDuplicate(int[] nums) {
       Arrays.sort(nums);
       for (int i = 0; i < nums.length-1; i++) {
            if(nums[i] == nums[i+1]) {
                return nums[i];
            }
       }
       return 0;
    }
}

5.下一个排列

1.从后往前遍历,第一个递减的排序,记录左边元素a

2.从后往前遍历,找到第一个大于记录的左边元素为b

3.交换a和b

4.a右边的元素全部逆序

java 复制代码
class Solution {
    public void nextPermutation(int[] nums) {
        // 1.从后往前找出第一个升序对(i,j) nums[i] < nums[j] 123465  a[i] = 4 a[j] = 6
        int a = -1;
        for (int i = nums.length-1; i > 0; i--) {
            if (nums[i-1] < nums[i]) {
                a = i-1;
                break;
            }
        }
        if (a == -1) {
            int left = 0, right = nums.length-1;
            while (left <= right) {
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
                left++;
                right--;
            }
            return;
        }
        // 2.从后往前找第一个比a[i]大的元素,并交换位置
        for (int i = nums.length-1; i >= 0; i--) {
            if (nums[i] > nums[a]) {
               int temp = nums[i];
               nums[i] = nums[a];
               nums[a] = temp;
               break;
            }
        }

        // 3.a[j]及后面的元素逆序
        int left = a+1, right = nums.length-1;
        while (left <= right) {
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            left++;
            right--;
        }

    }
}
相关推荐
Tiny番茄4 小时前
31.下一个排列
数据结构·python·算法·leetcode
挂科是不可能出现的4 小时前
最长连续序列
数据结构·c++·算法
_Aaron___5 小时前
List.subList() 返回值为什么不能强转成 ArrayList
数据结构·windows·list
前端小L5 小时前
动态规划的“数学之魂”:从DP推演到质因数分解——巧解「只有两个键的键盘」
算法·动态规划
RTC老炮5 小时前
webrtc弱网-ReceiveSideCongestionController类源码分析及算法原理
网络·算法·webrtc
21号 15 小时前
9.Redis 集群(重在理解)
数据库·redis·算法
码农多耕地呗6 小时前
力扣146.LRU缓存(哈希表缓存.映射+双向链表数据结构手搓.维护使用状况顺序)(java)
数据结构·leetcode·缓存
晚枫~7 小时前
数据结构基石:从线性表到树形世界的探索
数据结构
hadage2337 小时前
--- 数据结构 AVL树 ---
数据结构·算法