题解
找出目标数字的位置
http://www.nowcoder.com/questionTerminal/a75c799f9b6845e18f536f1ecb104f3f
暴力解法
暴力遍历一遍, 比较简单易懂
利用题目条件进行快速搜索方法
每次循环遍历的时候加Math.abs(A[i]-t) 能够加快搜索速度
import java.util.*; public class Solution { /** * 找出给定数据位置 * @param A int整型一维数组 给定数组 * @param t int整型 目标值 * @return int整型 */ public int findPos (int[] A, int t) { // write code here int len = A.length; int i = 0; while(i < len) { int tmp = Math.abs(A[i] - t); if(tmp == 0) return i; else i += tmp; } return -1; } }