有这样的一个数组A,大小为n,相邻元素差的绝对值都是1,如:{4,5,6,7,8,9,10,9},现在给定一个A和一个目标整数t,请找到t在A中的位置。
示例1
输入
[4,5,6,7,8,9,10,9],7
输出
3
说明
正常情况
示例2
输入
[4,5,6,7,8,9,10,9],11
输出
-1
说明
异常情况
加载中...
import java.util.*; public class Solution { /** * 找出给定数据位置 * @param A int整型一维数组 给定数组 * @param t int整型 目标值 * @return int整型 */ public int findPos (int[] A, int t) { // write code here } }
class Solution { public: /** * 找出给定数据位置 * @param A int整型一维数组 给定数组 * @param ALen int A数组长度 * @param t int整型 目标值 * @return int整型 */ int findPos(int* A, int ALen, int t) { // write code here } };
# # 找出给定数据位置 # @param A int整型一维数组 给定数组 # @param t int整型 目标值 # @return int整型 # class Solution: def findPos(self , A , t ): # write code here
/** * 找出给定数据位置 * @param A int整型一维数组 给定数组 * @param t int整型 目标值 * @return int整型 */ function findPos( A , t ) { // write code here } module.exports = { findPos : findPos };
# # 找出给定数据位置 # @param A int整型一维数组 给定数组 # @param t int整型 目标值 # @return int整型 # class Solution: def findPos(self , A , t ): # write code here
[4,5,6,7,8,9,10,9],7
3
[4,5,6,7,8,9,10,9],11
-1