当前位置: 首页>>代码示例>>C++>>正文


C++ NestedInteger类代码示例

本文整理汇总了C++中NestedInteger的典型用法代码示例。如果您正苦于以下问题:C++ NestedInteger类的具体用法?C++ NestedInteger怎么用?C++ NestedInteger使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了NestedInteger类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: helper

 NestedInteger helper(string& s, int start, int end) {
     NestedInteger ni;
     int nestedStart = -1;
     int n = 0;
     int num = 0;
     if (s[start] != '[') {
         bool neg = false;
         for (int i = start; i <= end; i++) {
             if (s[i] == '-') {
                 neg = true;
             } else {
                 num = 10 * num + (s[i] - '0');
             }
         }
         if (neg == true) num *= -1;
         ni.setInteger(num);
     } else {
         start++;
         end--;
         int lastStart = start;
         for (int i = start ; i <= end; i++) {
             if (s[i] == '[') {
                 n++;
                 if (nestedStart == -1) nestedStart = i;
             } else if (s[i] == ']') {
                 n--;
             } else if (s[i] == ',' && n == 0) {
                 ni.add(helper(s, lastStart, i - 1));
                 lastStart = i + 1;
             }
             if (i == end) ni.add(helper(s, lastStart, end));
         }
     }
     return ni;
 }
开发者ID:hawkphantomnet,项目名称:leetcode,代码行数:35,代码来源:Solution.cpp

示例2: impl

    NestedInteger impl(const std::string& s, int startIdx, int endIdx)
    {
        if(s[startIdx] != '[')
        {
            return NestedInteger(std::stoi(s.substr(startIdx, endIdx - startIdx)));
        }

        NestedInteger t;
        for(int i = startIdx + 1; i < endIdx - 1;)
        {
            if(s[i] == '[')
            {
                int j = i + 1;
                int cnt = 1;
                while(j < endIdx && cnt)
                {
                    if(s[j] == '[') ++cnt;
                    else if(s[j] == ']') --cnt;
                    ++j;
                }
                t.add(impl(s, i, j));
                i = j + 1;
            }
            else
            {
                int j = i;
                while(j < endIdx && s[j] != ',') ++j;
                t.add(impl(s, i, j));
                i = j + 1;
            }
        }
        return t;
    }
开发者ID:knightzf,项目名称:review,代码行数:33,代码来源:main.cpp

示例3: deserialize

 NestedInteger deserialize(string s) {
     if (s.empty()) return NestedInteger(); // empty list
     if (s[0] == '[') { // nested list
         NestedInteger n;
         int i = 1, end = s.size() - 1;
         while (i < end) {
             int begin = i;
             if (s[i] == '[') { // element being nested list
                 int cnt = 0;
                 do {
                     if (s[i] == '[') ++cnt;
                     else if (s[i] == ']') --cnt;
                     ++i;
                 } while (cnt > 0);
             } else {
                 while (isdigit(s[i]) || s[i] == '-') ++i;
             }
             n.add(deserialize(s.substr(begin, i - begin)));
             ++i;
         }
         return n;
     }
     // plain number
     return NestedInteger(stoi(s));
 }
开发者ID:lzl124631x,项目名称:code,代码行数:25,代码来源:s1.cpp

示例4: deserializeRecursively

	NestedInteger deserializeRecursively(string &s, int& pos) {
		if (s[pos] == '[') {
			NestedInteger n;
			pos++;
			while (s[pos] != ']') {
				n.add(deserializeRecursively(s, pos));
			}
			pos++;
			return n;
		}
		else if (isdigit(s[pos]) || s[pos] == '-') {
			int res = 1;
			int sign = 1;
			if (s[pos] == '-') {
				pos++;
				sign = -1;
			}
			while (s[pos] != ',' && s[pos] != '\0') {
				res = res * 10 + s[pos] - '0';
				pos++;
			}
			NestedInteger n;
			n.setInteger(res * sign);
			pos++;
			return n;
		}
	}
开发者ID:hyzups,项目名称:leetcode,代码行数:27,代码来源:MiniParser.cpp

示例5: helper

 void helper(NestedInteger num){
     if(num.isInteger())
         res.push_back(num.getInteger());
     else{
         for(auto n : num.getList())
             helper(n);
     }
 }
开发者ID:vincivans,项目名称:LeetCode,代码行数:8,代码来源:22flattenList_LintCode.cpp

示例6: getSum

int getSum(NestedInteger ni, int level) {
    int res = 0;
    if (ni.isInteger()) return level * ni.getInteger();
    for (auto a : ni.getList()) {
        res += getSum(a, level + 1);
    }
    return res;
}
开发者ID:BranY,项目名称:CodeSummary,代码行数:8,代码来源:NestListWeightSum_1.cpp

示例7: flatten

 void flatten(NestedInteger lariat) {
     if (lariat.isInteger())
         vec.push_back(lariat.getInteger());
     else {
         for (NestedInteger KillerBee : lariat.getList())
             flatten(KillerBee);
     }
 }
开发者ID:g33kyaditya,项目名称:OJs,代码行数:8,代码来源:341.cpp

示例8: main

int main()
{
	string s = "234";
	NestedInteger value = parser(s);

	cout << "value " << value.getValue() << endl;

	return 0;
}
开发者ID:chenyu1927,项目名称:hello-world,代码行数:9,代码来源:minParser.cpp

示例9: dfs

 void dfs(NestedInteger& ni) {
     if (ni.isInteger())
         container.push_back(ni.getInteger());
     else {
         vector<NestedInteger> nestedList = ni.getList();
         for (int i = 0; i < nestedList.size(); ++i)
             dfs(nestedList[i]);
     }
 }
开发者ID:Altynai,项目名称:LeetCode,代码行数:9,代码来源:answer.cpp

示例10: getValue

 void getValue(NestedInteger& a, vector<int>& v, int lvl) {
     if (a.isInteger()) {
         v[lvl] += (v.size() - lvl) * a.getInteger();
     }
     else {
         for (auto b: a.getList()) {
             getValue(b, v, lvl + 1);
         }
     }
 }
开发者ID:alvas,项目名称:ms_interview_100,代码行数:10,代码来源:NestedListWeightSumII.cpp

示例11: putNestedListIntoVector

 void putNestedListIntoVector(const NestedInteger &nested) {
     if (nested.isInteger()) {
         integerList.push_back(nested.getInteger());
     } else {
         const vector<NestedInteger> &v = nested.getList();
         for (int i = 0; i < v.size(); i++) {
             putNestedListIntoVector(v[i]);
         }
     }
 }
开发者ID:zhushh,项目名称:leetcode,代码行数:10,代码来源:flattenNestedListIterator.cpp

示例12: configNums

 void configNums(const NestedInteger i,vector<int>& nums){
     if(i.isInteger()){
         nums.push_back(i.getInteger());
     } else {
         vector<NestedInteger> list = i.getList();
         for(NestedInteger j : list){
             configNums(j, nums);
         }
     }
 }
开发者ID:778477,项目名称:LeetCode,代码行数:10,代码来源:00341.+Flatten+Nested+List+Iterator.cpp

示例13: hasNext

 bool hasNext() {
     while (!s.empty()) {
         NestedInteger t = s.top(); 
         if (t.isInteger()) return true;
         s.pop();
         for (int i = t.getList().size() - 1; i >= 0; --i) {
             s.push(t.getList()[i]);
         }
     }
     return false;
 }
开发者ID:yqliving,项目名称:leetcode,代码行数:11,代码来源:solution.cpp

示例14: getDepth

    int getDepth(NestedInteger& root) {
        if (root.isInteger()) {
            return 1;
        }

        int depth = 0;
        for (auto child : root.getList()) {
            depth = max(depth, getDepth(child) + 1);
        }

        return depth;
    }
开发者ID:pmdiano,项目名称:shuati,代码行数:12,代码来源:solution.cpp

示例15: dfs

 void dfs(NestedInteger& it, vector<int>&result, int dep){
     if(result.size() < dep+1){
         result.resize(dep+1);
     }
     if(it.isInteger()){
         result[dep] += it.getInteger();
     }else{
         for(auto t : it.getList()){
             dfs(t, result, dep+1);
         }
     }
 }
开发者ID:csxuejin,项目名称:LeetcodeCpp,代码行数:12,代码来源:364.cpp


注:本文中的NestedInteger类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。