【Leetcode】Exam Bank - Simple Questions (1)

Table of contents

Written in front:

Title: 67. Binary Summation - Leetcode

Problem-solving ideas:

code:

Over, over, over, over! ! ! !

Topic: 83. Delete duplicate elements in a sorted linked list - Leetcode

Problem-solving ideas:

code:

Over, over, over, over! ! ! !

Write at the end:


Written in front:

Whether you are tired from studying, depressed, unsatisfactory in life, even broken in love, come with me and let go of the stress of life

Don't worry, open LeetCode, click on the question bank, choose simple questions , click this link, and enjoy the joy of brushing questions with me!

Title: 67. Binary Summation - Leetcode

Problem-solving ideas:

This question is actually, let us use code to simulate binary operations,

Then let's think about how to count in binary,

According to the knowledge of primary school mathematics, the addition of two numbers should be calculated from left to right,

When the added value of each bit is greater than the base, then carry,

In order to prevent the addition of one number due to the different sizes of the two binary numbers,

Another one hasn't been added yet,

1. We first calculate the length of the two numbers, pad the smaller number with zeros, and make the two numbers equal in length

2. Then add from left to right, if the added value needs to be carried, then carry it

Here is the code:

code:

class Solution {
public:
    string addBinary(string a, string b) {
        //计算他们的长度
        int alen = a.size();
        int blen = b.size();

        //在短的字符串前补零,让他们等长,之后好比较计算
        while(alen < blen)
        {
            a = '0' + a;
            alen++;
        }
        while(alen > blen)
        {
            b = '0' + b;
            blen++;
        }

        //从后往前遍历字符串,并相加进位
        for(int j = a.size() - 1; j > 0; j--)
        {
            a[j] = a[j] - '0' + b[j];
            if(a[j] > '1')
            {
                a[j] = (a[j] - '0') % 2 + '0';
                a[j - 1] = a[j - 1] + 1;
            }
        }

        //字符串第零位单独操作,相加
        a[0] = a[0] - '0' + b[0];

        //进位
        if(a[0] > '1')
        {
            a[0] = (a[0] - '0') % 2 + '0';
            a = '1' + a;
        }
        return a;
    }
};

Over, over, over, over! ! ! !

Topic: 83. Delete duplicate elements in a sorted linked list - Leetcode

Problem-solving ideas:

So this question is to delete the duplicates of the linked list,

Then for a simple problem,

My suggestion is not to use your brains,

We directly traverse the linked list without thinking, and then form a new linked list with the numbers that have appeared.

Because the title says that this is a sorted array, so the repeated numbers are connected together,

You only need to temporarily record whether that number is repeated to prevent repeated insertion.

Personally, I prefer to have a sentry position and then plug in the tail. Whether you take it or not depends on your personal habits.

Finally, just return the head of the new linked list.

Here is the code:

code:

/**
 * 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* deleteDuplicates(ListNode* head) {
        //判断空链表
        if(!head)
        {
            return nullptr;
        }

        //建一个哨兵位的头结点
        ListNode* newhead = new ListNode;

        //cur用来遍历原链表
        ListNode* cur = head;

        //先头插一个数据
        newhead->next = cur;

        //prev用来遍历新链表
        ListNode* prev = cur;

        //遍历原链表
        while(cur)
        {
            //如果节点的值重复就继续遍历,如果不同就尾插进新链表
            if(cur->val != prev->val)
            {
                prev->next = cur;
                prev = prev->next;
            }
            cur = cur->next;
        }

        //尾结点指向空
        prev->next = nullptr;

        //换头(其实可以不换的啦)
        head = newhead->next;
        return head;
    }
};

Over, over, over, over! ! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch.

Guess you like

Origin blog.csdn.net/Locky136/article/details/130040333