1734. 解码异或后的排列

  1. 解码异或后的排列

题目描述
官方解题思路
我的解决方案`

package xortest;

public class XorTest {
    public static void main(String[] args) {
        XorTest xorTest = new XorTest();
        int[] perm = xorTest.decode(new int[]{6,5,4,6});
        for (int i = 0; i < perm.length; i++) {
            Object o = perm[i];
            System.out.print(o+" ");

        }

    }
    public int[] decode(int[] encode){
        int decodeLength = encode.length +1;           //decode数组的长度需为encode数组长度+1
        int[] perm = new int[decodeLength];
        int output = 0;
        for (int i = 1; i <= decodeLength; i++) {         //如果encode长度为4,那么decode的长度就为5,数组基本元素为1,2,3,4,5;
            output ^=i;
        }
        int odd = 0;
        for (int i = 1; i <encode.length ; i+=2) {     //如果encode长度为4,那么奇数位为1,3
            odd^=encode[i];                              //奇数位元素进行异或;
        }
        perm[0] = output^odd;            //得出perm首位的元素,由于perm[0]^perm[1] = encodep[0] 、perm[1]^perm[2] = encodep[1]...,
                                        //所以 perm[1] = encode[0]^perm[0],perm[2] = encodep[1]^perm[1]...
        for (int i = 1; i < perm.length; i++) {
            perm[i] = encode[i-1]^perm[i-1];
        }
        return perm;
    }
}

[原题链接(https://leetcode-cn.com/problems/decode-xored-permutation/)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值