基础算法学习

基础算法学习

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;
        }
    }
   }

}
相关推荐
罗西的思考13 小时前
机器人 / 强化学习】HIL-SERL:人类在环驱动的具身智能进化框架
人工智能·算法·机器学习
美团技术团队16 小时前
LongCat 开源 VitaBench 2.0:长期动态智能体基准新标杆
人工智能·算法
To_OC1 天前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode
To_OC1 天前
LC 208 实现 Trie 前缀树:曾被名字劝退,写完发现是送分题
javascript·算法·leetcode
BadBadBad__AK1 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
_清歌2 天前
DSpark 深度解读:DeepSeek-V4 如何用「半自回归」把推理速度提升 85%
算法
统计实现局2 天前
SVD 的三步走:双对角化、Givens 收敛、排序
算法
躬行见万象2 天前
《VLA 系列》UniLab 强化训练 | G1 机器人 |复现
算法
统计实现局2 天前
对称不定分解(Bunch-Kaufman):为什么 Cholesky 不够用
算法