📣 独立日限时特惠: VIP通行证立减$68
12
返回列表 发新帖
楼主: Yang_butterfly
跳转到指定楼层
上一主题 下一主题
收起左侧

LinkedIn 电面

🔗
ilhrx 2014-7-31 09:50:04 | 只看该作者
全局:
两种情况, 有duplicate or not
回复

使用道具 举报

🔗
 楼主| Yang_butterfly 2014-7-31 13:22:44 | 只看该作者
全局:
第二题要注意没有rotate的情况。也就是给你array 【1,2,3,4,5,6,7】找rotate的点
回复

使用道具 举报

🔗
tiexiong 2014-8-1 02:58:38 | 只看该作者
全局:
第一题要注意Duplicates or not,写之前沟通好就行。我写了一个,是否有Duplicates都可以处理,通过LeetCode.

    public static boolean findSearchInRotatedSortedArray(int[] A, int target) {
        if (A == null || A.length < 1)
            return false;
        
        int L = 0;
        int R = A.length - 1;
        
        while (L <= R) {
            int M = (L + R) / 2;
            if (A[M] == target)
                return true;
            
            if (A[L] < A[M]) {
                if (A[L] <= target && target < A[M]) {
                    R = M - 1;
                } else {
                    L = M + 1;
                }
            } else if (A[L] > A[M]){
                if (A[M] < target && target <= A[R]) {
                    L = M + 1;
                } else {
                    R = M - 1;
                }
            } else {
                L++;
            }
        }
        return false;
    }

回复

使用道具 举报

🔗
tiexiong 2014-8-1 03:03:40 | 只看该作者
全局:
刚才的代码未能正常显示
  1. public static boolean findSearchInRotatedSortedArray(int[] A, int target) {
  2.         if (A == null || A.length < 1)
  3.             return false;
  4.         
  5.         int L = 0;
  6.         int R = A.length - 1;
  7.         
  8.         while (L <= R) {
  9.             int M = (L + R) / 2;
  10.             if (A[M] == target)
  11.                 return true;
  12.             
  13.             if (A[L] < A[M]) {
  14.                 if (A[L] <= target && target < A[M]) {
  15.                     R = M - 1;
  16.                 } else {
  17.                     L = M + 1;
  18.                 }
  19.             } else if (A[L] > A[M]){
  20.                 if (A[M] < target && target <= A[R]) {
  21.                     L = M + 1;
  22.                 } else {
  23.                     R = M - 1;
  24.                 }
  25.             } else {
  26.                 L++;
  27.             }
  28.         }
  29.         return false;
  30.     }
复制代码
回复

使用道具 举报

🔗
tiexiong 2014-8-1 03:08:37 | 只看该作者
全局:
第二题要注意duplicates,是否rotate,以下是我的code。
  1.     public static int findSmallestElement(int[] A) {
  2.         if (A == null || A.length < 2) // null, empty and one element.
  3.             return 0;

  4.         int L = 0;
  5.         int R = A.length - 1;
  6.         
  7.         if (A[R] > A[L]) // No rotation.
  8.             return A[L];

  9.         while (L < R) {
  10.             int M = (L + R) / 2;
  11.             if (L == M)
  12.                 return M + 1; // Finally the loop will reach L = M, then A[M + 1] should be the smallest element.
  13.             
  14.             if (A[L] < A[M]) { // Left is smaller than middle, then search the right half array including current middle element.
  15.                 L = M; // Not L = M + 1.
  16.             } else if (A[L] > A[M]) { // Left is greater than middle, then search the left half array including current middle element.
  17.                 R = M; // Not R = M + 1.
  18.             } else { // Left is the same as middle, just increment left pointer by one.
  19.                 L++;
  20.             }
  21.         }
  22.         return 0;
  23.     }
复制代码
回复

使用道具 举报

🔗
shire1989 2014-8-1 11:46:40 | 只看该作者
全局:
你是new grad吗
回复

使用道具 举报

🔗
MYcolting 2014-9-30 11:05:17 | 只看该作者
全局:
tiexiong 发表于 2014-8-1 03:08
第二题要注意duplicates,是否rotate,以下是我的code。

想确认下我题目理解的对不对,首先是sorted的array,其次是绕着某个pivot转了N次,最后我们也不知道是否rotate过。这样子遍历一遍数组,记录数组最后访问的最大值的index(有duplicates)情况,如果index == 数组length-1,返回0,否则返回index+1, 比如6, 7, 7, 1, 1, 2, 3, 最后访问的最大值7的index是2,这样rotate点位置就是3
回复

使用道具 举报

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

本版积分规则

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