📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
12
返回列表 发新帖
楼主: Ziyan
跳转到指定楼层
上一主题 下一主题
收起左侧

Google电面

🔗
oneshot 2015-11-17 23:54:15 | 只看该作者
全局:
楼主原来的想法是说先把list根据长度sort一下,然后从最长的开始选择?然后面试官让写俩for loop就好?
回复

使用道具 举报

🔗
oneshot 2015-11-18 14:25:18 | 只看该作者
全局:
public int maxProduct(List<String> list){
回复

使用道具 举报

🔗
bobzhang2004 2015-12-6 05:14:19 | 只看该作者
全局:
写了下代码,请问有比brute force更好的办法吗?
  1. public class MaxStringProductWithoutSameCharacter {

  2.         public static void main(String[] args) {
  3.                 List<String> strs = new ArrayList<String>();
  4.                 strs.add("abc");
  5.                 strs.add("cgdsrh");
  6.                 strs.add("egdsrh");
  7.                 strs.add("badgagerwgwe");
  8.                 strs.add("dfaaadfsdfe");
  9.                 strs.add("jtyjukuk");
  10.                 int res = findMaxStringProductWithoutSameCharacter(strs);
  11.                 System.out.println(res);
  12.         }
  13.         public static int findMaxStringProductWithoutSameCharacter(List<String> strs) {
  14.                 if (strs == null || strs.size() == 0) {
  15.                         return 0;
  16.                 }
  17.                 int max = 0;
  18.                 for (int i = 0; i < strs.size(); i++) {
  19.                         for (int j = i + 1; j < strs.size(); j++) {
  20.                                 if (!containsSameCharacter(strs.get(i), strs.get(j))) {
  21.                                         System.out.println(strs.get(i) + "  " + strs.get(j) + " " + strs.get(i).length() * strs.get(j).length());
  22.                                         max = Math.max(max, strs.get(i).length() * strs.get(j).length());
  23.                                 }
  24.                         }
  25.                 }
  26.                
  27.                 return max;
  28.         }

  29.         private static boolean containsSameCharacter(String str1, String str2) {
  30.                 HashSet<Character> set = new HashSet<Character>();
  31.                 for (char c : str1.toCharArray()) {
  32.                         set.add(c);
  33.                 }
  34.                 for (char c : str2.toCharArray()) {
  35.                         if (set.contains(c)) {
  36.                                 return true;
  37.                         }
  38.                 }
  39.                 return false;
  40.         }
  41. }
复制代码
回复

使用道具 举报

🔗
cszeus 2015-12-6 06:08:07 | 只看该作者
全局:
Ziyan 发表于 2015-10-29 13:27
嗯 我想的是把所有words根据长度group一下,先从长度最长的单词开始找,如果找到了就不用再往下找了  但 ...

感觉从长度最长的开始找,也不一定会找到最长的
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表