首页 > 其他分享 >leetcode-1486-easy

leetcode-1486-easy

时间:2022-11-12 20:46:21浏览次数:56  
标签:XOR nums int start 1486 easy Array where leetcode

XOR Operation in an Array

You are given an integer n and an integer start.

Define an array nums where nums[i] = start + 2 * i (0-indexed) and n == nums.length.

Return the bitwise XOR of all elements of nums.

 

Example 1:

Input: n = 5, start = 0
Output: 8
Explanation: Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.
Where "^" corresponds to bitwise XOR operator.
Example 2:

Input: n = 4, start = 3
Output: 8
Explanation: Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.
 

Constraints:

1 <= n <= 1000
0 <= start <= 1000
n == nums.length

思路一:这题有点奇怪,直接教你怎么做...

public int xorOperation(int n, int start) {
	int result = 0;
	for (int i = 0; i < n; i++) {
		result ^= start + 2 * i;
	}

	return result;
}

思路二:看了题解,发现从数学上有 O(1) 优化的优化解

标签:XOR,nums,int,start,1486,easy,Array,where,leetcode
From: https://www.cnblogs.com/iyiluo/p/16884594.html

相关文章

  • Leetcode第790题:多米诺和托米诺平铺(Domino and tromino tiling)
    解题思路采用动态规划思路。参考题解。核心代码如下:constlonglongmod=1e9+7;classSolution{public:intnumTilings(intn){vector<vector<lo......
  • leetcode-2047-easy
    NumberofValidWordsinaSentenceAsentenceconsistsoflowercaseletters('a'to'z'),digits('0'to'9'),hyphens('-'),punctuationmarks('!','.',and......
  • LeetCode Hot1--两数之和
    两数之和给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输......
  • [回溯算法]leetcode216. 组合总和 III(c实现)
    题目找出所有相加之和为 n的 k 个数的组合,且满足下列条件:只使用数字1到9每个数字 最多使用一次 返回所有可能的有效组合的列表。该列表不能包含相同的组合两次......
  • 最大公约数 C/C++ leetcode , 辗转相除,更相减损
    #include <iostream>using namespace std;// 辗转相除法求最大公约数,用大的模小的,然后用除数模余数,该接口在新版的C++17的numeric 包中也有int gcd1(int a ,......
  • LeetCode 406.根据身高重建队列
    首先根据身高从小到大排序如果身高相等那么根据第二个值降序排序classSolution{public:vector<vector<int>>reconstructQueue(vector<vector<int>>&people){......
  • LeetCode刷题记录.Day12
    三数之和题目链接15.三数之和-力扣(LeetCode)classSolution{public:vector<vector<int>>threeSum(vector<int>&nums){vector<vector<int>>result......
  • 【leetcode_C++_二叉树_day12】层序遍历 10 && 226.翻转二叉树&&101. 对称二叉树
    1.层序遍历学会二叉树的层序遍历,可以一口气打完以下十题:102.二叉树的层序遍历107.二叉树的层次遍历II199.二叉树的右视图637.二叉树的层平均值429.N叉树的层序遍......
  • [oeasy]python0013_ASCII码表_英文字符编码_键盘字符
    ASCII码表回忆上次内容​ord(c)​​和​​chr(i)​这是俩函数这俩函数是一对,相反相成的⚖️​​ord​​通过​​字符​​找到对应的​​数字​​​​chr​​通过​​......
  • leetcode441
    排列硬币Category Difficulty Likes Dislikesalgorithms Easy(45.89%) 248 -TagsCompanies你总共有n枚硬币,并计划将它们按阶梯状排列。对于一个由k行组成的阶梯,......