算法笔记|Day1数组基础
- ☆☆☆☆☆leetcode 704. 二分查找
- ☆☆☆leetcode 35.搜索插入位置(20241009更新)
- ☆☆☆leetcode 34. 在排序数组中查找元素的第一个和最后一个位置(20241009更新)
- ☆leetcode 69. x 的平方根(20241009更新)
- ☆leetcode 367.有效的完全平方数(20241009更新)
- ☆☆☆☆☆leetcode 27. 移除元素
- ☆leetcode 26.删除排序数组中的重复项(20241009更新)
- ☆leetcode 283.移动零(20241009更新)
- ☆leetcode 844.比较含退格的字符串(20241009更新)
☆☆☆☆☆leetcode 704. 二分查找
题目链接:leetcode 704. 二分查找
题目分析
1.二分法的前提是有序数组,且本题无重复元素;
2.若采用左闭右闭区间[left,right],这决定循环满足的条件为left<=right,right初始值为n-1,每次迭代中right=mid-1;
3.若采用左闭右开区间[left,right),这决定循环满足的条件为left<right,right初始值为n,每次迭代中right=mid;
4.时间复杂度为O(log2n),空间复杂度为O(1)。
5.关于mid溢出问题
- 若采用mid=(right+left)>>1或mid=(right+left)/2,可能会出现right+left超出整形的上限,导致溢出;
- 可改写为mid=left+(right-left)/2,可以避免上述溢出问题;
- 还可以采用位运算mid=left+((right-left)>>1),优点是比直接相除的操作快。
此处要注意运算符优先级:加减>移位运算,故需要加括号
代码
1.左闭右闭区间
class Solution {
public int search(int[] nums, int target) {
int left=0,right=nums.length-1;
while(left<=right){
int mid=left+((right-left)>>1);
if(nums[mid]<target){
left=mid+1;
}else if(nums[mid]>target){
right=mid-1;
}else if(nums[mid]==target){
return mid;
}
}
return -1;
}
}
2.左闭右开区间
class Solution {
public int search(int[] nums, int target) {
int left=0,right=nums.length;
while(left<right){
int mid=left+((right-left)>>1);
if(nums[mid]<target){
left=mid+1;
}else if(nums[mid]>target){
right=mid;
}else if(nums[mid]==target){
return mid;
}
}
return -1;
}
}
☆☆☆leetcode 35.搜索插入位置(20241009更新)
题目链接:leetcode 35.搜索插入位置
题目分析
代码
class Solution {
public int search(int[] nums, int target) {
int left=0,right=nums.length-1;
while(left<=right){
int mid=(left+right)/2;
if(nums[mid]<target)
left=mid+1;
else if(nums[mid]>target)
right=mid-1;
else
return mid;
}
return -1;
}
}
☆☆☆leetcode 34. 在排序数组中查找元素的第一个和最后一个位置(20241009更新)
题目链接:leetcode 34. 在排序数组中查找元素的第一个和最后一个位置
题目分析
代码
1.分别寻找第一个位置和最后一个位置
class Solution {
public int[] searchRange(int[] nums, int target) {
int leftBound=searchLeft(nums,target);
int righrBound=searchRight(nums,target);
return new int[]{leftBound,righrBound};
}
public int searchLeft(int[] nums, int target) {
int left=0,right=nums.length-1;
while(left<=right){
int mid=(left+right)/2;
if(nums[mid]<target)
left=mid+1;
else if(nums[mid]>target)
right=mid-1;
else{
while(mid>0&&nums[mid-1]==target)
mid--;
return mid;
}
}
return -1;
}
public int searchRight(int[] nums, int target) {
int left=0,right=nums.length-1;
while(left<=right){
int mid=(left+right)/2;
if(nums[mid]<target)
left=mid+1;
else if(nums[mid]>target)
right=mid-1;
else{
while(mid<nums.length-1&&nums[mid+1]==target)
mid++;
return mid;
}
}
return -1;
}
}
2.简化版本
class Solution {
public int[] searchRange(int[] nums, int target) {
int left=0,right=nums.length-1;
while(left<=right){
int mid=(left+right)/2;
if(nums[mid]<target)
left=mid+1;
else if(nums[mid]>target)
right=mid-1;
else{
int first=mid,last=mid;
while(first>0&&nums[first-1]==target)
first--;
while(last<nums.length-1&&nums[last+1]==target)
last++;
return new int[]{first,last};
}
}
return new int[]{-1,-1};
}
}
☆leetcode 69. x 的平方根(20241009更新)
题目链接:leetcode 69. x 的平方根
题目分析
代码
class Solution {
public int mySqrt(int x) {
int left=0,right=x;
while(left<=right){
int mid=(left+right)/2;
if((long)mid*mid<x)
left=mid+1;
else if((long)mid*mid>x)
right=mid-1;
else
return mid;
}
return right;
}
}
2.优化版本
class Solution {
public int mySqrt(int x) {
int left=1,right=x;
while(left<=right){
int mid=left+((right-left)>>1);//这里要加括号
if((long)mid*mid<x)
left=mid+1;
else if((long)mid*mid>x)
right=mid-1;
else
return mid;
}
return right;
}
}
☆leetcode 367.有效的完全平方数(20241009更新)
题目分析
代码
class Solution {
public boolean isPerfectSquare(int num) {
int left=0,right=num;
while(left<=right){
int mid=(left+right)/2;
if((long)mid*mid<num)
left=mid+1;
else if((long)mid*mid>num)
right=mid-1;
else
return true;
}
return false;
}
}
☆☆☆☆☆leetcode 27. 移除元素
题目链接:leetcode 27. 移除元素
题目分析
1.可以采用暴力法(两层for循环)求解,一个for循环遍历数组元素 ,第二个for循环更新数组。时间复杂度为O(n2),空间复杂度为O(1);
2.还可以采用双指针法(快慢指针法),通过一个快指针和慢指针在一个for循环下完成两个for循环的工作,快指针寻找新数组(不含有目标元素的数组)的元素,慢指针指向更新新数组下标的位置,时间复杂度为O(n),空间复杂度为O(1);
3.还可以使用相向双指针法,因为此题元素位置可以改变,这样可以确保移动最少元素,即找左边等于val的元素和右侧不等于val的元素,并将右侧不等于val的元素覆盖左边等于val的元素,时间复杂度为O(n),空间复杂度为O(1);
代码
1.暴力法(两层for循环)
class Solution {
public int removeElement(int[] nums, int val) {
int length=nums.length;
for(int i=0;i<length;i++){
if (nums[i]==val){
for(int j=i+1;j<length;j++){
nums[j-1]=nums[j];
}
i--;
length--;
}
}
return length;
}
}
2.双指针法(快慢指针法)
class Solution {
public int removeElement(int[] nums, int val) {
int fast=0,slow=0;
for(fast=0;fast<nums.length;fast++){
if(nums[fast]!=val){
nums[slow]=nums[fast];
slow++;
}
}
return slow;
}
}
3.相向双指针法
class Solution {
public int removeElement(int[] nums, int val) {
int left=0,right=nums.length-1;
while(left<=right){
while(left<=right&&nums[left]!=val){
left++;
}
while(left<=right&&nums[right]==val){
right--;
}
if(left<right){
nums[left]=nums[right];
left++;
right--;
}
}
return left;
}
}
☆leetcode 26.删除排序数组中的重复项(20241009更新)
题目分析
代码
class Solution {
public int removeDuplicates(int[] nums) {
int fast=0,slow=0;
for(fast=0;fast<nums.length;fast++){
while(fast<nums.length-1&&nums[fast]==nums[fast+1])
fast++;
nums[slow]=nums[fast];
slow++;
}
return slow;
}
}
☆leetcode 283.移动零(20241009更新)
题目链接:leetcode 283.移动零
题目分析
代码
class Solution {
public void moveZeroes(int[] nums) {
int fast=0,slow=0;
for(fast=0;fast<nums.length;fast++){
if(nums[fast]!=0){
nums[slow]=nums[fast];
slow++;
}
}
for(;slow<nums.length;slow++)
nums[slow]=0;
return;
}
}
☆leetcode 844.比较含退格的字符串(20241009更新)
题目分析
代码
class Solution {
public boolean backspaceCompare(String s, String t) {
return build(s).equals(build(t));
}
public String build(String s){
StringBuilder sb=new StringBuilder();
char str[]=s.toCharArray();
for(char c:str){
if(c=='#'){
if(sb.length()>0)
sb.deleteCharAt(sb.length()-1);
}
else
sb.append(c);
}
return sb.toString();
}
}