最长公共子串&&两个链表的第一个公共结点&&两个链表生成相加链表

236 阅读2分钟

NC127 最长公共子串

题目链接

1、解题思路
  • 这个题目和一般的题目有个区别,这个公共子串必须连续,这样在dp的过程中,如果两个字符不等,可以直接等于0就好了;其他的操作就和一般的一样了,求串的时候也好求一点。
2、代码
import java.util.*;


public class Solution {
    /**
     * longest common substring
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @return string字符串
     */
    public String LCS (String str1, String str2) {
        // write code here
        int r = str1.length();
        int c = str2.length();
        int maxLen = -1;
        int rr = 0,cc = 0;
        int[][] dp = new int[r+1][c+1];
        for(int i = 0;i <= r;i++){
            dp[i][0] = 0;
        }
        for(int i = 0;i <= c;i++){
            dp[0][i] = 0;
        }
        for(int i = 1;i <= r;i++){
            for(int j = 1;j <= c;j++){
                if(str1.charAt(i-1) == str2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else{
                    dp[i][j] = 0;
                }
                if(dp[i][j] > maxLen){
                    maxLen = dp[i][j];
                    rr = i;
                    cc = j;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        while(rr >= 1 && str1.charAt(rr-1) == str2.charAt(cc-1)){
            sb.append(str1.charAt(rr-1));
            rr--;
            cc--;
        }
        return sb.reverse().toString();
    }
}

NC66 两个链表的第一个公共结点

题目链接

1、解题思路
  • 就两个指针同时跑,然后一个到头了换个p2,一个到头了换p1,每个人只有一次交换机会
2、代码
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        if(pHead2 == null || pHead1 == null){
            return null;
        }
        boolean turn1 = false;
        boolean turn2 = false;
        ListNode p1 = pHead1;
        ListNode p2 = pHead2;
        while(p1 != p2){
            if(p1.next == null){
                if(!turn1){
                    p1 = pHead2;
                    turn1 = true;
                }else{
                    return null;
                }
            }else{
                p1 = p1.next;                
            }
            
            if(p2.next == null){
                if(!turn2){
                    p2 = pHead1;
                    turn2 = true;
                }else{
                    return null;
                }
            }else{
                p2 = p2.next;                
            }
        }
        return p1;
    }
}

NC40 两个链表生成相加链表

题目链接

1、解题思路
  • 首先逆转链表,然后头插法一次相加就好
2、代码
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head1 ListNode类 
     * @param head2 ListNode类 
     * @return ListNode类
     */
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        ListNode l1 = new ListNode(0);
        while(head1 != null){
            ListNode temp = head1;
            head1 = head1.next;
            temp.next = l1.next;
            l1.next = temp;
        }
        
        ListNode l2 = new ListNode(0);
        while(head2 != null){
            ListNode temp = head2;
            head2 = head2.next;
            temp.next = l2.next;
            l2.next = temp;
        }
        l1 = l1.next;
        l2 = l2.next;
        int carry = 0;
        ListNode ret = new ListNode(0);
        while(l1 != null || l2 != null){
            int val1 = 0,val2 = 0;
            if(l1 != null){
                val1 = l1.val;
                l1 = l1.next;
            }
            if(l2 != null){
                val2 = l2.val;
                l2 = l2.next;
            }
            int sum = val1 + val2 + carry;
            carry = sum / 10;
            sum = sum % 10;
            ListNode t = new ListNode(sum);
            t.next = ret.next;
            ret.next = t;
        }
        if(carry != 0){
            ListNode t = new ListNode(carry);
            t.next = ret.next;
            ret.next = t;
        }
        return ret.next;
    }
}