面试题56 - II. 数组中数字出现的次数 II

在一个数组中,除了一个数字只出现一次外,其余数字均出现三次。本文介绍如何找到这个唯一的数字,通过示例展示输入输出,并提供了解决方案的代码实现。

在一个数组 nums 中除一个数字只出现一次之外,其他数字都出现了三次。请找出那个只出现一次的数字。

示例 1:

输入:nums = [3,4,3,3]
输出:4

示例 2:

输入:nums = [9,1,7,9,7,9,7]
输出:1

限制:

1 <= nums.length <= 10000
1 <= nums[i] < 2^31

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:卡诺图可以做,但是我冲不起来,就用map了。。。。。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    private List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        if(root == null){
            return res;
        }
        List<Integer> list = new ArrayList<>();
        findPath(root,0,list,sum);
        return res;
    }

    public void findPath(TreeNode node,int temp,List<Integer> list,int sum){
        if(node.val + temp == sum && node.left == null & node.right == null){
            list.add(node.val);
            List <Integer> t = new ArrayList<>(list);
            res.add(t);
            list.remove(list.size() - 1);
            return;
        }
        list.add(node.val);
        if(node.left != null){
            findPath(node.left,temp + node.val,list,sum);
        }
        if(node.right != null){
            findPath(node.right,temp + node.val,list,sum);
        }
        list.remove(list.size() - 1);
        
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值