力扣---2020.3.23

876. 链表的中间结点

//快慢指针
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast !=null &&fast.next !=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode[] A = new ListNode[100];
        int t = 0;
        while (head != null) {
            A[t++] = head;
            head = head.next;
        }
        return A[t / 2];
    }
}

面试题03. 数组中重复的数字

class Solution {
    public int findRepeatNumber(int[] nums) {
        Arrays.sort(nums);
        for(int i=1;i<nums.length;i++){
            if(nums[i]==nums[i-1]){
                return nums[i];
            }
        }
        return -1;
    }
}
//hash
class Solution {
    public int findRepeatNumber(int[] nums) {
        int[] arr = new int[nums.length];
        for(int i = 0;i<nums.length;i++){
            arr[nums[i]]++;
            if(arr[nums[i]] > 1)  return nums[i];
        }
        return -1;
    }
}
class Solution {
    public int findRepeatNumber(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        int repeat = -1;
        for (int num : nums) {
            if (!set.add(num)) {
                repeat = num;
                break;
            }
        }
        return repeat;
    }
}
//【数学-鸽巢原理】
class Solution {
    public int findRepeatNumber(int[] nums) {
        for(int i = 0; i < nums.length; i++) {
            while(nums[i] != i) {
                if(nums[i] == nums[nums[i]]) return nums[i];
                swap(nums, i, nums[i]);
            }
        }
        return -1;
    }
    private void swap1(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
    private void swap(int[] nums, int i, int j) {
        nums[i] = nums[i] ^ nums[j];
        nums[j] = nums[i] ^ nums[j];
        nums[i] = nums[i] ^ nums[j];
    }
}

面试题39. 数组中出现次数超过一半的数字

class Solution {
    public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length/2];
    }
}
class Solution {
    public int majorityElement(int[] nums) {
        //摩尔投票法
        int count = 0;
        int card = 0;
        for(int num:nums){
            if(count == 0) card = num;
            count += (card==num)?1:-1;
        }
        return card;
    }
}
lass Solution {
    public int majorityElement(int[] nums) {
        int target = nums[0];//初始化为数组的第一个元素,接下来用于记录上一次访问的值
		int count = 1;//用于记录出现次数
		for(int i = 1;i<nums.length;i++) {
			if(target == nums[i]) {
				count++;
			}else {
				count--;
			}
			
			//当count=0时,更换target的值为当前访问的数组元素的值,次数设为1
			if(count == 0) {
				target = nums[i];
				count = 1;
			}
		}
		return target;
    }
}

你知道的越多,你不知道的越多。
有道无术,术尚可求,有术无道,止于术。
如有其它问题,欢迎大家留言,我们一起讨论,一起学习,一起进步

发布了203 篇原创文章 · 获赞 131 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_40722827/article/details/105058326