回复: 20
跳转到指定楼层
上一主题 下一主题
收起左侧

facebook电面

全局:

2016(1-3月) 码农类General 硕士 全职@meta - 内推 - 技术电面  | | Other | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
一个笑呵呵的三哥,出了道题:自然string comparator。不知道的搜下。就是string 比较的时候考虑里面数字的大小,比如 abc9 < abc123 abc > ab9  因为char比digit重要。写的很顺利,最后run了test case 人家也说没问题。 然后就结束了。结果下了我自己ide跑发现有case 没cover,不知道他怎么想的,求过!求bless。

要的就是这样:
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
ic int compare(String l, String r) {}






上一篇:Bloomberg挂经
下一篇:Do I need prepare for the System design
推荐
liurudahai 2016-9-28 02:55:01 | 只看该作者
全局:
这题难道不是两个指针一起挪,边挪边比,如果都是字符串,直接比大小,相等继续,如果一个是字符串一个是数字,直接字符串胜,如果两个都是数字,同时遍历到数字的末尾,把整个数字取下来,对数字比大小,赢得胜,如果相等再继续重复上面的过程,如果到一个字符串已经完了还是相等,那么如果另一个字符串也同时遍历完的话就相等,不然另一个字符串胜
回复

使用道具 举报

推荐
freemail165 2016-3-28 03:46:09 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

全局:
写了下代码
  1. class NaturalComparator implements Comparator<String> {
  2.         /**
  3.          * return negative num if l < r 0 if l == r positive if l > r
  4.          */
  5.         public static void main(String[] args) {
  6.                 NaturalComparator nc = new NaturalComparator();
  7.                 System.out.println(nc.compare("abc8b", "abc7a"));
  8.         }
  9.         public  int compare(String l, String r) {
  10.                 int i = 0;
  11.                 int j = 0;
  12.                 while (i < l.length() && j < r.length()) {
  13.                         if (l.charAt(i) == r.charAt(j) && !Character.isDigit(l.charAt(i))) {
  14.                                 i++;
  15.                                 j++;
  16.                         } else {
  17.                                 if (Character.isDigit(l.charAt(i)) && Character.isDigit(r.charAt(j))) {
  18.                                         int num1 = 0;
  19.                                         int num2 = 0;
  20.                                         while (i < l.length() && Character.isDigit(l.charAt(i))) {
  21.                                                 num1 = num1 * 10 + (l.charAt(i) - '0');
  22.                                                 i++;
  23.                                         }
  24.                                         while (j < r.length() && Character.isDigit(r.charAt(j))) {
  25.                                                 num2 = num2 * 10 + (r.charAt(j) - '0');
  26.                                                 j++;
  27.                                         }
  28.                                         if (num1 < num2) {
  29.                                                 return -1;
  30.                                         } else if (num1 > num2){
  31.                                                 return 1;
  32.                                         }
  33.                                 } else {
  34.                                         if (Character.isDigit(l.charAt(i))) {
  35.                                                 return -1;
  36.                                         } else if (Character.isDigit(r.charAt(j))) {
  37.                                                 return 1;
  38.                                         } else {
  39.                                                 if (l.charAt(i) < r.charAt(j)) {
  40.                                                         return -1;
  41.                                                 } else {
  42.                                                         return 1;
  43.                                                 }
  44.                                         }
  45.                                        
  46.                                 }
  47.                         }
  48.                 }
  49.                 if (i == l.length() && j == r.length()) {
  50.                         return 0;
  51.                 }
  52.                 if (i == l.length()) {
  53.                         return -1;
  54.                 } else {
  55.                         return 1;
  56.                 }
  57.         }
  58. }
复制代码
回复

使用道具 举报

🔗
lzm0909 2016-3-23 09:44:30 | 只看该作者
全局:
请问这种情况是哪个大?  abc9,  abc001
回复

使用道具 举报

🔗
 楼主| kungfucop 2016-3-23 12:19:57 | 只看该作者
全局:
lzm0909 发表于 2016-3-23 09:44
请问这种情况是哪个大?  abc9,  abc001

abc9大,001要作为一个部分处理。
回复

使用道具 举报

🔗
dimi 2016-3-28 04:06:34 | 只看该作者
全局:

Character.isAlphabetic() 这个很有用

apache commons里的
NumberUtils.isDigits(str:String):boolean
NumberUtils.isNumber(str:String):boolean

也有用
回复

使用道具 举报

🔗
freemail165 2016-3-28 04:18:58 | 只看该作者
全局:
dimi 发表于 2016-3-28 04:06
Character.isAlphabetic() 这个很有用

apache commons里的

这个知道更好,不知道的话写一下就一行的事
回复

使用道具 举报

🔗
sealove999 2016-4-4 15:12:08 | 只看该作者
全局:
  1. class NaturalComparator implements Comparator<String> {
  2.   /**
  3.    * return negative num if l < r; 0 if l == r; positive if l > r
  4.    */
  5.   public int compare(String l, String r) {
  6.     if (l.isEmpty() && !r.isEmpty())
  7.       return -1;
  8.     if (!l.isEmpty() && r.isEmpty())
  9.       return 1;
  10.     if (l.isEmpty() && r.isEmpty())
  11.       return 0;

  12.     int ll = 0;
  13.     while (ll < l.length() && Character.isDigit(l.charAt(ll))) {
  14.       ll++;
  15.     }
  16.     int rr = 0;
  17.     while (rr < r.length() && Character.isDigit(r.charAt(rr))) {
  18.       rr++;
  19.     }

  20.     if (ll == 0 && rr > 0)
  21.       return 1;
  22.     else if (ll > 0 && rr == 0)
  23.       return -1;
  24.     else if (ll > 0 && rr > 0) {
  25.       int lll = Integer.parseInt(l.substring(0, ll));
  26.       int rrr = Integer.parseInt(r.substring(0, rr));
  27.       if (lll != rrr) {
  28.         return lll - rrr;
  29.       } else {
  30.         return compare(l.substring(ll), r.substring(rr));
  31.       }
  32.     } else {
  33.       if (l.charAt(0) != r.charAt(0)) {
  34.         return l.charAt(0) - r.charAt(0);
  35.       } else {
  36.         return compare(l.substring(1), r.substring(1));
  37.       }
  38.     }
  39.   }
  40. }


  41. public class Solution {
  42.   public static void main(String[] args) {
  43.     System.out.println(new NaturalComparator().compare("abc9", "abc123"));
  44.     System.out.println(new NaturalComparator().compare("abc", "ab9"));
  45.     System.out.println(new NaturalComparator().compare("abc9", "abc001"));
  46.     return;
  47.   }
  48. }
复制代码
回复

使用道具 举报

🔗
mdyuki1016 2016-7-27 03:43:54 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
tigercode 2016-9-13 12:22:36 | 只看该作者
全局:
mdyuki1016 发表于 2016-7-27 03:43
为何都写得如此复杂....
public int compare(String l, String r) {
    int b1 = 0, b2 =0;

abc121a, abc121b
回复

使用道具 举报

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

本版积分规则

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