当前位置:网站首页>Leetcode 2194. Cells within a range in Excel table (yes, solved)

Leetcode 2194. Cells within a range in Excel table (yes, solved)

2022-06-12 16:31:00 I'm not xiaohaiwa~~~~

 Insert picture description here
Excel A cell in a table (r, c) String “” In the form of , among :

  • That is, the column number of the cell c . Use... In the English alphabet Letter identification . for example , The first 1 Column use 'A' Express , The first 2 Column use 'B' Express , The first 3 Column use 'C' Express , And so on .
  • That is, the row number of the cell r . The first r Just use it Integers r identification .

Give you a format of “:” String s , among Express c1 Column , Express r1 That's ok , Express c2 Column , Express r2 That's ok , And satisfy r1 <= r2 And c1 <= c2 .

Find all the satisfaction r1 <= x <= r2 And c1 <= y <= c2 Cells of , And return... As a list . Cells should be in the format described above character string Express , And The decreasing Sequential arrangement ( First by column , Then line up ).

Example 1:

 Insert picture description here

 Input :s = "K1:L2"
 Output :["K1","K2","L1","L2"]
 explain :
 The above figure shows the cells that should appear in the list .
 Red arrows indicate the order in which cells appear .

Example 2:

 Insert picture description here

 Input :s = "A1:F1"
 Output :["A1","B1","C1","D1","E1","F1"]
 explain :
 The above figure shows the cells that should appear in the list . 
 Red arrows indicate the order in which cells appear .
 

Tips :

  • s.length == 5
  • ‘A’ <= s[0] <= s[3] <= ‘Z’
  • ‘1’ <= s[1] <= s[4] <= ‘9’
  • s It consists of capital letters 、 Numbers 、 and ‘:’ form

Code:

class Solution {
    
public:
    vector<string> cellsInRange(string s) {
    
        int start=s[1]-'0';
        int end=s[4]-'0';
        vector<string>res;



        for(int j=s[0];j<=s[3];j++)
        {
    

            string temp;

            for(int i=start;i<=end;i++)
            {
    
                temp+=j;
                temp+=to_string(i);
          // cout<<temp<<endl;
                res.push_back(temp);
                temp="";
            }
        }
        return res;

    }
};
原网站

版权声明
本文为[I'm not xiaohaiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121629144475.html