找LeetCode上简单的题目来撑过30天啦(DAY18)

我越来越懂linklist了(应该吧?),可喜可贺

题号:21 标题:Merge Two Sorted Lists 难度:Easy

Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.

Example 1:

Input: l1 = [1,2,4], l2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2:
Input: l1 = [], l2 = []
Output: []
Example 3:
Input: l1 = [], l2 = [0]
Output: [0]

Constraints:
• The number of nodes in both lists is in the range [0, 50].
• -100 <= Node.val <= 100
• Both l1 and l2 are sorted in non-decreasing order.

/**
 * 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 l1, ListNode l2) {
        ListNode result = new ListNode();
        ListNode cur = new ListNode();

        ListNode temp = new ListNode();
        int start = 0;
        if(l1 == null && l2==null){
            return l1;
        }
        while(l1 != null || l2!= null){
            //System.out.println("v1:" + l1.val + "  v2:" + l2.val);
            if(l1 == null){
                if(start == 0){
                    result = l2;
                    cur = l2;
                    temp = l2;
                    System.out.println("1:" + cur.val);
                    l2 = l2.next;
                    start++;
                }else{
                    temp.next = l2;
                    System.out.println("2:" + l2.val);
                    cur = temp.next;
                    temp = l2;
                    l2 = l2.next;
                }
            }else if(l2 == null){
                if(start == 0){
                    result = l1;
                    cur = l1;
                    temp = l1;
                    System.out.println("3:" + cur.val);
                    l1 = l1.next;
                    start++;
                }else{
                    temp.next = l1;
                    cur = temp.next;
                    temp = cur;
                    System.out.println("4:" + cur.val);
                    l1 = l1.next;
                }
            }else{
                if(l1.val <= l2.val && start == 0){
                    result = l1;
                    cur = l1;
                    temp = l1;
                    System.out.println("5:" + cur.val);
                    l1 = l1.next;
                    start++;
                }else if(l2.val < l1.val && start == 0){
                    result = l2;
                    cur = l2;
                    temp = l2;
                    System.out.println("7:" + cur.val);
                    l2 = l2.next;
                    start++;
                }else if(l1.val <= l2.val ){
                    temp.next = l1;
                    cur = temp.next;
                    temp = cur;
                    System.out.println("6:" + cur.val);
                    l1 = l1.next;
                }else{
                    temp.next = l2;
                    System.out.println("8:" + cur.val);
                    cur = temp.next;
                    temp = cur;
                    l2 = l2.next;                
                }
            }
            
        }
        
        return result;
    }
}

题号:205 标题:Isomorphic Strings 难度:Easy

Given two strings s and t, determine if they are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

Example 1:
Input: s = "egg", t = "add"
Output: true


Example 2:
Input: s = "foo", t = "bar"
Output: false


Example 3:
Input: s = "paper", t = "title"
Output: true


Constraints:
• 1 <= s.length <= 5 * 104
• t.length == s.length
• s and t consist of any valid ascii character.


bool isIsomorphic(char * s, char * t){
    int len=strlen(s),i,j;
    //printf("%d,%d",lens,lent);
    int temp[128]={0};
    for(i=0;i<len;i++){
        if(temp[s[i]]==0){
            temp[s[i]] = t[i];
        }else if(temp[s[i]] != t[i]){
            return false;
        }
    }
    int temp2[128]={0};
    for(i=0;i<len;i++){
        if(temp2[t[i]]==0){
            temp2[t[i]] = s[i];
        }else if(temp2[t[i]] != s[i]){
            return false;
        }
    }

       
    return true;
}

DAY18心得
我有进步的感觉,开心/images/emoticon/emoticon12.gif


<<:  交易策略 - Backtrader

>>:  [Day 28] 建立 migration 时使用的套件,来谈谈 Flyway

不只懂 Vue 语法:什麽是 slot?请示范 slot 的用法?

问题回答 slot(插槽)的概念是把外层的内容塞进子元件的指定位置里。跟插槽的字面意思一样,前提是:...

[Day4] Face Detection - 使用Google Cloud Vision API

reference: medium - Filtering Image content with ...

业务驱动者和推动者(Business Drivers and Enablers)

-波特的价值链(Porter’s Value Chain) 业务就是提供产品和服务以创造价值并实现...

[Day 19] 针对网页的单元测试(五)

再写登入的验证及功能 今天我们要来做登入的判断跟动作, 我们在HomeController.php引...

{CMoney战斗营} 的第八周 #如果我不再这里学习

游戏专题进行了半个月,耗费最大的心力是在处理碰撞的逻辑问题,为1 Pixcel的逻辑误差斤斤计较,不...