This is the 23rd day of my participation in Gwen Challenge

1688. Number of matches in a match

Topic describes

You are given an integer n for the number of teams in the game. The competition follows a unique format:

  • If the current number of teams is even, each team will be paired with another team. A total of N / 2 matches are played and n / 2 teams are generated to advance to the next round.
  • If the current number of teams is odd, a random round will be played and one team will advance, and the remaining teams will be paired. A total of (n-1) / 2 matches are played and (n-1) / 2 + 1 teams are produced to advance to the next round.

Returns the number of pairings made during the tournament until the winning team is determined.

link

Leetcode-cn.com/problems/co…

Algorithm implementation

class Solution {
    int res=0;
    public int numberOfMatches(int n) {
        if(n==1) return 0;
        int res=(n%2= =0? n/2:(n/2) +1);
        return numberOfMatches(res)+n/2; }}Copy the code

The results

Find the xor sum of all subsets and sum them

Topic describes

The XOR sum of an array is defined as the result of bitwise XOR of all elements in the array; If the array is empty, xor sum is 0.

  • For example, the XOR sum of the array [2,5,6] is 2 XOR 5 XOR 6 = 1.

Given an array of nums, find the xOR sum of each subset of nums, calculate and return the sum of these values.

Note: In this case, different subsets of the same element should be counted more than once.

Array A is a subset of array B only if you delete a few elements from B (or not) to get a.

link

Leetcode-cn.com/problems/su…

Algorithm implementation

class Solution {
    public int subsetXORSum(int[] nums) {
        int ans=0,length=nums.length;
        for(int i=0; i<(1<<length); i++){int res=0;
            for(int j=0; j<length; j++){if((i&(1<<j))>0){
                    res=res^nums[j];
                }
            }
            ans+=res;
        }
        returnans; }}Copy the code

The results