查看: 3403| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

Groupon面试题

全局:

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

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

x
看到今天别人发的groupon的面经,有一道题是这样的,请问该怎么解?nt3acres.com/bbs
write a function
f(4) = four
f(14) = fourteen
f(123) = one hundred, twenty three
f(12345) = twelve thousand, three hundred, forty five


上一篇:关于三次方和三次方根的题目
下一篇:请教大神一道parallel progeamming的题
🔗
liuwujijay 2016-3-26 03:22:40 | 只看该作者
全局:
抛砖: 假设input 是 String input = “123456”。根据input.length()/3 的 结果来决定 当前单位是多少,如果input.length()/3 == 1 且input.length() % 3>0.则单位是千,然后百万,十亿。 先处理 input.length() % 3,然后三个 三个的 读。
回复

使用道具 举报

🔗
zdhzh05 2016-4-3 03:08:53 | 只看该作者
全局:
public class Solution {
        public String f(int n) {
                if (n == 0) return "zero";
                boolean pos = n > 0;
                long num = n;
                if (num < 0) num = -num;
                String[] a1 = new String[] {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
                String[] a2 = new String[] {"", "", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
                String[] base = new String[] {"", "thousand", "million", "billion"};
                int b = 0;
                int d = 1;
                while (num/d >= 1000) {
                        b++;
                        d *= 1000;
                }
                StringBuilder sb = new StringBuilder();
                while (b >= 0) {
                        int v = (int)(num/d);
                        if (v >= 100) {
                                sb.append(a1[v/100]+" hundred ");
                                v %= 100;
                        }
                        if (v < 20) {
                                sb.append(a1[v]);
                        } else {
                                sb.append(a2[v/10]+" "+a1[v%10]);
                        }
                        sb.append(" "+base[b]+" ");
                        b--;
                        num %= d;
                        d /= 1000;
                }
                String ret = sb.toString().trim();
                return pos ? ret : "minus "+ret;
        }
}

Test:

4 four
14 fourteen
123 one hundred twenty three
12345 twelve thousand three hundred fourty five
1000 one thousand
100 one hundred
10 ten
1 one
0 zero
2147483647 two billion one hundred fourty seven million four hundred eighty three thousand six hundred fourty seven
-2147483648 minus two billion one hundred fourty seven million four hundred eighty three thousand six hundred fourty eight
回复

使用道具 举报

🔗
alpher 2016-4-3 07:02:28 | 只看该作者
全局:
Leet Code 273. Integer to English Words
回复

使用道具 举报

🔗
 楼主| Sendoh2015 2016-4-4 02:27:45 | 只看该作者
全局:
多谢啊,看来还是刷题不够啊
回复

使用道具 举报

🔗
Chilu_Tsao 2018-2-25 07:42:41 | 只看该作者
全局:
楼主你好,YouTube上有个大神讲解LeetCode的题目的,这里是这道题的视频链接:https://www.youtube.com/watch?v=RwFQvF0xx_k&list=PLvyIyKZVcfAmc54SmSOw0E6Y6L9oReTeb&index=23
回复

使用道具 举报

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

本版积分规则

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