LeetCode刷题 | 726. Number of Atoms 难题 for循环处理题

Givena chemical formula (givenas a string), return the count of each atom.

Anatomic element always starts with an uppercase character, then zero or morelowercase letters, representing the name.

1or more digits representing the count of that element may follow if the countis greater than 1. If the count is 1, no digits will follow. For example, H2Oand H2O2 are possible, but H1O2 is impossible.

Twoformulas concatenated together produce another formula. For example, H2O2He3Mg4is also a formula.

Aformula placed in parentheses, and a count (optionally added) is also aformula. For example, (H2O2) and (H2O2)3 are formulas.

Givena formula, output the count of all elements as a string in the following form:the first name (in sorted order), followed by its count (if that count is morethan 1), followed by the second name (in sorted order), followed by its count(if that count is more than 1), and so on.

Example1:

Input:

formula = "H2O"

Output: "H2O"

Explanation:

The count of elements are {'H': 2, 'O': 1}.

Example2:

Input:

formula = "Mg(OH)2"

Output: "H2MgO2"

Explanation:

The count of elements are {'H': 2, 'Mg': 1, 'O': 2}.

Example3:

Input:

formula = "K4(ON(SO3)2)2"

Output: "K4N2O14S4"

Explanation:

The count of elements are {'K': 4, 'N': 2, 'O': 14, 'S': 4}.

Note:

· All atom names consist of lowercase letters, except for the firstcharacter which is uppercase.

· The length of formula will bein the range [1, 1000].

· formula will only consist of letters,digits, and round parentheses, and is a valid formula as defined in theproblem.

这题就是按照题目的要求去计算出所有的原子的种类和个数,然后要求你用字典排序,这题难都不难,就是非常的复杂,容易把人绕晕,

通过学习这一题,我以后要记住求解这种题目的技巧,这种编程的层次结构,也就是主循环里面套副循环的结构体,要记牢,

以后在做题的时候,判断某个数字和字母是否是大小写和数字的时候,记住判断数字写法是>=’0’<=’9’ 而不是>=’1’<=’9’记住,判断小写和大写是>=’a’<=’z’记住是小于z,

其实这题可以使用栈不是用递归来做,这样的话题目能快很多,

在使用substring的时候也要注意index和字符串位置的错位,记得要减一,同时也要注意for循环里面的i++

这题在最后要求我们进行字典升序排序,这里使用sort(theAtom.begin(),theAtom.end(),cmp)就行,至于cmp的写法bool cmp(Ta,T,b),这里的T可以是任何类型,但一定要是输入数组的成员类型,这里的T还可以使用引用类型,当cmp返回true的的时候,b会排在a的前面,当cmp返回false的时候,a会排在b的前面,sort可以对任何类型的成员排序,只要把cmp写好

如何判断两个字符串在字典序下的大小呢,使用strcmp(char * a,char * b)和a.compare(b),前一种用来判断c风格字符串的大小,后一种用来判断string字符串的大小!

class Solution {

public:

  #include <algorithm>

   vector<pair <string, int> > theAtoms;

int CheciIsHave(string tmp)

{

      for(int i=0;i< theAtoms.size();i++)

      {

            if(theAtoms[i].first == tmp) return i;

      }

      return-1;

}

 

void carculate(string formula, int str, intend,int multi)

{

      intmarkStart, markEnd;

      markStart= str - 1; markEnd = str - 1;

      stringtmp;

      for(int i = str; i <= end;)

      {

            if((formula[i - 1] >= 'A'&&formula[i - 1] <= 'Z'))

            {

                  markStart= i;

                 

                  i++;

                  while(i<=end&&formula[i - 1] >= 'a'&&formula[i - 1] <= 'z')i++;

                  markEnd= i;

                  while(i<=end&&formula[i - 1] >= '0'&&formula[i - 1] <= '9')i++;

                 

                  tmp= formula.substr(markStart-1, markEnd - markStart);//检查是否需要将字符串加入数组

                  intnumCount = CheciIsHave(tmp);

                 

                  inttheNum;

                  if(markEnd == i) theNum = 1;

                  else

                       theNum= atoi(formula.substr(markEnd-1, i - markEnd).c_str());

 

                  if(numCount>=0)

                  {

                       theAtoms[numCount].second+= (theNum*multi);

                  }

                  else

                  {

                       theAtoms.push_back(pair<string,int>(tmp, theNum*multi));

                  }

            }

            if(i > end) break;

            if(formula[i - 1] == '(')

            {

                  markStart= i;

                  intkuoCount = 1; int t = i;

                  while (kuoCount!=0)

                  {

                       t++;

                       if(formula[t - 1] == '(') kuoCount++;

                       if(formula[t - 1] == ')') kuoCount--;

                  }

                  markEnd= t++;

                  while(t <= end&&formula[t - 1] <= '9'&&formula[t - 1] >='0') t++;

 

                  inttheNum;

                  if(markEnd == t-1) theNum = 1;

                  else

                       theNum= atoi(formula.substr(markEnd+1-1, t - markEnd-1).c_str());

 

                  carculate(formula,markStart + 1, markEnd - 1, theNum*multi);

                  i= t;

            }

            if(i > end) break;

      }

}

 

static bool cmp(pair <string, int>& a, pair <string, int> & b)

{

      returna.first.compare(b.first)<0;

}

string countOfAtoms(string formula) {

      carculate(formula,1, formula.size(), 1);

      stringresult = "";

      sort(theAtoms.begin(),theAtoms.end(),cmp);

      for(int i = 0; i < theAtoms.size(); i++)

            if(theAtoms[i].second!=1)

            result= result + theAtoms[i].first + to_string(theAtoms[i].second);

            else

                  result= result + theAtoms[i].first;

      returnresult;

}

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值