代码随想录五刷day12

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


一、力扣98. 所有可达路径

import java.util.*;
public class Main{
    public static List<List<Integer>> res = new ArrayList<>();
    public static List<Integer> path = new ArrayList<>();
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] graph = new int[n+1][n+1];
        for(int i = 1; i <= m; i ++){
            int from = in.nextInt();
            int to = in.nextInt();
            graph[from][to] = 1;
        }
        path.add(1);
        for(int i = 1; i <= n; i ++){
            if(graph[1][i] != 0){
                path.add(i);
                fun(graph, i, n);
                path.remove(path.size()-1);
            }
        }
        if(res.size() == 0){
            System.out.println(-1);
            return;
        }
        for(List<Integer> p : res){
            for(int i = 0; i < p.size(); i ++){
                System.out.print(p.get(i));
                if(i < p.size()-1){
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    public static void fun(int[][] graph, int cur, int n){
        if(cur == n){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = 1; i <= n; i ++){
            if(graph[cur][i] != 0){
                path.add(i);
                fun(graph,i,n);;
                path.remove(path.size()-1);
            }
        }
    }
}

邻接表

import java.util.*;
public class Main{
    public static List<List<Integer>> res = new ArrayList<>();
    public static List<Integer> path = new ArrayList<>();
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        List<List<Integer>> graph = new ArrayList<>();
        for(int i = 0; i <= n; i ++){
            graph.add(new ArrayList<Integer>());
        }
        for(int i = 1; i <= m; i ++){
            int from = in.nextInt();
            int to = in.nextInt();
            graph.get(from).add(to);
        }
        path.add(1);
        fun(graph, 1, n);
        if(res.size() == 0){
            System.out.println(-1);
            return;
        }
        for(List<Integer> p : res){
            for(int i = 0; i < p.size(); i ++){
                System.out.print(p.get(i));
                if(i < p.size()-1){
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    public static void fun(List<List<Integer>> graph, int cur, int n){
        if(cur == n){
            res.add(new ArrayList<>(path));
            return;
        }
        for(Integer t : graph.get(cur)){
            path.add(t);
            fun(graph,t,n);;
            path.remove(path.size()-1);
        }
    }
}

二、力扣797. 所有可能的路径

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        int n = graph.length;
        path.add(0);
        dfs(graph, 0, n);
        return res;
    }
    public void dfs(int[][] graph, int cur, int n){
        if(cur == n-1){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i = 0; i < graph[cur].length; i ++){
            path.add(graph[cur][i]);
            dfs(graph, graph[cur][i], n);
            path.remove(path.size()-1);
        }
    }
}

三、力扣99. 岛屿数量

在这里插入代码片import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] graph = new int[n][m];
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < m; j ++){
                graph[i][j] = in.nextInt();
            }
        }
        Deque<int[]> deq = new LinkedList<>();
        int[][] arr = new int[][]{
            {-1,0}, {1,0}, {0,-1}, {0,1}
        };
        int res = 0;
        boolean[][] flag = new boolean[n][m];
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < m; j ++){
                if(!flag[i][j] && graph[i][j] == 1){
                    deq.offerLast(new int[]{i,j});
                    while(!deq.isEmpty()){
                        int[] cur = deq.pollLast();
                        for(int k = 0; k < 4; k ++){
                            int nextX = cur[0] + arr[k][0];
                            int nextY = cur[1] + arr[k][1];
                            if(nextX < 0 || nextX >= n || nextY < 0 || nextY >= m){
                                continue;
                            }
                            if( flag[nextX][nextY] || graph[nextX][nextY] == 0){
                                continue;
                            }
                            deq.offerLast(new int[]{nextX, nextY});
                            flag[nextX][nextY] = true;
                        }
                    }
                    res ++;
                }
            }
        }
        System.out.println(res);
    }
}

四、力扣99. 岛屿数量

import java.util.*;
public class Main{
    private static int[][] arr = new int[][]{
            {-1,0}, {1,0}, {0,-1}, {0,1}
        };
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] graph = new int[n][m];
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < m; j ++){
                graph[i][j] = in.nextInt();
            }
        }
        int  res = 0;
        
        boolean[][] flag = new boolean[n][m];
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < m; j ++){
                if(!flag[i][j] && graph[i][j] == 1){
                    res ++;
                    flag[i][j] = true;
                    dfs(flag, graph, i, j, n,m);
                }
            }
        }
        System.out.println(res);
    }
    public static void dfs(boolean[][] flag, int[][] graph, int curX, int curY, int n,int m){
        for(int k = 0; k < 4; k ++){
            int nextX = curX + arr[k][0];
            int nextY = curY + arr[k][1];
            if(nextX < 0 || nextX >= n || nextY < 0 || nextY >= m){
                continue;
            }
            if( flag[nextX][nextY] || graph[nextX][nextY] == 0){
                continue;
            }
            flag[nextX][nextY] = true;
            dfs(flag, graph, nextX, nextY, n, m);
            
        }
    }
}

五、力扣100. 岛屿的最大面积

import java.util.*;

public class Main {
    private static int temp = 0;
    private static int[][] arr = new int[][] {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        int[][] graph = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                graph[i][j] = in.nextInt();
            }
        }
        boolean[][] flag = new boolean[m][n];
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!flag[i][j] && graph[i][j] == 1) {
                    flag[i][j] = true;
                    temp = 0;
                    bfs(graph, flag, i, j);
                    res = Math.max(res, temp);
                }
            }
        }
        System.out.println(res);
    }

    private static void bfs(int[][] graph, boolean[][] flag, int curX, int curY) {
        Deque<int[]> deq = new LinkedList<>();
        deq.offerLast(new int[] {curX, curY});
        temp++;
        while (!deq.isEmpty()) {
            int[] cur = deq.pollFirst();
            for (int i = 0; i < 4; i++) {
                int nextX = cur[0] + arr[i][0];
                int nextY = cur[1] + arr[i][1];
                if (nextX < 0 || nextX >= graph.length || nextY < 0 || nextY >= graph[0].length) {
                    continue;
                }
                if (flag[nextX][nextY] || graph[nextX][nextY] == 0) {
                    continue;
                }
                flag[nextX][nextY] = true;
                temp++;
                deq.offerLast(new int[] {nextX, nextY});
            }
        }
    }
}

六、力扣101. 孤岛的总面积

import java.util.*;
public class Main{
    public static int[][] arr = new int[][]{
        {-1,0}, {1,0}, {0,-1}, {0,1}
    };
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        int[][] graph = new int[m][n];
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                graph[i][j] = in.nextInt();
            }
        }
        boolean[][] flag = new boolean[m][n];
        int res = 0;
        for(int i = 0; i < m; i ++){
            if(graph[i][0] == 1)
            dfs(graph, flag, i, 0);
            if(graph[i][n-1] == 1)
            dfs(graph, flag, i, n-1);
        }
        for(int i = 0; i < n; i ++){
            if(graph[0][i] == 1)
            dfs(graph, flag, 0, i);
            if(graph[m-1][i] == 1)
            dfs(graph, flag, m-1, i);
        }
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                if(graph[i][j] == 1){
                    res ++;
                }
            }
        }
        System.out.println(res);
    }
    public static void dfs(int[][] graph, boolean[][] flag, int curx, int cury){
        flag[curx][cury] = true;
        graph[curx][cury] = 0;
        for(int i = 0; i < 4; i ++){
            int nextx = curx + arr[i][0];
            int nexty = cury + arr[i][1];
            if(nextx < 0 || nextx >= graph.length || nexty < 0 || nexty >= graph[0].length){
                continue;
            }
            if(flag[nextx][nexty] || graph[nextx][nexty] == 0){
                continue;
            }
            flag[nextx][nexty] = true;
            dfs(graph, flag, nextx, nexty);
        }
    }
}

七、力扣102. 沉没孤岛

import java.util.*;
public class Main{
    public static int[][] arr = new int[][]{
        {-1,0}, {1,0}, {0,-1}, {0,1}
    };
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        int[][] griph = new int[m][n];
        boolean[][] flag = new boolean[m][n];
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                griph[i][j] = in.nextInt();
            }
        }
        for(int i = 0; i < m; i ++){
            if(!flag[i][0] && griph[i][0] == 1){
                dfs(griph, flag, i, 0);
            }
            if(!flag[i][n-1] && griph[i][n-1] == 1){
                dfs(griph, flag, i, n-1);
            }
        }
        for(int i = 0; i < n; i ++){
            if(!flag[0][i] && griph[0][i] == 1){
                dfs(griph, flag, 0, i);
            }
            if(!flag[m-1][i] && griph[m-1][i] == 1){
                dfs(griph, flag, m-1, i);
            }
        }
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                if(!flag[i][j] && griph[i][j] == 1){
                    System.out.print(0);
                }else{
                    System.out.print(griph[i][j]);
                }
                if(j <= n-1){
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        
    }
    public static void dfs(int[][] griph, boolean[][] flag, int curx, int cury){
        flag[curx][cury] = true;
        for(int i = 0; i < 4; i ++){
            int nextx = curx + arr[i][0];
            int nexty = cury + arr[i][1];
            if(nextx < 0 || nextx >= griph.length || nexty < 0 || nexty >= griph[0].length){
                continue;
            }
            if(flag[nextx][nexty] || griph[nextx][nexty] == 0){
                continue;
            }
            dfs(griph,  flag, nextx, nexty);
        }
    }
}

八、力扣103. 水流问题

import java.util.*;
public class Main{
    public static int[][] arr = new int[][]{
        {-1,0}, {1,0}, {0,-1}, {0,1}
    };
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] griph = new int[m][n];
        boolean[][] flaga = new boolean[m][n];
        boolean[][] flagb = new boolean[m][n];
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                griph[i][j] = sc.nextInt();
            }
        }
        for(int i = 0; i < n; i ++){
            if(!flaga[0][i]){
                bfs(griph, flaga, 0, i);
            }
        }
        for(int i = 0; i < m; i ++){
            if(!flaga[i][0]){
                bfs(griph, flaga, i, 0);
            }
        }
        for(int i = 0; i < m; i ++){
            if(!flagb[i][n-1]){
                bfs(griph, flagb, i, n-1);
            }
        }
        for(int i = 0; i < n; i ++){
            if(!flagb[m-1][i]){
                bfs(griph, flagb, m-1, i);
            }
        }
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                if(flaga[i][j] && flagb[i][j]){
                    System.out.println(i + " " + j);
                }
            }
        }
    }
    public static void bfs(int[][] griph, boolean[][] flag, int x, int y){
        flag[x][y] = true;
        Deque<int[]> deq = new LinkedList<>();
        deq.offerLast(new int[]{x,y});
        while(!deq.isEmpty()){
            int[] cur = deq.pollFirst();
            for(int i = 0; i < 4; i ++){
                int nextx = cur[0] + arr[i][0];
                int nexty = cur[1] + arr[i][1];
                if(nextx < 0 || nextx >= griph.length || nexty < 0 || nexty >= griph[0].length){
                    continue;
                }
                if(flag[nextx][nexty] || griph[x][y] > griph[nextx][nexty]){
                    continue;
                }
                flag[nextx][nexty] = true;
                bfs(griph, flag, nextx, nexty);
            }
        }
    }
}

九、力扣104. 建造最大岛屿

import java.util.*;
public class Main{
    public static int[][] arr = new int[][]{
        {-1,0}, {1,0}, {0,-1}, {0,1}
    };
    public static Map<Integer,Integer> map = new HashMap<>();
    public static int t = 2;
    public static int sum = 0;
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] griph = new int[m][n];
        boolean[][] flag = new boolean[m][n];
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                griph[i][j] = sc.nextInt();
            }
        }
        int res = 0;
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                if(!flag[i][j] && griph[i][j] == 1){
                    dfs(flag, griph, i, j);
                    map.put(t, sum);
                    res = Math.max(res, sum);
                    t ++;
                    sum = 0;
                }
            }
        }
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                if(griph[i][j] == 0){
                    int count = 0;
                    Set<Integer> set = new HashSet<>();
                    for(int k = 0; k < 4; k ++){
                        int x = i + arr[k][0];
                        int y = j + arr[k][1];
                        if(x < 0 || x >= m || y < 0 || y >= n){
                            continue;
                        }
                        if(griph[x][y] > 1){
                            set.add(griph[x][y]);
                        }
                    }
                    for(int dd : set){
                        count += map.get(dd);
                    }
                    res = Math.max(res, count+1);
                }
            }
        }
        System.out.println(res);
    }
    public static void dfs(boolean[][] flag, int[][] griph, int x, int y){
        griph[x][y] = t;
        sum ++;
        flag[x][y] = true;

        for(int i = 0; i < 4; i ++){
            int curx = x + arr[i][0];
            int cury = y + arr[i][1];
            if(curx < 0 || curx >= griph.length || cury < 0 || cury >= griph[0].length){
                continue;
            }
            if(flag[curx][cury] || griph[curx][cury] == 0 || griph[curx][cury] > 1){
                continue;
            }
            dfs(flag, griph, curx, cury);
        }
    }
}


十、力扣106. 岛屿的周长

import java.util.*;
public class Main{
    public static int[][] arr = new int[][]{
        {-1,0}, {1,0}, {0,-1}, {0,1}
    };
    public static int count = 0;
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int[][] griph = new int[m][n];
        boolean[][] flag = new boolean[m][n];
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                griph[i][j] = sc.nextInt();
            }
        }
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                if(!flag[i][j] && griph[i][j] == 1){
                    dfs(flag, griph, i, j);
                }
            }
        }
        System.out.println(count);
    }
    public static void dfs(boolean[][] flag, int[][] griph, int x, int y){
        flag[x][y] = true;
        for(int i = 0; i < 4; i ++){
            int curx = x + arr[i][0];
            int cury = y + arr[i][1];
            if(curx < 0 || curx >= griph.length || cury < 0 || cury >= griph[0].length){
                count ++;
                continue;
            }
            if(flag[curx][cury]){
                continue;
            }
            if(griph[curx][cury] == 0){
                count ++;
                continue;
            }
            dfs(flag, griph, curx, cury);
        }
    }
}

十一、力扣110. 字符串接龙

在这里插入代码片

十二、力扣3. 无重复字符的最长子串

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int res = 0;
        if(s.length() == 0 || s.length() == 1){
            return s.length();
        }
        char[] ch = s.toCharArray();
        int left = -1, right = 0;
        Map<Character,Integer> map = new HashMap<>();
        while(left < right && right < ch.length){
            map.put(ch[right], map.getOrDefault(ch[right],0) + 1);
            while(map.get(ch[right]) > 1){
                left ++;
                map.put(ch[left], map.get(ch[left]) - 1);
            }
            res = Math.max(right-left, res);
            right ++;
        }
        return res;
    }
}

十三、力扣146. LRU 缓存

class LRUCache {
    public Map<Integer,DNode> map = new HashMap<>();
    public int count = 0;
    public int capacity;
    public DNode head;
    public DNode tail;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        this.head = new DNode(-1,-1);
        this.tail = new DNode(-1,-1);
        this.head.next = this.tail;
        this.tail.prev = this.head;
    }
    
    public int get(int key) {
        if(map.containsKey(key)){
            DNode cur = remove(key);
            cur = addTail(cur);
            return cur.value;
        }else{
            return -1;
        }
    }
    
    public void put(int key, int value) {
        if(map.containsKey(key)){
            DNode cur = map.get(key);
            cur = remove(cur.key);
            cur.value = value;
            addTail(cur);
        }else if(count >= capacity){
            DNode d = remove(head.next.key);
            map.remove(d.key);
            DNode cur = new DNode(key, value);
            addTail(cur);
            map.put(key, cur);
        }else{
            count ++;
            DNode cur = new DNode(key, value);
            addTail(cur);
            map.put(key, cur);
        }
    }

    public DNode addTail(DNode node){
        node.prev = this.tail.prev;
        node.next = this.tail;
        this.tail.prev.next = node;
        this.tail.prev = node;
        return node;
    }

    public DNode remove(int key){
        DNode cur = map.get(key);
        cur.prev.next = cur.next;
        cur.next.prev = cur.prev;
        cur.prev = null;
        cur.next = null;
        return cur;
    }
}
class DNode{
    public int key;
    public int value;
    public DNode prev;
    public DNode next;
    public DNode(int key, int value){
        this.key = key;
        this.value = value;
    }
    public DNode(){

    }
    
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache obj = new LRUCache(capacity);
 * int param_1 = obj.get(key);
 * obj.put(key,value);
 */

十四、力扣206. 反转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode L = new ListNode(-1,null);
        ListNode pre = head;
        while(pre != null){
            ListNode t = pre;
            pre = pre.next;
            t.next = L.next;
            L.next = t;
        }
        return L.next;
    }
}

十五、力扣15. 三数之和

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for(int i = 0; i < nums.length-2; i ++){
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            for(int j = i+1, k = nums.length-1; j < k; ){
                
                
                int sum = nums[i] + nums[j] + nums[k];
                if(sum == 0){
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[j]);
                    list.add(nums[k]);
                    res.add(list);
                    do{
                        j ++;
                    }while(j > i+1 && nums[j] == nums[j-1] && j < k);
                    do{
                        k --;
                    }while(k < nums.length-1 && nums[k] == nums[k+1] && j < k);
                }else if(sum < 0){
                    do{
                        j ++;
                    }while(j > i+1 && nums[j] == nums[j-1] && j < k);
                }else{
                    do{
                        k --;
                    }while(k < nums.length-1 && nums[k] == nums[k+1] && j < k);
                }
            }
        }
        return res;
    }
}

十六、力扣53. 最大子数组和

class Solution {
    public int maxSubArray(int[] nums) {
        int res = Integer.MIN_VALUE, count = 0;
        for(int i = 0; i < nums.length; i ++){
            if(count >= 0){
                count += nums[i];
            }else{
                count = nums[i];
            }
            res = Math.max(res, count);
        }
        return res;
    }
}

十七、力扣21. 合并两个有序链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode p1 = list1, p2 = list2;
        ListNode head = new ListNode();
        ListNode pre = head;
        while(p1 != null && p2 != null){
            if(p1.val < p2.val){
                ListNode t = p1;
                p1 = p1.next;
                pre.next = t;
                pre = pre.next;
            }else{
                ListNode t = p2;
                p2 = p2.next;
                pre.next = t;
                pre = pre.next;
            }
        }
        if(p1 != null){
            pre.next = p1;
        }else{
            pre.next = p2;
        }
        return head.next;
    }
}

十八、力扣5. 最长回文子串

class Solution {
    public String longestPalindrome(String s) {
        String res = "";
        for(int i = 0; i < s.length(); i ++){
            String str1 = fun(s, i, i);
            String str2 = fun(s, i, i + 1);
            res = str1.length() > res.length() ? str1 : res;
            res = str2.length() > res.length() ? str2 : res;
        }
        return res;
    }
    public String fun(String s, int l, int r){
        while(l >= 0 && r < s.length()){
            if(s.charAt(l) == s.charAt(r)){
                l --;
                r ++;
            }else{
                break;
            }
        }
        return s.substring(l+1,r);
    }
}

十九、力扣102. 二叉树的层序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Deque<TreeNode> deq = new LinkedList<>();
        deq.offerLast(root);
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        while(!deq.isEmpty()){
            int size = deq.size();
            List<Integer> list = new ArrayList<>();
            for(int i = 0; i < size; i ++){
                TreeNode cur = deq.pollFirst();
                list.add(cur.val);
                if(cur.left != null){
                    deq.offerLast(cur.left);
                }
                if(cur.right != null){
                    deq.offerLast(cur.right);
                }
            }
            res.add(list);
        }
        return res;
    }
}

二十、力扣1. 两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i ++){
            map.put(nums[i], i);
        }
        for(int i = 0; i < nums.length; i ++){
            int cur = target - nums[i];
            if(map.containsKey(cur) && i != map.get(cur)){
                return new int[]{i, map.get(cur)};
            }
        }
        return null;
    }
}

二十一、力扣33. 搜索旋转排序数组

class Solution {
    public int search(int[] nums, int target) {
        int n = nums.length;
        if(n == 0){
            return -1;
        }
        if(n == 1){
            return nums[0] == target ? 0 : -1;
        }
        int l = 0, r = n-1;
        while(l <= r){
            int mid = l + (r-l)/2;
            if(nums[mid] == target){
                return mid;
            }
            if(nums[0] <= nums[mid]){
                if(nums[0] <= target && nums[mid] > target){
                    r = mid - 1;
                }else{
                    l = mid + 1;
                }
            }else{
                if(nums[mid] < target && target <= nums[n-1]){
                    l = mid + 1;
                }else{
                    r = mid - 1;
                }
            }
        }
        return -1;
    }
}

二十二、力扣200. 岛屿数量

class Solution {
    int[][] arr = new int[][]{
        {-1,0}, {1,0}, {0,-1}, {0,1}
    };
    boolean[][] flag;
    public int numIslands(char[][] grid) {
        flag = new boolean[grid.length][grid[0].length];
        int count = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[0].length; j ++){
                if(!flag[i][j] && grid[i][j] == '1'){
                    count ++;
                    dfs(grid, i, j);
                }
            }
        }
        return count;
    }
    public void dfs(char[][] grid, int x, int y){
        flag[x][y] = true;
        for(int i = 0; i < 4; i ++){
            int curx = x + arr[i][0];
            int cury = y + arr[i][1];
            if(curx < 0 || curx >= grid.length || cury < 0 || cury >= grid[0].length){
                continue;
            }
            if(flag[curx][cury] || grid[curx][cury] == '0'){
                continue;
            }
            dfs(grid, curx, cury);
        }
    }
}

二十三、力扣46. 全排列

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    boolean[] flag;
    public List<List<Integer>> permute(int[] nums) {
        flag = new boolean[nums.length];
        fun(nums);
        return res;
    }
    public void fun(int[] nums){
        if(path.size() == nums.length){
            res.add(new ArrayList<>(path));
            return ;
        }
        for(int i = 0; i < nums.length; i ++){
            if(!flag[i]){
                path.add(nums[i]);
                flag[i] = true;
                fun(nums);
                path.remove(path.size()-1);
                flag[i] = false;
            }
        }
    }
}

二十四、力扣20. 有效的括号

class Solution {
    public boolean isValid(String s) {
        Deque<Character> deq = new ArrayDeque<>();
        for(char c : s.toCharArray()){
            if(c == '(' || c == '[' || c == '{'){
                deq.offerLast(c);
            }else{
                if(deq.isEmpty()){
                    return false;
                }
                char t = deq.peekLast();
                if(c == ')' && t != '('){
                    return false;
                }
                if(c == ']' && t != '['){
                    return false;
                }
                if(c == '}' && t != '{'){
                    return false;
                }
                deq.pollLast();
            }
        }
        return deq.size() == 0;
    }
}

二十五、力扣121. 买卖股票的最佳时机

class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n][2];
        dp[0][0] = -prices[0];
        for(int i = 1; i < n; i ++){
            dp[i][0] = Math.max(dp[i-1][0], -prices[i]);
            dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
        }
        return dp[n-1][1];
    }
}

二十六、力扣236. 二叉树的最近公共祖先

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q){
            return root;
        }
        TreeNode l = lowestCommonAncestor(root.left, p, q);
        TreeNode r = lowestCommonAncestor(root.right, p, q);
        if(l == null && r == null){
            return null;
        }else if(l != null && r == null){
            return l;
        }else if(l == null && r != null){
            return r;
        }else{
            return root;
        }
    }
}

二十七、力扣141. 环形链表

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null){
            return false;
        }
        ListNode L = new ListNode(-1, head);
        ListNode s = L.next, f = L.next.next;
        while(s != null && f != null){
            if(s == f){
                return true;
            }
            s = s.next;
            if(f.next == null){
                return false;
            }
            f = f.next.next;
        }
        return false;
    }
}

二十八、力扣300. 最长递增子序列

class Solution {
    public int lengthOfLIS(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n];
        Arrays.fill(dp, 1);
        int res = 1;
        for(int i = 1; i <n; i ++){
            for(int j = 0; j < i; j ++){
                if(nums[i] > nums[j]){
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
                res = Math.max(res, dp[i]);
            }
        }
        return res;
    }
}

二十九、力扣23. 合并 K 个升序链表

在这里插入代码片/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode L = new ListNode(-1,null), r = L;
        PriorityQueue<ListNode> pq = new PriorityQueue<>(
            (a,b) -> {return a.val - b.val;}
        );
        if(lists == null || lists.length == 0){
            return null;
        }
        for(ListNode node : lists){
            while(node != null){
                pq.offer(node);
                node = node.next;
            }
        }
        while(!pq.isEmpty()){
            ListNode cur = pq.poll();
            r.next = cur;
            r = r.next;
        }
        r.next = null;
        return L.next;
    }
}

三十、力扣160. 相交链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int l1 = 0, l2 = 0;
        ListNode p1 = headA, p2 = headB;
        while(p1 != null){
            l1 ++;
            p1 = p1.next;
        }
        while(p2 != null){
            l2 ++;
            p2 = p2.next;
        }
        if(l1 > l2){
            p1 = headA;
            p2 = headB;
            int c = l1 - l2;
            while( c -- > 0){
                p1 = p1.next;
            }
        }else if(l2 > l1){
            p2 = headB;
            p1 = headA;
            int c = l2 - l1;
            while(c -- > 0){
                p2 = p2.next;
            }
        }else{
            p1 = headA;
            p2 = headB;
        }
        while(p1 != null && p2 != null){
            if(p1 == p2){
                return p1;
            }
            p1 = p1.next;
            p2 = p2.next;
        }
        return null;
    }
}

三十一、力扣

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值