毕业生面试题

十大经典排序算法
https://www.runoob.com/w3cnote/ten-sorting-algorithm.html
Java最新常见面试题 + 答案汇总
https://blog.csdn.net/fangchao2011/article/details/89203535

 

11、输入一个递增排序的数组nums和一个数字target,在数组中查找两个数,使得它们的和正好是target。如果有多对数字的和等于target,输出全部组合。30

要求: 要考虑时间复杂度和空间复杂度
如果数组中不存在目标值,返回 [-1, -1]。
示例 1:
输入: nums = [1,2,4,7,8,11,15], target = 15
输出: [4,11], [7,8]
示例 2:
输入: nums = [5,7,9,10,13], target = 23
输出: [-1,-1]

 

12、输入一个正数target,打印出所有和为target的连续正数序列(至少含有两个数)。例如输入15,
由于1+2+3+4+5=4+5+6=7+8=15,所以结果打印出3个连续序列1~5、4~6和7~8。35

 

public static void main(String[] args) {

//num=[1,2,4,7,8,11,15]  target=15  输出  [4,11]  [7,8]              ,//11

//findContinueSequence(15);

int[] num= new int[] {1,3,5,6,8,9};   //输入一个正数 target =13  ,//12
System.out.println(FindNumbersWithSum(num,13));
}

public static ArrayList<Integer> FindNumbersWithSum(int[]num, Integer target) {
int p = 0;
int q = num.length- 1;
ArrayList<Integer> rst = new ArrayList<Integer>();

while (p < q) {
if (num[p] + num[q] == target) {
rst.add(num[p]);
rst.add(num[q]);
break;
} else if (num[p] + num[q] > target) {
q--;
} else {
p++;
}
}
return rst;
}


private static void findContinueSequence(int target) {
if (target < 3) {
return;
}
int start = 1;
int end = 2;
int mid = (1 + target) / 2;
int sum = start + end;
while (start < mid) {
if (sum == target) {
printNum(start, end);
}
while (sum > target && start < mid) {
sum -= start;
start++;
if (sum == target) {
printNum(start, end);
}
}
end++;
sum += end;

}
}

private static void printNum(int start, int end) {
for (int i = start; i <= end; i++) {
System.out.print(i + " ");
}
System.out.println();

}

posted @ 2020-02-19 16:30  kelelipeng  阅读(277)  评论(0编辑  收藏  举报