lintcode458- Last Position of Target- easy

Find the last position of a target number in a sorted array. Return -1 if target does not exist.

Example

Given [1, 2, 2, 4, 5, 5].

For target = 2, return 2.

For target = 5, return 5.

For target = 6, return -1.

用二分法模板,缩范围时想一下怎么保证当前的数字不会被丢掉就好了。

其实就是因为这种要找最后一个第一个的题目才产生这种模板的。要最后一个所以逼着改指针不写= mid - 1写= mid,所以while条件不写start < end写start + 1 < end,所以最后要判断两个数而不是一个数。

public class Solution {
    /*
     * @param nums: An integer array sorted in ascending order
     * @param target: An integer
     * @return: An integer
     */
    public int lastPosition(int[] nums, int target) {
        // write your code here
        if(nums == null || nums.length == 0){
            return -1;
        }

        int start = 0;
        int end = nums.length - 1;
        while (start + 1 < end){
            int mid = start + (end - start) / 2;
            if (nums[mid] < target){
                start = mid;
            }
            else if (nums[mid] == target){
                start = mid;
            }
            else {
                end = mid;
            }
        }

        if (nums[end] == target){
            return end;
        }

        if(nums[start] == target){
            return start;
        }

        return -1;
    }
}
时间: 2024-10-10 06:19:32

lintcode458- Last Position of Target- easy的相关文章

[lintcode 14] First Position of Target

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 If the array is [1, 2, 3, 3, 4, 5, 10], for given ta

Lintcode14 First Position of Target Solution 题解

[题目描述] 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. 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一

First Position of Target

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) { //write your code here if (nums == null || n

lintcode-easy-First Position of Target

For a given sorted array (ascending order) and atarget number, find the first index of this number inO(log n) time complexity. If the target number does not exist in the array, return -1. Example If the array is [1, 2, 3, 3, 4, 5, 10], for given targ

Last Position of Target

public class Solution { /** * @param nums: An integer array sorted in ascending order * @param target: An integer * @return an integer */ public int lastPosition(int[] nums, int target) { // Write your code here if (nums == null || nums.length == 0)

Easy UI Tree某一级别下全部折叠

(折叠前)→(折叠后) 如上图的树形结构,我想让第三级的昭通市下的所有监测区都折叠这时候,我们可以这样做: function collapseTo() { var childs = $('#mytree').tree('getChildren', $('#_easyui_tree_3')); $.each(childs, function (i, v) { if (v) { $('#mytree').tree('collapse', v.target); } }); } Easy UI Tree

prototype使用学习手册指南之Position.js

Position是prototype中定义的一个对象,提供了操作DOM中与位置相关的方法,要很好的理解元素在页面中的位置,具体代码如下,按照代码说说,其中英文是作者的注释,中文的才是偶的说明或翻译英文的注释,采用顶式注释法(注释在要说明的代码的上面)说明 // set to true if needed, warning: firefox performance problems // NOT neeeded for page scrolling, only if draggable conta

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Given a sorted array of n integers, find the starting and ending position of a given target va

LintCode Problems Link Table

Practice Makes Perfect! # Problem Link Tag Difficulty 1 A + B problem Bitwise Operation Easy 2 Trailing Zeros Math Easy 3 Digit Counts   Medium 4 Ugly Number II   Medium 5 Kth Largest Element   Medium 6 Merge Two Sorted Arrays   Easy 7 Binary Tree Se

DCC Software and Graphics System

After working with DCC software for so many years, I saw the realtime solution went forward so much, also more and more low-level. As the game engine, 10 hears ago the game engine was such a challenge that now everything become so cheap, everybody co