📣 独立日限时特惠: VIP通行证立减$68
查看: 2539| 回复: 10
跳转到指定楼层
上一主题 下一主题
收起左侧

求解 Search in Rotated Sorted Array

全局:

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

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

x
题目:/*Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.
*/
结果显示:Status: Runtime Error,Last executed input:[1], 0。 不知哪里错了 求大神解救!
我的程序:
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int search(vector<int>& nums, int target) {
                int first,last;
                vector<int> result;
                while(nums[first]!=nums[last])
                {
                        const int mid=first+0.5*(last-first);
                        if (nums[first] <= nums[mid]) {
                                if (nums[first] <= target && target < nums[mid])
                                        last = mid;
                                else
                                        first = mid + 1;
                                }
                        else
                        {
                                if (nums[mid] < target && target <= nums[last-1])
                                        first = mid + 1;
                                else
                                last = mid;
                        }

                }
       return -1;
    }
};





上一篇:CC150 有没有像leetcode 的online judge?
下一篇:lintcode word break一问
🔗
stellari 2015-6-2 14:23:10 | 只看该作者

回帖奖励 +1

全局:
first和last都需要初始化。另外你的那个result数组是拿来干什么的?
回复

使用道具 举报

🔗
oio14644 2015-6-8 02:38:46 | 只看该作者

回帖奖励 +1

全局:
mid 不是一直变吗,为什么前面加 const
回复

使用道具 举报

🔗
57656929bb 2015-6-8 03:19:14 | 只看该作者

回帖奖励 +1

全局:
你正确结果在哪儿return的。。。
回复

使用道具 举报

🔗
2006reload 2015-6-8 21:06:22 | 只看该作者

回帖奖励 +1

全局:
不知道lz这样看明白了没有


class Solution {
public:
    int search(vector<int>& nums, int target) {
        int first=0,last=nums.size()-1;
        while(first <= last){
            int mid=first+(last-first)/2;
            if(nums[mid] == target) return mid;
            if (nums[first] <= nums[mid]) {
                if (nums[first] <= target && target < nums[mid])
                    last = mid-1;
                else
                    first = mid + 1;
            }else {
                if (nums[mid] < target && target <= nums[last])
                    first = mid + 1;
                else
                    last = mid-1;
            }
        }
       return -1;
        
    }
};
回复

使用道具 举报

🔗
love1point 2015-6-9 10:41:10 | 只看该作者

回帖奖励 +1

全局:
Java 版AC过的供楼主参考:
  1. public class Solution {
  2.     /**
  3.      *@param A : an integer rotated sorted array
  4.      *@param target :  an integer to be searched
  5.      *return : an integer
  6.      */
  7.     public int search(int[] A, int target) {
  8.         // write your code here
  9.         int l = 0;
  10.         int r = A.length - 1;
  11.         
  12.         for(int i = 0; i < A.length; i++)
  13.         {
  14.             int mid = (l+r) / 2;
  15.             if(A[mid] == target)
  16.             {
  17.                 return mid;
  18.             }
  19.             else if(A[mid] < A[r])
  20.             {
  21.                 if(target > A[mid] && target <= A[r])
  22.                 {
  23.                     l = mid + 1;
  24.                 }
  25.                 else
  26.                 {
  27.                     r = mid - 1;
  28.                 }
  29.                
  30.             }
  31.             else
  32.             {
  33.                 if(target < A[mid] && target >= A[l])
  34.                 {
  35.                     r = mid - 1;
  36.                 }
  37.                 else
  38.                 {
  39.                     l = mid + 1;
  40.                 }
  41.             }
  42.         }
  43.         return -1;
  44.     }
  45. }
复制代码
回复

使用道具 举报

🔗
liuzonyuan 2015-6-10 09:24:29 | 只看该作者

回帖奖励 +1

全局:
用first<=last来控制循环会好写一点,另外循环里面没有return啊?
回复

使用道具 举报

🔗
yuwang1986s 2015-6-11 16:33:48 | 只看该作者

回帖奖励 +1

全局:
推荐 用first < last-1 作为循环条件
回复

使用道具 举报

🔗
iker01 2015-6-12 00:38:56 | 只看该作者

回帖奖励 +1

全局:
对于binary search类的题目,建议使用left + 1 < right作为循环条件,之后再判断left和right上的value。推荐模板:
http://www.shuatiblog.com/blog/2014/06/08/NineChap-Binary-Search/
回复

使用道具 举报

🔗
sugar 2017-8-15 02:49:17 | 只看该作者

回帖奖励 +1

全局:
同意楼上,亲测好用
回复

使用道具 举报

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

本版积分规则

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