1LL(1ll) 与 std::to_string()
发布时间:2020-12-16 09:15:39 所属栏目:百科 来源:网络整理
导读:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x),left(NULL),right(NULL) {} * }; */class Solution {public: bool isValidBST(TreeNode* root) { return dfs(r
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x),left(NULL),right(NULL) {} * }; */ class Solution { public: bool isValidBST(TreeNode* root) { return dfs(root,INT_MIN,INT_MAX); } bool dfs(TreeNode *root,long long minv,long long maxv){ if(!root) return true; if(root -> val < minv || root -> val > maxv) return false; return dfs(root -> left,minv,root -> val -1ll) && dfs(root -> right,root -> val + 1ll,maxv); } }; 1LL是long long 类型的1,一个int 型的数和一个long long 型的数做运算返回值是long long 类型的,利用这种方式来防止溢出。 std::to_string() 是c++11引入的函数,其使用方法如下: string to_string (int val); string to_string (long val); string to_string (long long val); string to_string (unsigned val); string to_string (unsigned long val); string to_string (unsigned long long val); string to_string (float val); string to_string (double val); string to_string (long double val); 样例如下: // to_string example #include <iostream> // std::cout #include <string> // std::string,std::to_string int main () { std::string pi = "pi is " + std::to_string(3.1415926); std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number"; std::cout << pi << 'n'; std::cout << perfect << 'n'; return 0; } Output: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- SQLite查找表是否有时有记录
- c# – 你可以从.net运行SSIS任务吗?
- [Swift通天遁地]一、超级工具-(20)图片面部聚焦:使图像视图
- React Native: Possible unhandled promise rejection requ
- actionscript-3 – 在儿童电影剪辑上的as3 gotoandplay帧
- React Native 学习(二)---仿微信我的钱包界面
- ruby-on-rails – 在Rails中撤消以前播种的数据
- Oracle OCP笔记(29)RMAN备份 - Config
- ruby – 如何在nanoc中创建“draft”项目?
- c# – 为什么我的for-each只抛出一个例外?