LeetCode 318. Maximum Product of Word Lengths (状态压缩)
题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符。(字符只考虑小写字母)。
题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来表示它含有哪些字符。
代码如下:
class Solution {
public:
int maxProduct(vector<string>& words) {
int maxn=0;
int *Bit=new int[words.size()];
fill(Bit,Bit+words.size(),0);
for(int i=0;i<words.size();++i){
int t=0;
for(int j=0;j<words[i].size();++j)
Bit[i]|=(1<<(words[i][j]-'a'));
for(int j=0;j<i;++j){
if(Bit[i]&Bit[j]) continue;
maxn=max(maxn,(int)(words[i].size()*words[j].size()));
}
}
delete []Bit;
return maxn;
}
};
LeetCode 318. Maximum Product of Word Lengths (状态压缩)的更多相关文章
- leetcode 318. Maximum Product of Word Lengths
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)
https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...
- Java [Leetcode 318]Maximum Product of Word Lengths
题目描述: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where ...
- [leetcode]318. Maximum Product of Word Lengths单词长度最大乘积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算
先介绍下本题的题意: 在一个字符串组成的数组words中,找出max{Length(words[i]) * Length(words[j]) },其中words[i]和words[j]中没有相同的字母 ...
- 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...
- LeetCode 【318. Maximum Product of Word Lengths】
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 318. Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
随机推荐
- 学写网页 #04# w3school
索引: HTML 输入类型 XHTML HTML5 HTML5 样式指南和代码约定 WHO 成立于 1948 年. 对缩写进行标记能够为浏览器.翻译系统以及搜索引擎提供有用的信息. code 元素不保 ...
- python-随机数的产生random模块
random模块用来产生随机数: 查看random模块的方法: import random random.__dir__ Out[39]: <function __dir__> rando ...
- 解决mysql的Too many connections
解决: /etc/my.cnf vim编辑 添加 max_connections= wait_timeout= 然后执行code service mysqld reload service mysql ...
- USACO 1.3 Ski Course Design - 暴力
Ski Course Design Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer ...
- linux下使用grep在当前目录下搜索所有文件中含有的字符串
grep -r yourstr ./ 举例: grep -r sprintf ./ (在当前目录下递归查找含有字符串sprintf的文件)
- swift设计模式学习 - 策略模式
移动端访问不佳,请访问我的个人博客 设计模式学习的demo地址,欢迎大家学习交流 策略模式 策略模式定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户. ...
- 论文笔记——PRUNING FILTERS FOR EFFICIENT CONVNETS
论文地址:https://arxiv.org/abs/1608.08710 主要思想 这篇文章主要讲了对filters的裁剪,裁剪方法是计算L1范数,然后裁剪掉较少的,多少取决于加速比. 实现效果 V ...
- C#学习笔记(十):函数和参数
函数 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...
- 【Android实验】组件通信Intent
实验目的 [TOC] 了解使用Intent进行组件通信原理 掌握使用Intent启动Activity的方法 熟悉和掌握Android组件间通信的方式和技巧 实验要求 设计一个主Activity和一个子 ...
- 【Coursera】Security Introduction -Ninth Week(1)
前言 Coursera 的 Internet History,Technology,and Security 进入最后一周的学习了,在这最后一周内,需要进行的内容是 public-key 公钥系统的讲 ...