基础算法学习

基础算法学习

1.双指针

双指针思想:一个数组,设定左右指针,left 和 right,左指针指向数组第一项,右指针指向数组最后一项。相加得出一个和sum,与target值相比较。根据比较的结果,对left和right进行移动。但需注意的是,使用双指针,数组得是有序的,如果数组无序,需要先是使用Arrays.sort()进行排序。

16. 最接近的三数之和 - 力扣(LeetCode)

j 复制代码
class Solution {
    public int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);
        int closeSum = nums[0]+nums[1]+nums[2];
        for(int i=0;i<nums.length-2;i++){
            int left = i+1;
            int right =nums.length-1;
            while(left<right){
                int sum2 = nums[i] + nums[left]+nums[right];
                if(Math.abs(target-sum2)<Math.abs(target-closeSum)){
                    closeSum = sum2;
                }
                if(sum2>target){
                    right--;
                }else if(target>sum2){
                    left++;
                }else{
                    return target;
                }
            }
           
        }
        return closeSum;
    }
}

18. 四数之和 - 力扣(LeetCode)

j 复制代码
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for(int i=0;i<nums.length-3;i++){
            if(i>0&&nums[i] == nums[i-1]) continue;
            for(int j =i+1;j<nums.length-2;j++){
                if(j>i+1&&nums[j] == nums[j-1]) continue;
                int left = j+1;
                int right = nums.length -1;
                while(left<right){
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    if(sum == target){
                     res.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
                        while (left < right && nums[left] == nums[left + 1]) left++;
                        while (left < right && nums[right] == nums[right - 1]) right--;
                        left++;
                        right--;
                    }else if(sum > target){
                        right--;
                    }else{
                        left++;
                    }
                }
            }
        }
        return res;
    }
}

611. 有效三角形的个数 - 力扣(LeetCode)

j 复制代码
class Solution {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length-1;
        int sum = 0;
        for(int i =n;i>1;i--){
            int left = 0;
            int right = i-1;
            while(left<right){
                if(nums[left] +nums[right]>nums[i]){
                    sum += right- left;
                    right--;
                }else{
                   left++;
                }
            }
        }
        return sum;
    }
}

二分查找

二分查找原理就是通过将left+right的中间值与target进行比较,然后移动左右指针,来缩小未检索范围。

34. 在排序数组中查找元素的第一个和最后一个位置 - 力扣(LeetCode)

j 复制代码
class Solution {
    public int[] searchRange(int[] nums, int target) {
        int start = lowerBound(nums,target)
        if( start == nums.length || nums[start] != target){
            return new int[]{-1,-1}
        }
        int end = lowerBound(nums,target+1)-1
        return new int[]{start,end}
    }
}

private int lowerBound( int[] nums,int target){
    int left = 0;
    int right = nums.length()-1;
    while(left<=right){
        int mid = left + (right - left) / 2;
        if(nums[mid] >= target){
            right = mid-1;
        } else {
            left = mid + 1;
        }
    }
    return left
}

162. 寻找峰值 - 力扣(LeetCode)

java 复制代码
class Solution {
    public int findPeakElement(int[] nums) {
        int left = 0;
        int right = nums.length-2;
        while(left<=right){
            int mid = left + (right-left) / 2;
            if(nums[mid] > nums[mid+1]){
                right=mid-1;
            }else{
                left=mid+1;
            }
        }
        return left;
    }
}

dfs

全排列
java 复制代码
class Solution {
    private int[] nums;
    private boolean[] onPath;
    private List<Integer> path;
    private final List<List<Integer>> ans = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        this.nums = nums;
        path = Arrays.asList(new Integer[nums.length]);
        onPath = new boolean[nums.length];
        dfs(0);
        return ans;
    }

    private void dfs(int i){
        if(i == nums.length){
            ans.add(new ArrayList<>(path));
            return;
        }
        for(int j =0;j<nums.length;++j){
            if(!onPath[j]){
                path.set(i,nums[j]);
                onPath[j] = true;
                dfs(i+1);
                onPath[j] = false;
            }
        }
    }

}
N皇后
java 复制代码
class Solution {
    public List<List<String>> solveNQueens(int n) {
        List<List<String>> ans = new ArrayList<>();
        int [] queens = new int[n];
        boolean [] col = new boolean[n];
        boolean [] diag1 = new boolean[n*2-1];
        boolean [] diag2 = new boolean[n*2-1];
        dfs(0,queens,col,diag1,diag2,ans);
        return ans;
    }


   private static void dfs(int r,int[] queens,boolean [] col,boolean[] diag1,boolean[] diag2,List<List<String>> ans){
    int n = col.length;
    if(r == n){
        List<String> board = new ArrayList<>();
        for(int c :queens){
            char [] row = new char[n];
            Arrays.fill(row,'.');
            row[c] = 'Q';
            board.add(new String(row));
        }
        ans.add(board);
        return;
    }

    for(int c=0;c<n;c++){
        int rc = r-c+n-1;
        if(!col[c]&&!diag1[r+c]&&!diag2[rc]){
            queens[r] =c;
            col[c] = diag1[r+c] = diag2[rc] = true;
            dfs(r+1,queens,col,diag1,diag2,ans);
              col[c] = diag1[r+c] = diag2[rc] = false;
        }
    }
   }

}
相关推荐
creator_Li2 小时前
Kafka 全面技术笔记
笔记·学习·kafka
小O的算法实验室2 小时前
2026年ASOC,学习驱动人工蜂群算法+移动机器人多目标路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
楼田莉子2 小时前
设计模式:构造器模式
开发语言·c++·后端·学习·设计模式
南境十里·墨染春水2 小时前
linux学习进展 进程
linux·运维·学习
sp_fyf_20242 小时前
【大语言模型】 语言模型学习什么以及何时学习?隐式课程假说
人工智能·学习·语言模型
星辰即远方2 小时前
UI学习2
学习·ui
wfbcg2 小时前
每日算法练习:LeetCode 30. 串联所有单词的子串 ✅
算法·leetcode·职场和发展
玉树临风ives2 小时前
atcoder ABC 453 题解
数据结构·c++·算法·图论·atcoder
拥抱AGI2 小时前
Qwen3.5开源矩阵震撼发布!从0.8B到397B,不同规模模型性能、显存、速度深度对比与选型指南来了!
人工智能·学习·程序员·开源·大模型·大模型训练·qwen3.5