1. 合并两个有序数组
用时:5ms
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i=0,j=0,temp1,temp2;
while(j<n&&i<(m+j)){
// 找到插入点
if(nums1[i]<=nums2[j]){
++i;
continue;
}
// 插入nums2的元素
temp1=nums1[i];
nums1[i++]=nums2[j++];
// 将插入点后的元素右移
for(int s=i;s<(m+j);++s){
temp2=nums1[s];
nums1[s]=temp1;
temp1=temp2;
}
}
while(j<n){
nums1[i++]=nums2[j++];
}
}
}
2. 第一个错误的版本
用时:17ms
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int s=1,e=n,c=(n+1)/2;
while(s!=e){
if(isBadVersion(c)){
e=c;
}else{
s=c+1;
}
// 两个大int相加会导致溢出
c=(int)(s/2.0+e/2.0);
}
return c;
}
}