LintCode : 14. First Position of Target 二分查找,重复数字第一次出现下标

本文介绍了一种在已排序数组中寻找目标数字首次出现位置的算法,通过二分查找实现O(logn)的时间复杂度。文章提供了具体的代码示例,展示了如何在不同情况下调整搜索范围,以精确找到目标值的首个位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

试题
For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity.

If the target number does not exist in the array, return -1.

Example
Example 1:
Input: [1,4,4,5,7,7,8,9,9,10],1
Output: 0

Explanation: 
the first index of  1 is 0.

Example 2:
Input: [1, 2, 3, 3, 4, 5, 10],3
Output: 2

Explanation: 
the first index of 3 is 2.

Example 3:
Input: [1, 2, 3, 3, 4, 5, 10],6
Output: -1

Explanation: 
Not exist 6 in array.

Challenge
If the count of numbers is bigger than 2^32, can your code work properly?
代码
1、如果相等的话,要查找的值将会在左区间,并且有可能就是mid所在位置。
2、循环终止条件,对于重复的数最后left和right都会定位到第一个出现的位置,这是如果我们以left==right作为循环终止条件就会陷入死循环。
3、对于返回值,我们要判断最终查找位置的值是否等于target。

public class Solution {
    /**
     * @param nums: The integer array.
     * @param target: Target to find.
     * @return: The first position of target. Position starts from 0.
     */
    public int binarySearch(int[] nums, int target) {
        int left=0, right=nums.length-1;
        while(left<right){
            int mid = left+(right-left)/2;
            if(nums[mid]==target){
                right = mid;
            }else if(nums[mid]>target){
                right = mid-1;
            }else if(nums[mid]<target){
                left = mid+1;
            }
        }
        if(nums[left]==target)
            return left;
        else
            return -1;
    }
}
### 回答1: 可以使用字符串的 `find()` 方法来查找某个字符在字符串中的位置,如果找到则返回该字符第一次出现的位置,如果未找到则返回 -1。示例代码如下: ```python s = "hello world" char = "o" pos = s.find(char) if pos != -1: print(f"The first position of {char} is {pos}") else: print(f"{char} is not found in the string") ``` 输出结果为: ``` The first position of o is 4 ``` ### 回答2: 在字符串中查找某个指定的字符,可以使用遍历的方法逐个字符地进行比较,找到与指定字符相同的字符时,输出其位置。 具体步骤如下: 1. 声明一个变量index,并初始化为-1,表示未找到指定字符时的输出值。 2. 使用for循环遍历字符串中的每个字符,循环条件是字符的下标i从0到字符串长度减1。 3. 在循环内部,使用if条件语句判断当前字符是否与指定字符相等。如果相等,则将index设置为当前字符的下标i,并使用break语句跳出循环。 4. 循环结束后,通过判断index的值是否为-1来确定是否找到了指定字符。如果index不为-1,则输出index;否则,输出-1。 简单实现的代码如下: ``` def find_character(s, target): index = -1 for i in range(len(s)): if s[i] == target: index = i break return index s = "Hello, World!" target = "o" result = find_character(s, target) print(result) ``` 以上代码将输出为4,表示指定字符"o"首次出现在字符串的索引位置为4。如果将target修改为"d",则输出为-1,表示在字符串中未找到指定字符。 ### 回答3: 题目要求在字符串中查找某个指定的字符,并输出其第一次出现的位置。如果未找到该字符,则输出-1。 假设字符串为str,指定的字符为target。我们可以使用一个循环遍历字符串中的每一个字符,然后与target进行比较。如果找到了与target相等的字符,就返回该字符在字符串中的索引位置;如果遍历完整个字符串都没有找到与target相等的字符,则返回-1。 具体步骤如下: 1. 定义一个变量index,用于记录target字符的索引位置,初始化为-1。 2. 使用一个循环遍历字符串str中的每一个字符。 3. 在循环中,判断当前字符是否与target相等。 - 如果相等,则将index更新为当前字符的索引位置,并且跳出循环。 - 如果不相等,则继续下一次循环。 4. 循环结束后,判断index的值。 - 如果index仍为-1,则表示未找到target字符,返回-1。 - 如果index不为-1,则表示找到了target字符,返回index的值。 示例代码如下所示: ```python def find_character(str, target): index = -1 for i in range(len(str)): if str[i] == target: index = i break if index == -1: return -1 else: return index ``` 这样就实现了在字符串中查找某个指定的字符,并输出其第一次出现的位置的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值