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

[Leetcode] Help: wired output order problem in TwoSum sorted

全局:

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

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

x
public class Test{

     public static void main(String []args){
         int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
         int target = 15;
         TwoSumSorted twoSumSorted = new TwoSumSorted();
         int[] solutions = twoSumSorted.twoSum(nums, target);
         int[] solutionsBi = twoSumSorted.twoSumBi(nums, target);
         
        System.out.printf("Solutions are %d and %d! \n", solutions[0], solutions[1]);
        System.out.printf("Binary Search method: Solutions are %d and %d! \n", solutionsBi[0], solutionsBi[1]);
     }
     
}

class TwoSumSorted{
    // binary search, time: O(nlogn), space: O(1)
    public int[] twoSumBi(int[] nums, int target){
        for(int i = 0; i < nums.length; i++){
            int diffIndex = biSearch(nums, target - nums[i]);
            if(diffIndex != -1){
                return new int[] {i + 1, diffIndex + 1};
            }
        }
        throw new IllegalArgumentException("No two sum solution exist");
    }
   
    private int biSearch(int[] nums, int target){
        int l = 0, h = nums.length - 1;
        while(l < h){
            int m = (l + h)/2;
            if(nums[m] < target){
                l = m + 1;
            }else if(nums[m] > target){
                h = m - 1;
            }else{
                return m;
            }
        }
        return -1;  // use this line will print 8, 7, why????
        //return nums[l] == target ? l : -1;  
    }
}

上一篇:双链表遍历问题
下一篇:关于递归与空间复杂度
🔗
2006reload 2015-7-14 15:42:29 | 只看该作者
全局:
private int biSearch(int[] nums, int target){
        int l = 0, h = nums.length - 1;
        while(l <= h){
            int m = (l + h)/2;
            if(nums[m] < target){
                l = m + 1;
            }else if(nums[m] > target){
                h = m - 1;
            }else{
                return m;
            }
        }
        return -1;  // use this line will print 8, 7, why????
        //return nums[l] == target ? l : -1;  
    }
回复

使用道具 举报

🔗
2006reload 2015-7-14 16:52:51 | 只看该作者
全局:
话说楼主这样写binary search 也是错的
回复

使用道具 举报

🔗
 楼主| fishriver 2015-7-15 02:17:51 | 只看该作者
全局:
2006reload 发表于 2015-7-14 15:42
private int biSearch(int[] nums, int target){
        int l = 0, h = nums.length - 1;
        whi ...

Thank you for your comments.
But I don't think <= make difference from <.
I'd love to hear your insight.
回复

使用道具 举报

🔗
 楼主| fishriver 2015-7-15 02:18:45 | 只看该作者
全局:
        return -1;  // use this line will print 8, 7, why????
The problem is this line. Why it will get 8 7 ?
回复

使用道具 举报

🔗
2006reload 2015-7-15 14:57:35 | 只看该作者
全局:
  private int biSearch(int[] nums, int target){
        int l = 0, h = nums.length - 1;
        while(l < h){
            int m = (l + h)/2;
            if(nums[m] < target){
                l = m + 1;
            }else if(nums[m] > target){
                h = m - 1;
            }else{
                return m;
            }
        }
        return -1;  // use this line will print 8, 7, why????
        //return nums[l] == target ? l : -1;  
    }

这样写7可以找到但是8找不到
所以返回8,7
回复

使用道具 举报

🔗
stellari 2015-7-15 15:34:12 | 只看该作者
全局:
fishriver 发表于 2015-7-15 02:17
Thank you for your comments.
But I don't think

差别在于。如果只用<号的话的意思是“只要找到一个位置,这个位置一定满足条件”,<=号则是在“找到位置后,还要检查一下位置上的值是否==target才能断定是否满足条件”。有些应用里面,比如“Search Insert Position”等就是“找到的肯定是能够插入的位置”。但是在这个楼主给定的情况下则必须确认最后找到的那个值是满足条件的。如果不用等于号,就必须在循环外多加一个判断。
回复

使用道具 举报

🔗
 楼主| fishriver 2015-7-22 04:21:26 | 只看该作者
全局:
2006reload 发表于 2015-7-15 14:57
private int biSearch(int[] nums, int target){
        int l = 0, h = nums.length - 1;
        wh ...

Thank you.
回复

使用道具 举报

🔗
 楼主| fishriver 2015-7-22 04:21:42 | 只看该作者
全局:
stellari 发表于 2015-7-15 15:34
差别在于。如果只用

Thank you!
回复

使用道具 举报

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

本版积分规则

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