2025年11月7日 星期五

92. Reverse Linked List II

 92. Reverse Linked List II


難度: Medium
類型: Linked List
CPP程式下載: 92.cpp

前情題要:
反向連結 --- 給定一個連結的 list, 一個左邊位址, 一個右邊位置, 左邊位置小於或等於右邊位置, 把這個連結的左邊位置到右邊位置的連結反向, 回傳結果。(能夠一輪就給答案嗎?)

Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.

 

Example 1:

Input: head = [1,2,3,4,5], left = 2, right = 4
Output: [1,4,3,2,5]

Example 2:

Input: head = [5], left = 1, right = 1
Output: [5]

 

Constraints:

  • The number of nodes in the list is n.
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

 

Follow up: Could you do it in one pass?


思考方式:
stack 的先進後出特性, 剛好拿來做反向連結。
1. 為了不破壞原有的 link list, 所有 output link list 的成員都是 new 出來的。這種方式會花費比較多的記憶體空間。其實也有另一種方法是直接改在原本的 linked list 上, 有機會另一篇文章再寫。
2. 運算過程中, 在大於等於左邊位置, 卻還仍小於右邊位置之前, 這些 linked list 的成員都 push 進Stack裡, 直到等於右邊位置時, 把右邊位置的 linked list 放上output連結後, 把 stack 裡的內容一個一個 pop 出來, 直到 stack 為空。
3. 記得更新 input linked list pointer 與 output linked list pointer。每次設定完一個新的output成員後, 記得把 output pointer 指定到 next。

複雜度思考:

Time Complexity: Worst case: O( N )


結果:

Runtime: 0 ms, Beats: 100%

Memory: 11.24 MB, Beats: 38.10%


Accepted
44 / 44 testcases passed
tendchen
tendchen
submitted at Nov 07, 2025 05:39
Runtime
0ms
Beats100.00%
Analyze Complexity
Memory
11.24MB
Beats38.10%
Analyze Complexity
Code
C++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int left, int right) {
        ListNode *out=nullptr;
        ListNode *cur_OutNode=nullptr;
        ListNode *cur_InNode;
        stack<ListNode> sListNode;
        cur_InNode=head;
        int i,j;
        for (i=1;;i++)
        {
            if (cur_InNode==nullptr) return out;
            if (i<left)
            {
                if (out==nullptr)
                {
                    out=(new ListNode(cur_InNode->val));
                    cur_OutNode=out;
                    cur_InNode=cur_InNode->next;
                }
                else
                {
                    cur_OutNode->next=(new ListNode(cur_InNode->val));
                    cur_OutNode=cur_OutNode->next;
                    cur_InNode=cur_InNode->next;
                }
            }
            else
            {
                if (i<right)
                {
                    sListNode.push(*cur_InNode);
                    cur_InNode=cur_InNode->next;
                    //cout << "push and then size=" << sListNode.size() << "\n";
                }
                else if (i==right)
                {
                    if (out==nullptr)
                    {
                        out=(new ListNode(cur_InNode->val));
                        cur_OutNode=out;
                        cur_InNode=cur_InNode->next;
                    }
                    else
                    {
                        cur_OutNode->next=(new ListNode(cur_InNode->val));
                        cur_OutNode=cur_OutNode->next;
                        cur_InNode=cur_InNode->next;
                    }
                    for (j=1;;j++)
                    {
                        if (sListNode.size()==0) break;
                        cur_OutNode->next=(new ListNode(sListNode.top().val));
                        cur_OutNode=cur_OutNode->next;
                        sListNode.pop();
                    }
                }
                else
                {
                    cur_OutNode->next=(new ListNode(cur_InNode->val));
                    cur_OutNode=cur_OutNode->next;
                    cur_InNode=cur_InNode->next;
                }

            }
        }
    }
};

2025年10月8日 星期三

65. Valid Number

 65. Valid Number

 

難度: Hard
類型:String
CPP程式下載: 65.cpp

前情題要:
判斷字串是否為完整的一個數。

Given a string s, return whether s is a valid number.

For example, all the following are valid numbers: "2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789", while the following are not valid numbers: "abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53".

Formally, a valid number is defined using one of the following definitions:

  1. An integer number followed by an optional exponent.
  2. A decimal number followed by an optional exponent.

An integer number is defined with an optional sign '-' or '+' followed by digits.

A decimal number is defined with an optional sign '-' or '+' followed by one of the following definitions:

  1. Digits followed by a dot '.'.
  2. Digits followed by a dot '.' followed by digits.
  3. A dot '.' followed by digits.

An exponent is defined with an exponent notation 'e' or 'E' followed by an integer number.

The digits are defined as one or more digits.

 

Example 1:

Input: s = "0"

Output: true

Example 2:

Input: s = "e"

Output: false

Example 3:

Input: s = "."

Output: false

 

Constraints:

  • 1 <= s.length <= 20
  • s consists of only English letters (both uppercase and lowercase), digits (0-9), plus '+', minus '-', or dot '.'.

思考方式:
1. 將數字切成第一個數字, 第二個數字,  小數點後的(第三)數字, 第一個正負符號, 第二個正負符號, exp, 以及 小數點(dot), 這七個部分。
2. 每個字元進來, 就根據目前各個部分是否存在來判斷合不合理。
3. 用邏輯的思維判斷這些數字存在與否能不能成為一個合理的數字。

複雜度思考:

Time Complexity: Worst case: O( N )


結果:

Runtime: 0 ms, Beats: 100%

Memory: 7.98 MB, Beats: 98.61%

Accepted
1498 / 1498 testcases passed
tendchen
tendchen
submitted at Oct 08, 2025 00:26
Runtime
0ms
Beats100.00%
Analyze Complexity
Memory
7.98MB
Beats98.61%
Analyze Complexity
Code
C++
class Solution {
public:
    bool isNumber(string s) {
        //int length=s.size();
        bool sign1=false;
        bool sign2=false;
        bool exp=false;
        bool dot=false;
        int num1=false;
        int num2=false;
        int num3=false;
        int leng=s.length();
        for (int i=0;i<leng;i++)
        {
            switch (s[i])
            {
                case '+':
                case '-':
                    if (sign2) return false;
                    else if (sign1)
                    {
                        if (!exp) return false;
                        else {
                            if (num2) return false;
                            else {
                                if (sign2) return false;
                                else
                                {
                                    sign2=true;
                                    continue;
                                }
                            }
                        }
                    }
                    else {
                        if (dot|num1)
                        {
                            return false;
                        }
                        else
                        {
                            sign1=true;
                            continue;
                        }
                    }
                    break;
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    if (num2)
                    {
                        num2++;
                        continue;
                    }
                    else
                    {
                        if (exp)
                        {
                            num2++;
                            sign2=true;
                            continue;
                        }
                        else
                        {
                            if (num1)
                            {
                                if (!dot)
                                {
                                    num1++;
                                    continue;
                                }
                                else
                                {
                                    num3++;
                                    continue;
                                }
                            }
                            else
                            {
                                num1++;
                                sign1=true;
                                if (dot) num3++;
                                continue;
                            }
                        }
                    }
                    break;
                case '.':
                    if (num2) return false;
                    else if (exp) return false;
                    else if (dot) return false;
                    else if (num1)
                    {
                        num1++;
                        dot=true;
                        continue;
                    }
                    else
                    {
                        sign1=true;
                        dot=true;
                        continue;
                    }
                    break;
                case 'e':
                case 'E':
                    if (exp) return false;
                    else
                    {
                        if (num1)
                        {
                            exp=true;
                            continue;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    break;
                default:
                    return false;
                    break;
            }
        }
        if (num2)   return true;
        if (exp)    return false;
        if (dot)
        {
            if (num3) return true;
            else 
            {
                if (num1) return true;
                else return false;
            }
        }
        if (num1) return true;
        else return false;
    }
};