Leetcode[1/9/13] The sum of two numbers, palindrome numbers, and Roman numerals converted to integers C/C++ - the first day

sum of two numbers

Title description: [Simple]

Given an integer array nums and an integer target value target , please find the two integers whose sum is the target value target in the array, and return their array subscripts.

You can assume that there is only one answer for each input. However, the same element in the array cannot appear repeatedly in the answer.

You can return answers in any order.

Example:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, return [0, 1].

C language

System default code block:

/**
 1. Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    
    
   
}

The system gives 4 parameters, which are nums[] (integer array), numsize (the number of the integer array), target (integer target value, that is, the sum of two numbers), returnSize[] (returns the number of subscripts in the array) number).

Among them, returnSize has been bothering me for a long time. I thought it was to put the returned array subscript in it, because it is also an array hhh. Later I realized that it is a number, so the default is 2. But it is very interesting that we can easily write this assignment in the if at the beginning, which actually increases the time complexity. Putting it outside does not affect the final test, but it will reduce the time complexity to a certain extent.

The first idea: for + if directly dry + array


int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    
    
    static int arr[2] = {
    
    0}; 
    *returnSize = 2;
       for(int i = 0 ; i < numsSize - 1; i++){
    
    
        for(int j = i+1 ; j < numsSize ; j++){
    
    
            if(nums[i] + nums[j] == target ){
    
    
                arr[0] = i;
                arr[1] = j;
                return arr;
           }      
        }
    }
     return 0;
}

Use two for to traverse the nums array. At first I used:

for(int j = 1 ; j < numsSize ; j++)

Then in the next step of if, add i != j to the judgment statement to identify whether it is repeated. Increased time complexity. if it becomes

for(int j = i+1 ; j < numsSize ; j++)

There is no need to judge whether the array elements are repeated.

———Update
You can use malloc to dynamically allocate memory, but the time complexity is actually not as low as before. When using, note that malloc returns void\* type, but the title requires int*, then a forced conversion is required, since the array has only two elements, then allocate int *2 space, that is

 int* twoSum(int* nums, int
 numsSize, int target, int* returnSize){
    
    
     int* arr = NULL;  
     *returnSize = 2;
        for(int i = 0 ; i < numsSize - 1; i++){
    
    
         for(int j = i+1 ; j < numsSize ; j++){
    
    
             if(nums[i] + nums[j] == target ){
    
    
                 arr = (int*) malloc(sizeof(int)*2); 
                 arr[0] = i;
                 arr[1] = j;
                 return arr;
            }      
        }
     }
      return 0; 
  } 
  

C++

System default code block:

class Solution {
    
    
public:
    vector<int> twoSum(vector<int>& nums, int target) {
    
    
        

    }
};

Problem-solving code:

class Solution {
    
    
public:
    vector<int> twoSum(vector<int>& nums, int target) {
    
    
        int i , j;
     for(i = 0 ; i < nums.size() - 1 ; i++){
    
    
         for(j = i+1 ;j < nums.size() ; j++){
    
    
             if (nums[i] + nums[j] == target)
             return {
    
     i , j };
         }

     }   
     return {
    
     i , j };
    };
};

Among them, the number of arrays can be obtained by calling nums.size(). And there is no returnSize by default here, you can directly return { i , j }; but the time complexity is indeed much higher than that of c.

Palindrome

Title Description [Simple]:
Give you an integer x, if x is a palindromic integer, return true; otherwise, return false.

A palindromic number is an integer that reads the same in forward order (from left to right) and in reverse order (from right to left).

Example 1:

Input: x = 0
Output: true

C++

System default code block:

class Solution {
    
    
public:
    bool isPalindrome(int x) {
    
    
    
    }
   
};

First of all, there is no limit to three digits in this question, which means that we must consider all situations. So for negative numbers , obviously because of the negative sign, it cannot be a palindromic number. So we have to distinguish them from the beginning.
All numbers with a unit digit of 0 are not palindromic numbers after the reverse, because the previous 0 will be omitted, but pay attention to a special case, 0 is a palindromic number.

class Solution {
    
    
public:
    bool isPalindrome(int x) {
    
    
      if(x < 0 || (x % 10 == 0 && x != 0)){
    
    
          return false;
      }
      int num = 0 ;
      while(x > num){
    
    
          num = num * 10 + x % 10 ;
          x/=10;
      }
     if (x == num || x == (num / 10) ){
    
    
       return true;  
       }
    return NULL;
    } 
};

I use only half of the flip, so in the while, only the original number is less than the flipped number, and the flip can be stopped.

When looking at numbers from the front and the back, they should be divided into odd and even numbers . If it is an odd number, then the middle one does not need to be reversed, because the comparison between yourself and yourself is definitely the same. So in the end we judge the conditions into two types. If it is an odd number, we need to /10 to eliminate the middle digit, and then compare whether they are equal. If it is an even number, it can be directly judged.

C language

bool isPalindrome(int x){
    
    
 if(x < 0 || (x % 10 == 0 && x != 0)){
    
    
          return false;
      }
      int num = 0 ;
      while(x > num){
    
    
          num = num * 10 + x % 10 ;
          x/=10;
      }
     if (x == num || x == (num / 10) ){
    
    
       return true;  
       }
    return NULL;
}

Convert Roman Numerals to Integers

Title description [simple]:

Roman numerals contain the following seven characters: I, V, X, L, C, D, and M.

character value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, the Roman numeral 2 is written as II, which is two 1s side by side. 12 written as XII is X + II. 27 is written as XXVII, which is XX + V + II.

Normally, the smaller digits in Roman numerals are to the right of the larger digits. But there are also special cases, for example, 4 is not written as IIII, but IV. The number 1 is on the left of the number 5, and the represented number is equal to the value 4 obtained by reducing the number 1 from the large number 5. Likewise, the number 9 is expressed as IX. This particular rule applies only to the following six situations:

I can be placed to the left of V (5) and X (10) to represent 4 and 9.
X can be placed to the left of L (50) and C (100) to represent 40 and 90.
C can be placed to the left of D (500) and M (1000) to represent 400 and 900.

Given a Roman numeral, convert it to an integer.

C++

class Solution {
    
    
public:
    int romanToInt(string s) {
    
    
    int num=0;
    //IV,IX 4,9; XL,XC 40,90;CD,CM 400,900
    for(int i=0;i<s.size();i++){
    
    
        if(s[i]=='V') 
          num+=5;
        if(s[i]=='L') 
          num+=50;
        if(s[i]=='D') 
          num+=500;
        if(s[i]=='M') 
          num+=1000;
        if(s[i]=='I'){
    
    
            if(s[i+1]=='V'||s[i+1]=='X') //I放在V和X左边,但是同时要-1
               num-=1;
            else num+=1;
        }
        if(s[i]=='X'){
    
    
            if(s[i+1]=='L'||s[i+1]=='C') //X放在L和C左边,同时-10
               num-=10;
            else num+=10;
        }
        if(s[i]=='C'){
    
    
            if(s[i+1]=='D'||s[i+1]=='M') //C放在D和M左边,同时-100
              num-=100;
            else num+=100;
        }
    }
    return num;
    }
};

Guess you like

Origin blog.csdn.net/Lailalalala/article/details/125980605