This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

It’s the last day of November, let’s just do two simple leetcode questions. Sometimes a simple question may take a lot of time, because I’m too lazy

Problems and flowers

Subject address: leetcode-cn.com/problems/ca…

Title description:

Suppose you have a very long flower bed, and one part of the plot is planted with flowers, but the other part is not. However, flowers cannot grow in adjacent plots. They compete for water and both die.

Flowerbed gives you an array of integers representing a flowerbed, consisting of zeros and ones, where 0 indicates no flowers and 1 indicates flowers. Another number, n, can I plant n flowers without breaking the planting rules? Returns true if yes, false if no.

The sample

Example 1

Input: Flowerbed = [1,0,0,0,1], n = 1

Output: true,

Example 1

Input: flowerbed = [1,0,0,0,1], n = 2

Output: false

Subject analysis

This is actually a relatively easy algorithm problem (although I took a wrong turn at the beginning, resulting in more and more complex writing, has not been solved). Firstly, the problem is analyzed to determine whether N flowers can be implanted in the current flower site, that is, to find out how many flowers can be planted in the current flower site.

It can be found from the rule that whether flowers can be planted in the current position only needs to judge whether the previous value and the last value are 0. If all conditions are met, flowers can be planted in this position. (Starting position and certain position can be special consideration, can judge the case of undefined)

When I wrote the code, I started with a forEach loop through the array, but each flower I planted had to change the array in addition to counting, so I stuck with the for loop.

code

let curCount = 0,len = flowerbed.length;
for (let i = 0; i < len; i++) {
    let ele = flowerbed[i]
    if (ele == 0) {
        if ((flowerbed[i - 1] = =undefined || flowerbed[i - 1] = =0) && (flowerbed[i + 1] = =undefined || flowerbed[i + 1] = =0)) {
            curCount++;
            flowerbed[i] = 1; }}}if (curCount >= n) {
    return true;
} else {
    return false;
}
Copy the code

Verify palindrome string â…±

Subject address: leetcode-cn.com/problems/va…

Title description:

Given a non-empty string s, at most one character is deleted. Determines whether it can become a palindrome string.

The sample

The sample1: Enter: s ="aba"Output:trueThe sample2: Enter: s ="abca"Output:trueExplanation: You can remove the C character. The sample3: Enter: s ="abc"Output:false
Copy the code

Subject analysis

In this case, you can determine whether the current string is a palindrome string by pushing both sides of the string inwardly at the same time. If the values of the two sides are not equal, it means that one needs to be deleted. Record the index of the left and right sides at this time and end the current loop judgment.

Delete both indexes and determine whether the array can be palindromed.

code

let arr = s.split(' '), left = -1, right = -1;
for(let i = 0; i < Math.floor(arr.length / 2); i++) {
    if (arr[i] == arr[arr.length - 1 - i]) {
        continue;
    } else {
        left = i;
        right = arr.length - 1 - i;
        break; }}if (left == -1 && right == -1) {return true;
} else {
    let new1 = s.split(' '), new2 = s.split(' ');
    new1.splice(left, 1)
    new2.splice(right, 1)
    // console.log(new2, new1);
    if (new1.join() == new1.reverse().join()) {
        return true;
    }
    if (new2.join() == new2.reverse().join()) {
        return true; }}return false;
Copy the code