Java 排序插入位置
问题陈述:给定排序数组和目标值,如果找到目标,则返回索引。如果不是,则返回按顺序插入的索引。 您可以假定阵列中没有重复项 这里有几个例子Java 排序插入位置,java,algorithm,binary-search,Java,Algorithm,Binary Search,问题陈述:给定排序数组和目标值,如果找到目标,则返回索引。如果不是,则返回按顺序插入的索引。 您可以假定阵列中没有重复项 这里有几个例子 [1,3,5,6], 5 → 2 [1,3,5,6], 2 → 1 [1,3,5,6], 7 → 4 [1,3,5,6], 0 → 0 我的代码: public class Solution { public int searchInsert(ArrayList<Integer> a, int b) { int low = 0, high
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
我的代码:
public class Solution {
public int searchInsert(ArrayList<Integer> a, int b) {
int low = 0, high = a.size()-1;
int mid = (low+high)/2;
int retIndex = mid;
while(low<=high){
if(a.get(mid)<b){
low = mid+1;
mid = (low+high)/2;
retIndex = low;
}
else{
high = mid-1;
mid = (low+high)/2;
if(high<0) retIndex = 0;
else retIndex = high;
}
}
return retIndex;
}
}
此输入的预期返回值为:149此输入的代码返回值为:148对于非常基本的测试用例,您的代码将失败: [1,3,5,6],5→ 2 当值位于mid
a.get(mid)>b
和a.get(mid)==b
分别地此外,您不需要单独维护变量retIndex
因此,请将代码更改为:
while(low<=high){
if(a.get(mid)<b){
low = mid+1;
mid = (low+high)/2;
}
else if(a.get(mid) > b){
high = mid-1;
mid = (low+high)/2;
}
else return mid;
}
return low;//handles the case when no match is found.
while(low对于非常基本的测试用例,您的代码将失败:
[1,3,5,6],5→ 2
当值位于mida.get(mid)>b
和a.get(mid)==b
另外,您不需要单独维护变量retIndex
因此,请将代码更改为:
while(low<=high){
if(a.get(mid)<b){
low = mid+1;
mid = (low+high)/2;
}
else if(a.get(mid) > b){
high = mid-1;
mid = (low+high)/2;
}
else return mid;
}
return low;//handles the case when no match is found.
while(low您需要处理相等并更改较大分支的返回
while(low<=high){
if (a.get(mid) == b) return mid;
else if(a.get(mid)<b){
low = mid+1;
mid = (low+high)/2;
retIndex = low;
}
else {
high = mid-1;
retIndex = mid;
mid = (low+high)/2;
}
}
while(low您需要处理相等并更改较大分支的返回
while(low<=high){
if (a.get(mid) == b) return mid;
else if(a.get(mid)<b){
low = mid+1;
mid = (low+high)/2;
retIndex = low;
}
else {
high = mid-1;
retIndex = mid;
mid = (low+high)/2;
}
}
while(low这个怎么样
public class Solution {
public int searchInsert(ArrayList<Integer> a, int b) {
int low = 0;
int high = a.size();
int candidateIdx = (low + high) / 2;
int candidateValue;
int prevCandidateIdx = -1;
while (low != high ) {
candidateValue = a.get(candidateIdx);
if (candidateValue == b) {
break;
} else if (candidateValue < b && prevCandidateIdx == candidateIdx -1) {
candidateIdx++;
break;
} else if (candidateValue < b) {
low = candidateIdx;
} else if (prevCandidateIdx == candidateIdx + 1) {
break;
} else {
high = candidateIdx;
}
prevCandidateIdx = candidateIdx;
candidateIdx = (low + high)/2;
}
return candidateIdx;
}
}
公共类解决方案{
公共整数搜索插入(数组列表a,整数b){
int低=0;
int高=a.大小();
int candidateIdx=(低+高)/2;
int值;
int-prevadidx=-1;
while(低!=高){
candidateValue=a.get(candidateIdx);
如果(候选值==b){
打破
}else if(candidateValue
这个怎么样
public class Solution {
public int searchInsert(ArrayList<Integer> a, int b) {
int low = 0;
int high = a.size();
int candidateIdx = (low + high) / 2;
int candidateValue;
int prevCandidateIdx = -1;
while (low != high ) {
candidateValue = a.get(candidateIdx);
if (candidateValue == b) {
break;
} else if (candidateValue < b && prevCandidateIdx == candidateIdx -1) {
candidateIdx++;
break;
} else if (candidateValue < b) {
low = candidateIdx;
} else if (prevCandidateIdx == candidateIdx + 1) {
break;
} else {
high = candidateIdx;
}
prevCandidateIdx = candidateIdx;
candidateIdx = (low + high)/2;
}
return candidateIdx;
}
}
公共类解决方案{
公共整数搜索插入(数组列表a,整数b){
int低=0;
int高=a.大小();
int candidateIdx=(低+高)/2;
int值;
int-prevadidx=-1;
while(低!=高){
candidateValue=a.get(candidateIdx);
如果(候选值==b){
打破
}else if(candidateValue
我们不喜欢到外部站点检查您的代码在做什么!请阅读并相应地更新问题。如果您有不起作用的示例,请将它们包括在这里!@ScaryWombat可能有性能方面的考虑。@ScaryWombatmid
将复杂性降低到O(日志n)
@GhostCat我知道我不应该在这里包括链接,因为没有人有时间为我检查链接。但是,显示不同o/p的测试用例非常大,如果我在这里包括,可能会激怒其他人。请阅读。我们不要求你把所有内容都放在这里。我们要求你简单地提供一个、两个预期和实际的示例放置不匹配。而否决票进来是因为你没有放置,而是坚持让我们检查任何非现场的内容。我们不喜欢到外部网站检查你的代码在做什么!请阅读并相应地更新问题。如果你有不起作用的示例,请将它们包括在这里!@ScaryWombat可能会有性能考虑。@ScaryWombatmid
将复杂性降低到O(logn)
@GhostCat我知道我不应该在这里包括链接,因为没有人有时间为我检查链接。但是,显示不同o/p的测试用例非常大,如果我在这里包括,可能会激怒其他人。请阅读。我们不要求你把所有内容都放在这里。我们要求你简单地提供一个、两个预期和实际的示例put不匹配。而否决票的出现是因为您没有张贴,而是坚持让我们查看场外的任何内容。这可能还不够,因为示例没有精确匹配。此代码仍然在问题中提到的输入上提供意外的输出。@Rohit。希望现在它能起作用。谢谢。我有此pr唯一的问题是我没能抓住。其他人也没能抓住,但他们没有给出直接的答案,也没有错误地将问题哲学化,这是不必要的。抓住的是:“你在寻找最少的元素>=x”。:-)这可能还不够,因为示例没有精确匹配。此代码仍然会对问题中提到的输入提供意外的输出。@Rohit。希望现在它能工作。谢谢。我只是遇到了这个问题,我没能抓住。其他人也没能抓住,但没有给出直接的答案,也没有错误地将问题哲学化n必需的.Catch是:“您正在查找最少的元素>=x.”:-)仍然对问题中的输入给出意外结果。仍然对问题中的输入给出意外结果。