LeetCode经典编程题(五)

本文深入解析了复杂链表的深拷贝实现,通过创建节点并链接随机与下一个节点来完成。此外,还介绍了两种单数查找算法,一种用于找出数组中仅出现一次的数,另一种用于找出除所有元素出现三次外仅出现一次的数,提供了不使用额外内存和使用额外内存的解决方案。

1. copy-list-with-random-pointer

Description

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

Solution

Create all nodes then link the random and next node.

Code
/**
 * Definition for singly-linked list with a random pointer.
 * class RandomListNode {
 *     int label;
 *     RandomListNode next, random;
 *     RandomListNode(int x) { this.label = x; }
 * };
 */
import java.util.ArrayList;
public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        //create all nodes
        ArrayList<RandomListNode> list = new ArrayList<>();
        ArrayList<Integer> labels = new ArrayList<>();
        RandomListNode p = head;
        while(p!=null){
            list.add(new RandomListNode(p.label));
            labels.add(p.label);
            p = p.next;
        }
        list.add(null);
        
        p = head;
        int pointer = 0;
        while(p!=null){
            if(p.random!=null)
                list.get(pointer).random = list.get(labels.indexOf(p.random.label));
            else
                list.get(pointer).random = null;
            list.get(pointer).next = list.get(pointer+1);
            pointer++;
            p = p.next;
        }
        return list.get(0);
    }
}

2. single-number-ii

Description

Given an array of integers, every element appears three times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Solution

reference: https://www.nowcoder.com/profile/2343147/codeBookDetail?submissionId=20452002

Code

without using extra memory

public int singleNumber(int[] A) {
	int ones = 0, twos = 0;
	for(int i = 0; i < A.length; i++){
		ones = (ones ^ A[i]) & ~twos;
		twos = (twos ^ A[i]) & ~ones;
	}
	return ones;
}

use extra memory

import java.util.*;
public class Solution {
    public int singleNumber(int[] A) {
        Map<Integer, Boolean> map = new HashMap<>();
        for(int i = 0; i < A.length; i++){
            if(map.containsKey(A[i])){
                map.put(A[i], false);
            }else{
                map.put(A[i], true);
            }
        }
        for(Integer key: map.keySet()){
            if(map.get(key)){
                return key;
            }
        }
        return -1;
    }
}

3. single-number

Description

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Code
public class Solution {
    public int singleNumber(int[] A) {
        int num = 0;
        for(int i=0;i<A.length;i++){
            num = (num ^ A[i]);
        }
        return num;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值