400. Nth Digit
题目描述
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …
Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 231).
Example 1:
Input:
3
Output:
3
Example 2:
Input:
11
Output:
0
Explanation:
The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, … is a 0, which is part of the number 10.
代码实现
class Solution {
public:
long long int pow10(int i) {
long long int res = 1;
while(--i)
res *= 10;
return res;
}
int findNthDigit(int n) {
long long int dim[32] = {0};
int ind = 0;
int stt = 0;
for(int i = 0; i < 32; i++) {
dim[i] = (i+1)*9*pow10(i+1);
if(dim[i] > INT_MAX || n - dim[i] <= 0) { ind = i; break; }
n -= dim[i];
}
stt = pow10(ind+1) + (n-1)/(ind+1);
int ind2 = n%(ind+1) == 0?ind + 1:n%(ind+1);
stringstream ss;
ss << stt;
return ss.str()[ind2 - 1] - '0';
}
};
还有人的方法和我很像:
class Solution
{
public:
int findNthDigit(int n)
{
// step 1. calculate how many digits the number has.
long base = 9, digits = 1;
while (n - base * digits > 0)
{
n -= base * digits;
base *= 10;
digits ++;
}
// step 2. calculate what the muber is.
int index = n % digits;
if (index == 0)
index = digits;
long num = 1;
for (int i = 1; i < digits; i ++)
num *= 10;
num += (index == digits) ? n / digits - 1 : n / digits;;
// step 3. find out which digit in the number is we want.
for (int i = index; i < digits; i ++)
num /= 10;
return num % 10;
}
};
202. Happy Number
题目描述
Write an algorithm to determine if a number is “happy”.
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
代码实现
class Solution {
public:
inline bool isCycle(int sum, set<int> &cycle) {
if(cycle.count(sum) != 0) return true;
cycle.insert(sum); return false;
}
bool isHappy(int n) {
int sum = n;
stringstream ss;
set<int> cycle;
while(sum != 1) {
if(isCycle(sum, cycle)) return false;
ss << sum;
string tmp = ss.str();
int len = tmp.length();
sum = 0;
while(len--)
sum += (tmp[len] - '0')*(tmp[len] - '0');
ss.str("");
}
return true;
}
};
在这里需要注意的是使用inline内联函数以后速度有了很大的提高。
C++中使用stringstream进行数字字符串的转化以及stringstream的使用:
1、stringstream的清空:http://blog.csdn.net/u012954083/article/details/23483619
2、数字字符串之间的转换:http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html
3、stringstream数字字符串之间转换:http://blog.csdn.net/touzani/article/details/1623850/
257. Binary Tree Paths
题目描述
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
代码实现
/**
* 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:
void btp(TreeNode *root, vector<string> &res, string tmp) {
if(!root) { if(tmp.length()) res.push_back(tmp); return; }
else {
if(tmp.length()) tmp += "->";
stringstream ss;
ss << root->val;
tmp += ss.str();
ss.str("");
if(!root->left && !root->right) { res.push_back(tmp); }
if(root->left) btp(root->left, res, tmp);
if(root->right) btp(root->right, res, tmp);
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
string tmp;
btp(root, res, tmp);
return res;
}
};
还有这样的递归方法:
void binaryTreePaths(vector<string>& result, TreeNode* root, string t) {
if(!root->left && !root->right) {
result.push_back(t);
return;
}
if(root->left) binaryTreePaths(result, root->left, t + "->" + to_string(root->left->val));
if(root->right) binaryTreePaths(result, root->right, t + "->" + to_string(root->right->val));
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
if(!root) return result;
binaryTreePaths(result, root, to_string(root->val));
return result;
}
还有BFS的方法:
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
queue<pair<int, TreeNode*>> q;
vector<string> path;
vector<string> res;
path.push_back("");
if(!root) return res;
q.push({0, root});
while(!q.empty()){
auto p = q.front();
q.pop();
string s = path[p.first];
s = s==""? to_string(p.second->val) : s + "->" + to_string(p.second->val);
if(!p.second->left && !p.second->right) res.push_back(s);
else{
path.push_back(s);
if(p.second->left) q.push({path.size()-1, p.second->left});
if(p.second->right) q.push({path.size()-1, p.second->right});
}
}
return res;
}
};