lintcode14/458 First/Last Position of Target
Posted 我就是UTD的码农包
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode14/458 First/Last Position of Target相关的知识,希望对你有一定的参考价值。
//I will update this blog about my thoughts tomorrow
public int binarySearch(int[] nums, int target) { if (nums == null || nums.length == 0) { return -1; } int start = 0, end = nums.length - 1; while (start + 1 < end) { int mid = start + (end - start) / 2; if (nums[mid] == target) { end = mid; } else if (nums[mid] < target) { start = mid; } else { end = mid; } } if (nums[start] == target) { return start; } if (nums[end] == target) { return end; } return -1; }
public int lastPosition(int[] nums, int target) { if (nums == null || nums.length == 0) { return -1; } int start = 0, 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; }
以上是关于lintcode14/458 First/Last Position of Target的主要内容,如果未能解决你的问题,请参考以下文章
pandas使用groupby函数first函数last函数分别获得每个分组的第一行和最后一行数据(first/last row of each group in dataframe)
pyspark 是不是支持窗口函数(例如 first、last、lag、lead)?