当前位置: 首页 > news >正文

网站页面数怎么做展台设计方案介绍

网站页面数怎么做,展台设计方案介绍,linux 建立网站,福建省亿力电力建设有限公司网站【LetMeFly】2044.统计按位或能得到最大值的子集数目:二进制枚举/DFS回溯(剪枝) 力扣题目链接:https://leetcode.cn/problems/count-number-of-maximum-bitwise-or-subsets/ 给你一个整数数组 nums ,请你找出 nums 子集 按位或 可能得到的 …

【LetMeFly】2044.统计按位或能得到最大值的子集数目:二进制枚举/DFS回溯(剪枝)

力扣题目链接:https://leetcode.cn/problems/count-number-of-maximum-bitwise-or-subsets/

给你一个整数数组 nums ,请你找出 nums 子集 按位或 可能得到的 最大值 ,并返回按位或能得到最大值的 不同非空子集的数目

如果数组 a 可以由数组 b 删除一些元素(或不删除)得到,则认为数组 a 是数组 b 的一个 子集 。如果选中的元素下标位置不一样,则认为两个子集 不同

对数组 a 执行 按位或 ,结果等于 a[0] OR a[1] OR ... OR a[a.length - 1](下标从 0 开始)。

 

示例 1:

输入:nums = [3,1]
输出:2
解释:子集按位或能得到的最大值是 3 。有 2 个子集按位或可以得到 3 :
- [3]
- [3,1]

示例 2:

输入:nums = [2,2,2]
输出:7
解释:[2,2,2] 的所有非空子集的按位或都可以得到 2 。总共有 23 - 1 = 7 个子集。

示例 3:

输入:nums = [3,2,1,5]
输出:6
解释:子集按位或可能的最大值是 7 。有 6 个子集按位或可以得到 7 :
- [3,5]
- [3,1,5]
- [3,2,5]
- [3,2,1,5]
- [2,5]
- [2,1,5]

 

提示:

  • 1 <= nums.length <= 16
  • 1 <= nums[i] <= 105

解题方法一:二进制枚举

使用一个整数二进制的每一位代表nums数组中每个元素的选择与不选。从0002nums−12^{nums}-12nums1即可枚举枚举完每个元素选与不选的情况。

  • 时间复杂度O(n×n2)O(n\times n^2)O(n×n2),其中n=len(nums)n=len(nums)n=len(nums)
  • 空间复杂度O(1)O(1)O(1)

AC代码

C++
/** @Author: LetMeFly* @Date: 2025-07-28 13:38:06* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-07-28 18:51:34*/
#if defined(_WIN32) || defined(__APPLE__)
#include "_[1,2]toVector.h"
#endifclass Solution {
public:int countMaxOrSubsets(vector<int>& nums) {int maxium = 0;for (int t : nums) {maxium |= t;}int ans = 0;int n = nums.size(), mask = 1 << n;for (int i = 0; i < mask; i++) {int thisVal = 0;for (int j = 0; j < n; j++) {if (i >> j & 1) {thisVal |= nums[j];}}ans += thisVal == maxium;}return ans;}
};

解题方法二:回溯DFS

写一个函数dfs(th, now)代表已经选(或不选)过前ththth个元素且总或值为nownownow的情况。

如果已经选了len(nums)len(nums)len(nums)个,则判断nownownow是否为numsnumsnums所有元素或的结果并返回。如果是则ans++。

否则,可以不选当前元素并递归(dfs(th, now)),也可以选当前元素并递归(dfs(th+1, now | nums[i])。

  • 时间复杂度O(n2)O(n^2)O(n2),其中n=len(nums)n=len(nums)n=len(nums)
  • 空间复杂度O(n)O(n)O(n)

AC代码

C++
/** @Author: LetMeFly* @Date: 2025-07-28 19:30:16* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-07-28 19:50:18*/
#if defined(_WIN32) || defined(__APPLE__)
#include "_[1,2]toVector.h"
#endifclass Solution {
private:int ans = 0;int maxium = 0;vector<int> nums;void dfs(int th, int now) {if (th == nums.size()) {ans += now == maxium;return;}dfs(th + 1, now);dfs(th + 1, now | nums[th]);}
public:int countMaxOrSubsets(vector<int>& nums) {for (int t : nums) {maxium |= t;}this->nums = move(nums);dfs(0, 0);return ans;}
};

解题方法三:回溯DFS+小剪枝

有没有办法提前退出dfs函数呢?有。当当前的或结果已经为最大或结果的时候,后面的元素不论或与不或都不改变最终结果了。此时假设还剩kkk个元素,那么就说明后面还有1<<k1<<k1<<k种选法。

  • 时间复杂度O(n2)O(n^2)O(n2),其中n=len(nums)n=len(nums)n=len(nums)
  • 空间复杂度O(n)O(n)O(n)

时空复杂度不变,但很多时候可以提前退出。

AC代码

C++
/** @Author: LetMeFly* @Date: 2025-07-28 19:30:16* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-07-28 19:51:20*/
#if defined(_WIN32) || defined(__APPLE__)
#include "_[1,2]toVector.h"
#endifclass Solution {
private:int ans = 0;int maxium = 0;vector<int> nums;void dfs(int th, int now) {if (now == maxium) {ans += 1 << (nums.size() - th);return;}if (th == nums.size()) {return;}dfs(th + 1, now);dfs(th + 1, now | nums[th]);}
public:int countMaxOrSubsets(vector<int>& nums) {for (int t : nums) {maxium |= t;}this->nums = move(nums);dfs(0, 0);return ans;}
};
Python
'''
Author: LetMeFly
Date: 2025-07-28 13:38:06
LastEditors: LetMeFly.xyz
LastEditTime: 2025-07-28 20:40:58
'''
from typing import Listclass Solution:def dfs(self, th: int, now: int) -> None:if now == self.M:self.ans += 1 << (len(self.nums) - th)returnif th == len(self.nums):returnself.dfs(th + 1, now)self.dfs(th + 1, now | self.nums[th])def countMaxOrSubsets(self, nums: List[int]) -> int:self.ans = 0self.nums = numsself.M = 0for t in nums:self.M |= tself.dfs(0, 0)return self.ans
Java
/** @Author: LetMeFly* @Date: 2025-07-28 13:38:06* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-07-28 20:43:13*/
class Solution {private int ans = 0;private int M = 0;private int[] nums;private void dfs(int th, int now) {if (now == M) {ans += 1 << (nums.length - th);return;}if (th == nums.length) {return;}dfs(th + 1, now);dfs(th + 1, now | nums[th]);}public int countMaxOrSubsets(int[] nums) {this.nums = nums;for (int t : nums) {M |= t;}dfs(0, 0);return ans;}
}
Go
/** @Author: LetMeFly* @Date: 2025-07-28 13:38:06* @LastEditors: LetMeFly.xyz* @LastEditTime: 2025-07-28 20:31:11*/
package main// import "fmt"var ans2044, maxium2044 int = 0, 0
var nums2044 []intfunc dfs2044(th, now int) {if now == maxium2044 {ans2044 += 1 << (len(nums2044) - th)return}if th == len(nums2044) {return}dfs2044(th + 1, now)dfs2044(th + 1, now | nums2044[th])
}func countMaxOrSubsets(nums []int) int {nums2044 = numsans2044 = 0maxium2044 = 0for _, t := range nums {maxium2044 |= t}dfs2044(0, 0)return ans2044
}// func main() {
//     fmt.Println(countMaxOrSubsets([]int{2, 2, 2}))
// }

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

千篇源码题解已开源

http://www.dtcms.com/a/534579.html

相关文章:

  • 哪个网站可以做封面公司网站搭建流程
  • 删除西部数码网站管理助手我的世界做图片网站
  • 自适应网站开发资源网站建设怎么赚钱
  • 什么是部署php网站建设网站怎么挣钱
  • 网站模板及素材用 net做网站
  • 网站改版必要性中小型企业建设网站
  • 投标建设用地是哪个网站wordpress文章名字相同的不发布
  • 沈阳网站制作思路制作企业网站的新闻显示
  • 建设学校网站策划书易烊千玺个人网站入口
  • 黑龙江省建设集团有限公司网站首页wordpress整站
  • 做网站怎么做多少钱网店设计思路怎么写
  • 摄影作品网站有哪些桂林网络公司有哪些
  • 个人网站开发开题报告刚刚
  • aspcms手机网站wampserver搭建wordpress
  • xp怎么做网站服务器基于html5的旅游网站的设计与实现
  • 免费网站建设论文一级a做爰片免费网站中文
  • 公司找网站做宣传做账中国网站有哪些
  • 旅游网站哪个做的好市桥有经验的网站建设
  • 网站建设助手 西部数码电商app开发解决方案
  • 湘潭市 网站建设ui网页设计报价
  • 杭州做网站的优质公司百度惠生活怎么做推广
  • 广元市规划和建设局网站软件开发模型有几种并简述其特点
  • 网站建设前期应该做哪些准备建设网站常见问题
  • wordpress 迁站湖北住房城乡建设厅网站
  • 代前导页的网站网站焦点图多少钱
  • 外网平面设计网站如何修改wordpress后台地址
  • 小说网站怎么建设网站建设公司找博行
  • 一个网站包括企业所得税税率2019
  • 网站开发技术项目怎么找公众号帮推广
  • 天目西路网站建设网站建设分金手指专业十七