1.搜索插入位置
leetcode-cn.com/problems/search-insert-position/
class Solution {
public int searchInsert(int[] nums, int target) {
int left=0,right=nums.length-1;
while(left<=right){
int mid=(left+right)/2;
if(nums[mid]target){
right=mid-1;
}else{
return mid;
}
}
return left;
}
}
2.搜索二维矩阵
leetcode-cn.com/problems/search-a-2d-matrix/
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix.length == 0)
return false;
int row = 0, col = matrix[0].length-1;
while(row < matrix.length && col >= 0){
if(matrix[row][col] < target)
row++;
else if(matrix[row][col] > target)
col--;
else
return true;
}
return false;
}
3.删除排序数组重复项
leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
public int removeDuplicates(int[] nums) {
// 使用双指针
if(nums==null || nums.length == 1){
return nums.length;
}
int i = 0,j =1;
while(j list = new LinkedList();
// 结果数组
int[] result = new int[nums.length-k+1];
for(int i=0;i=0){
result[i-k+1] = nums[list.peek()];
}
}
return result;
}
}
吴邪,小三爷,混迹于后台,大数据,人工智能领域的小菜鸟。
更多请关注
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優(yōu)質文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦