35. Search insertion position 58. Length of last word

Table of contents

35. Search insertion position 

train of thought

the code 

 58. The length of the last word

Idea 1

code 1

Idea 2

code 2 


 

35. Search insertion position 

Given a sorted array and a target value, find the target value in the array and return its index. If the target value does not exist in the array, returns the position where it will be inserted in order.

Please use an algorithm with time complexity O(log n).

Example 1:

Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:

Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:

Input: nums = [1,3,5,6], target = 7
Output: 4
 

Source: LeetCode
Link: https://leetcode.cn/problems/search-insert-position

train of thought

This question is simple, the only point to note is: when the target value does not exist in the array, return the position where it will be inserted in order (in ascending order) , such as

Example 2:

Input: nums = [1,3,5,6], target = 2
Output: 1

So I created the count variable and used him to solve this situation ( the array in this question is a sorted array )

Compare each bit with the target value, if the element is smaller than the target value, count is ++, ( count starts from 0, so the value of count is directly the subscript inserted by the target value )

the code 

int searchInsert(int* nums, int numsSize, int target)
{
    int i = 0;
    int count = 0;
    for(i = 0; i < numsSize; i++)
    {
        if(nums[i] == target)
        {
            return i;
        }
        if(nums[i] < target)
        {
            count++;
        }
    }
    return count;
}

 

 58. The length of the last word

You are given a string s consisting of several words separated by some space characters before and after the words. Returns the length of the last word in a string.

A word is the largest substring consisting of letters only, without any space characters.

 

Example 1:

Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Example 2:

Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" and has length 4.
Example 3:

Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" of length 6.
 

Source: LeetCode
Link: https://leetcode.cn/problems/length-of-last-word

Idea 1

violent problem solving

i comes from the last digit

 

From this example, we know that there will be a space after the last word, we need to skip the space,

Example 2:

Input: s = " fly me to the moon "
Output: 4

 

Then create a count variable to count, pay attention to i > 0 in the while loop, otherwise it will overflow, and Likou will report an error 

Because there is no i = 0; situation above (response to "a"), so fill it in,

Note that s[i] cannot be equal to ' ' (to prevent this case "ab")

 The order of these code blocks cannot be changed

code 1

int lengthOfLastWord(char* s)
{
    int len = strlen(s);
    int i = len - 1;
    int count = 0;
    while (s[i] == ' ')
    {
        i--;
    }
    while (s[i] != ' ' && i > 0)
    {
        i--;
        count++;
    }
    if (i == 0 && s[i] != ' ')
    {
        count++;
    }
    return count;
}

Idea 2

The second type of code is more concise, using the for loop, and the loop condition is written correctly, so there is no need to consider the overflow situation.

Still start from the back (i = len - 1), create a count variable to count, s[i] != ' ', count++;

I think the following code is quite ingenious (o゜▽゜)o☆

If s[i] == ' ' and count! = 0, this is very ingenious ヾ(≧▽≦*)o,

count! = 0 , it means that this is a space that has been encountered after a word has passed , it is time to jump out of the loop, and then return to count. 

 

 

code 2 

int lengthOfLastWord(char* s)
{
    int len = strlen(s);
    int i = len - 1;
    int count = 0;
    for(i = len - 1; i >= 0; i--)
    {
        if(s[i] != ' ')
        {
            count++;
        }
        if(s[i] == ' ' && count != 0)
        {
            break;
        }
    }
    return count;
}

 

 ╰(*°▽°*)╯╰(*°▽°*)╯╰(*°▽°*)╯╰(*°▽°*)╯╰(*°▽°*)╯End ╰(*° ▽°*)╯╰(*°▽°*)╯╰(*°▽°*)╯╰(*°▽°*)╯╰(*°▽°*)╯

Guess you like

Origin blog.csdn.net/iiiiiihuang/article/details/130327988