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

FACEBOOK 电面面经

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

使用道具 举报

🔗
johnnywsd 2014-4-22 11:17:15 | 只看该作者
全局:
第二题这个code可以不
  1. def plus_one(num):
  2.     """
  3.     plus one without using plus and minus operators
  4.     """
  5.     negative_one = ~(num) ^ (num)   # -1
  6.     negative_bitwise = ~(num)
  7.     return negative_one * negative_bitwise
复制代码
附上测试用例
  1. import unittest
  2. from solution import plus_one


  3. class Test(unittest.TestCase):

  4.     def test1(self):
  5.         for a in range(-10, 10):
  6.             res = plus_one(a)
  7.             self.assertEqual(a + 1, res)

  8. if __name__ == '__main__':
  9.     unittest.main(verbosity=2)
复制代码
回复

使用道具 举报

🔗
johnnywsd 2014-4-22 11:45:02 | 只看该作者
全局:
  1. def compare(a, b):
  2.     mydict = {}
  3.     mydict['R'] = 1
  4.     mydict['G'] = 2
  5.     mydict['B'] = 3
  6.     return mydict[a] - mydict[b]


  7. def three_colors_sort(A):
  8.     """
  9.     sort three colors keep the relative order
  10.     R < G < B
  11.     """
  12.     num = len(A)
  13.     for i in range(num):
  14.         for j in range(num - i - 1):
  15.             if compare(A[j], A[j + 1]) > 0:
  16.                 A[j], A[j + 1] = A[j + 1], A[j]
  17.     return A
复制代码
回复

使用道具 举报

🔗
specialton 2014-4-22 12:39:23 | 只看该作者
全局:
这周四跟F家的HR谈,求攒人品进入下一阶段~感觉面试来的太晚了,5月就毕业,压力贼大呀!
回复

使用道具 举报

🔗
johnnywsd 2014-4-22 12:53:33 | 只看该作者
全局:
  1. def compare(a, b):
  2.     mydict = {}
  3.     mydict['R'] = 1
  4.     mydict['G'] = 2
  5.     mydict['B'] = 3
  6.     return mydict[a] - mydict[b]


  7. def three_colors_sort_quick_sort(A):
  8.     """
  9.     sort three colors don't need to keep the
  10.     relative order
  11.     R < G < B
  12.     """
  13.     _partition_1(A)
  14.     _partition_2(A)
  15.     return A


  16. def _partition_1(A):
  17.     t = 0
  18.     right = len(A) - 1
  19.     for i in range(right):
  20.         if compare(A[i], 'G') < 0:
  21.             A[t], A[i] = A[i], A[t]
  22.             t += 1
  23.     return t


  24. def _partition_2(A):
  25.     right = len(A) - 1
  26.     t = right
  27.     for i in range(right, -1, -1):
  28.         if compare(A[i], 'G') > 0:
  29.             A[t], A[i] = A[i], A[t]
  30.             t -= 1
  31.     return t
复制代码
回复

使用道具 举报

🔗
johnnywsd 2014-4-22 12:53:56 | 只看该作者
全局:
  1. def three_colors_sort_quick_sort_2(A):
  2.     """
  3.     sort three colors don't need to keep the
  4.     relative order
  5.     R < G < B
  6.     """
  7.     ridx = 0
  8.     bidx = len(A) - 1
  9.     gidx = 0
  10.     while ridx <= gidx <= bidx:
  11.         if A[gidx] == 'G':
  12.             gidx += 1
  13.         elif A[gidx] == 'R':
  14.             A[ridx], A[gidx] = A[gidx], A[ridx]
  15.             ridx += 1
  16.             gidx += 1
  17.         elif A[gidx] == 'B':
  18.             A[bidx], A[gidx] = A[gidx], A[bidx]
  19.             bidx -= 1
  20.     return A
复制代码
回复

使用道具 举报

🔗
Neal_kks 2014-12-17 10:33:14 | 只看该作者
全局:

写了一下第二题,不知道题目是不是这个意思。
  1. #include <iostream>
  2. using namespace std;

  3. int plusOne(int x){
  4.     int c = 1;
  5.     for(int i=0;i<32;++i){
  6.         if((x>>i)&c){
  7.             c = 1;
  8.             x &= ~(1<<i);
  9.         }else{
  10.             x |= 1<<i;
  11.             break;
  12.         }
  13.     }
  14.     return x;
  15. }

  16. int main(){
  17.     int x;
  18.     while(cin>>x){
  19.         cout<<plusOne(x)<<endl;
  20.     }
  21. }
复制代码
回复

使用道具 举报

🔗
zmj1989 2015-1-5 16:07:51 | 只看该作者
全局:
感谢楼主分享!
回复

使用道具 举报

🔗
动次打次 2015-1-6 15:17:55 | 只看该作者
本楼:
全局:
感谢分享............
回复

使用道具 举报

🔗
c07181864 2015-5-25 02:44:41 | 只看该作者
全局:
plus one不能用+,-用*就好了输入x, 结果就用~x*-1
回复

使用道具 举报

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

本版积分规则

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