好!maximum-product-of-word-lengths
- 以后看到个数比较少,性能比较高,就要第一时间想到位操作!
这道题目mock没有通过。超时了。。。。。。
原来题目解法的思路非常非常好!
- 开始我关注于降低n*n的复杂度,但是这道题目复杂度高在每个字符串长,所以一定要问清楚题目
- 新做的记录:
- package com.company;
- import java.util.*;
- import java.util.List;
- class Solution {
- public int maxProduct(String[] words) {
- // int是32位,足够处理了。真的很牛逼
- // 以后看到个数比较少,性能比较高,就要第一时间想到位操作!
- // 开始我关注于降低n*n的复杂度,但是这道题目复杂度高在每个字符串长,所以一定要问清楚题目
- List<Integer> ilist = new ArrayList<>();
- for (int i=0; i<words.length; i++) {
- int nb = 0;
- for (int j=0; j<words[i].length(); j++) {
- nb |= 1 << (words[i].charAt(j) - 'a');
- }
- ilist.add(nb);
- }
- int ret = 0;
- for (int i=0; i<words.length; i++) {
- for (int j=i+1; j<words.length; j++) {
- if ((ilist.get(i) & ilist.get(j)) == 0) {
- if (words[i].length() * words[j].length() > ret) {
- ret = words[i].length() * words[j].length();
- }
- }
- }
- }
- return ret;
- }
- }
- public class Main {
- public static void main(String[] args) {
- // write your code here
- System.out.println("Hello");
- Solution solution = new Solution();
- String[] words= {"cdea","bdd"};
- int ret = solution.maxProduct(words);
- System.out.printf("Get ret: %d\n", ret);
- }
- }
原来做过的记录
- https://leetcode.com/problems/maximum-product-of-word-lengths/
- // 我在尽量做到比n*n效率更高
- // 对比new solution和previous solution
- // new solution 是纯n*n,优化在字符串比较
- // 对于大数据,耗时0ms
- // previous solution理论上比n*n要快,
- // 但是因为涉及vector的频繁增删、复制
- // 实际要慢的多,对于大数据耗时800+ms
- // 总的来看,stl container的复制、删除等,耗时很大
- class Solution {
- vector<pair<int, int>> vec;
- vector<string> words;
- int **DP;
- // 比较的好方法
- int *bits;
- static bool my_compair(const string &a, const string &b) {
- if (a.size() > b.size()) {
- return true;
- }
- else {
- return false;
- }
- }
- void insert(int i, int j) {
- int f = words[i].size() * words[j].size();
- int vlen = vec.size();
- int begin = 0;
- int end = vlen - 1;
- int mid;
- int tmp;
- while (begin <= end) {
- mid = (begin + end) / 2;
- tmp = words[vec[mid].first].size() * words[vec[mid].second].size();
- if (f == tmp) {
- begin = mid + 1;
- break;
- }
- else if (f > tmp) {
- end = mid - 1;
- }
- else {
- begin = mid + 1;
- }
- }
- vec.insert(vec.begin()+begin, make_pair(i, j));
- }
- int valid(int i, int j) {
- if ((bits[i] & bits[j]) != 0) {
- return 0;
- }
- return words[i].size() * words[j].size();
- }
- public:
- int maxProduct(vector<string>& w) {
- words = w;
- int wlen = words.size();
- if (wlen == 0) {
- return 0;
- }
- sort(words.begin(), words.end(), my_compair);
- // 初始化bits
- bits = new int[wlen];
- memset(bits, 0, sizeof(int)*wlen);
- for (int i=0; i<wlen; i++) {
- string wstr = words[i];
- int slen = wstr.size();
- for (int j=0; j<slen; j++) {
- bits[i] |= 1 << (wstr[j]-'a');
- }
- }
- // new solution(0 ms for big test case)
- int result = 0;
- for (int i=0; i<wlen-1; i++) {
- for (int j=i+1; j<wlen; j++) {
- if ((bits[i]&bits[j]) == 0) {
- int tmp = words[i].size() * words[j].size();
- if (tmp > result) {
- result = tmp;
- }
- }
- }
- }
- return result;
- // previous solution (800ms for big test case)
- DP = new int*[wlen];
- for (int i=0; i<wlen; i++) {
- DP[i] = new int[wlen];
- // 注意,new出来的数据初始值,不一定为0
- memset(DP[i], 0, sizeof(int)*wlen);
- }
- // 根据相乘的长度排序
- vec.push_back(make_pair(0, 1));
- DP[0][1] = 1;
- int fir;
- int sec;
- int tmp;
- while (!vec.empty()) {
- fir = vec[0].first;
- sec = vec[0].second;
- vec.erase(vec.begin());
- tmp = valid(fir, sec);
- if (tmp > result) {
- result = tmp;
- }
- if (fir + 1 < sec && DP[fir+1][sec] == 0 &&
- words[fir+1].size() * words[sec].size() > result) {
- insert(fir+1, sec);
- DP[fir+1][sec] = 1;
- }
- if (sec + 1 < wlen && DP[fir][sec+1] == 0 &&
- words[fir].size() * words[sec+1].size() > result) {
- insert(fir, sec+1);
- DP[fir][sec+1] = 1;
- }
- }
- return result;
- }
- };
- // 下面是我在 Mock里面做的,超时了。重来。
- package com.company;
- import java.awt.*;
- import java.util.*;
- import java.util.List;
- class Solution {
- public int maxProduct(String[] words) {
- // 直接用n*n*size的方法肯定不好
- // 注意限制条件, lower case的字符
- Map<Integer, Set<Integer>> mp = new HashMap<>();
- List<Integer> clist = new ArrayList<>();
- for (int i=0; i<words.length; i++) {
- clist.add(i);
- // 过滤
- char[] chs = words[i].toCharArray();
- Set<Integer> wSet = new HashSet();
- for (char ch :chs) {
- wSet.add(ch - 'a');
- }
- Iterator<Integer> iter = wSet.iterator();
- while (iter.hasNext()) {
- int key = iter.next();
- if (!mp.containsKey(key)) {
- Set<Integer> st = new HashSet<>();
- st.add(i);
- mp.put(key, st);
- }
- else {
- Set<Integer> st = mp.get(key);
- st.add(i);
- mp.put(key, st);
- }
- }
- }
- int ret = 0;
- for (int i=0; i<words.length; i++) {
- Set<Integer> oSet = new HashSet<>(clist);
- char[] chs = words[i].toCharArray();
- Set<Integer> wSet = new HashSet();
- for (char ch :chs) {
- wSet.add(ch - 'a');
- }
- Iterator<Integer> iter = wSet.iterator();
- while (iter.hasNext()) {
- int key = iter.next();
- Set<Integer> st = mp.get(key);
- oSet.removeAll(st);
- }
- iter = oSet.iterator();
- while (iter.hasNext()) {
- int index = iter.next();
- if (words[i].length() * words[index].length() > ret) {
- ret = words[i].length() * words[index].length();
- }
- }
- }
- return ret;
- }
- }
- public class Main {
- public static void main(String[] args) {
- // write your code here
- System.out.println("Hello");
- Solution solution = new Solution();
- String[] words= {};
- int ret = solution.maxProduct(words);
- System.out.printf("Get ret: %d\n", ret);
- }
- }
好!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] 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】
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 ...
- 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 ...
- 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 (Bit Manipulations)
https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...
- [Swift]LeetCode318. 最大单词长度乘积 | 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单词长度最大乘积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
随机推荐
- VS调试Libevent流程
下载源码包: libevent--stable.tar.gz 第一:编译libevent 进入VS2010命令提示,切换到libevent的所在目录 nmake /f Makefile.nmake 编 ...
- Codeforces Round #231 (Div2) 迟到的解题报告
题目A: 给一个火柴等式,可以从左边移动一根到右边,也可以从右边移到左边,但是不能移动“+”,”=“的火柴, 而且加法里面的数都要大于0(很重要的条件),基本上注意到这点的都过了,没注意的都被HACK ...
- mysql触发器使用实例
DELIMITER $$ USE `db`$$ DROP TRIGGER `member_walletinit_trigger`$$ CREATE TRIGGER `member_walletinit ...
- 利用dsniff的tcpkill杀TCP连接
利用dsniff的tcpkill杀TCP连接 Linux连接久久不能释放的现象不常见,但偶然也会发生.进程虽不复存在,但是客户端的连接咬定青山不放松,死活也不肯吐出连接,导致重启进程时因操作系统判断监 ...
- chrome 网络面板
Chrome Timeline的指标说明:Blocked.Connect.Send.Wait.Receive Blocked time includes any pre-processing time ...
- ASP.NET 4.5新特性WebAPI从入门到精通
在新出的MVC4中,增加了WebAPI,用于提供REST风格的WebService,新生成的WebAPI项目和典型的MVC项目一样,包含主要的Models.Views.Controllers等文件夹和 ...
- Masonry自动布局
介绍,入门: http://www.cocoachina.com/ios/20141219/10702.html 下载: http://code.cocoachina.com/detail/30114 ...
- 从.NET 1.1 升级到.NET 4.0 遇到 线程间操作无效: 从不是创建控件 [XX] 的线程访问它.
有两种方式解决 1.在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false;2.使用Invoke等委托函数 问题原因是 .NET2.0 以后拒绝 ...
- dom对象详解--document对象(三)
form对象 form对象代表一个HTML表单,在HTML文档中<form>每出现一次,form对象就会被创建.从dom对象层次图看,document.forms对象是当前文档所有for ...
- Spring REST for DELETE Request Method Not Supoorted
http://stackoverflow.com/questions/22055251/sending-data-with-angularjs-http-delete-request I have a ...