可以通过调试的时候添加查看:比如说有一个长度为3的vector v,如果想要查看v[0]的值,就在添加查看中写 *(&v[0])
如果想要查看整个数组的值,就可以写*(&v[0])@3
@后面的数字表示想要查看的长度,这里vector的长度是3所以可以写3就能看到所有的值~
你可不可以
成为我的main函数
做我此生必须有
且只能有一个的入口
——————————
我愿为自己加上private
在你的class中只有
你能调用
——————————————代 码 如 诗 。
可以通过调试的时候添加查看:比如说有一个长度为3的vector v,如果想要查看v[0]的值,就在添加查看中写 *(&v[0])
如果想要查看整个数组的值,就可以写*(&v[0])@3
@后面的数字表示想要查看的长度,这里vector的长度是3所以可以写3就能看到所有的值~
Given a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.
Example 1:
Input:
s = “abpcplea”, d = [“ale”,”apple”,”monkey”,”plea”]
Output:
“apple”
Example 2:
Input:
s = “abpcplea”, d = [“a”,”b”,”c”]
Output:
“a”
Note:
All the strings in the input will only contain lower-case letters.
The size of the dictionary won’t exceed 1,000.
The length of all the strings in the input won’t exceed 1,000.
题目大意:给一个string s和一个string字典d,找字典中的某个string,寻找s的子串(满足可以通过删除s中某些元素后得到该string),寻找满足条件的字符串中最长的一个,如果有多个长度相等的就返回字典序中最小的那个,如果一个都没有满足条件的string,就返回一个空字符串~
分析:遍历字典中的某一个字符串,设当前字符串的下标为index,对于当前字符串d[index],使用两个指针i和j分别从头到尾遍历s和d[index],随着i指针的增加,如果j指针所指元素和i指针所指元素相同就向后移动一位,当i指针都指完的时候,j如果也指完了说明满足条件,当前d[index]是s的子串,如果当前d[index]的长度比保存的result字符串长度长,就更新result,或者一样长但是字典序排列中d[index]比result小,也要更新result,最后返回result~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public: string findLongestWord(string s, vector<string>& d) { string result = ""; for (int index = 0; index < d.size(); index++) { int i, j; for (i = 0, j = 0; i < s.length() && j < d[index].length(); i++) { if (s[i] == d[index][j]) j++; } if (j == d[index].length() && (result.length() < d[index].length() || (result.length() == d[index].length() && result > d[index]))) result = d[index]; } return result; } }; |
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
Example:
Input:
1
\
3
/
2
Output:
1
Explanation:
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
Note: There are at least two nodes in this BST.
题目大意:给一棵二叉搜索树,结点的值都为非负,找到两个元素值之差的绝对值的最小值~
分析:二叉搜索树,用中序遍历后得到的结果是从小到大的元素,遍历这个中序遍历后的结果数组,就可以得到两个元素值之差的绝对值的最小值~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Solution { public: int getMinimumDifference(TreeNode* root) { inOrder(root); int result = INT_MAX; for (int i = 1; i < tree.size(); i++) result = min(result, tree[i] - tree[i-1]); return result; } private: vector<int> tree; void inOrder(TreeNode* root) { if (root->left != NULL) inOrder(root->left); tree.push_back(root->val); if (root->right != NULL) inOrder(root->right); } }; |
问题描述
接受用户输⼊的数值,输出以该值为半径的(1)圆面积,(2)球体表面积,(3)球体体积。pi 取值3.1415926536 ,结果保留10位小数,每一列占20个字符,左对齐。
样例输入
一个满足题目要求的输入范例。
例:
1
样例输出
与上面的样例输入对应的输出。
例:(第一行1是输入,第二行是输出)
数据规模和约定
所有结果在double类型的表示范围内。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #define pi 3.1415926536 using namespace std; int main() { double a, b, c; double r; cin >> r; a = pi * r * r; b = 4 * pi * r * r; c = 4.0 / 3 * pi * r * r * r; printf("%-20.10lf%-20.10lf%-20.10lf", a, b, c); return 0; } |
问题描述
编写一个程序,接受用户输入的10个整数,输出它们的和。
输出格式
要求用户的输出满足的格式。
例:输出1行,包含一个整数,表示所有元素的和。
样例输入
1 2 3 4 5 6 7 8 9 10
样例输出
55
数据规模和约定
输入数据中每一个数的范围。
例:输入数<100000。
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> using namespace std; int main() { int sum = 0; for(int i = 0; i < 10; i++) { int t; cin >> t; sum += t; } cout << sum; return 0; } |
基于例子3 ,写一个程序,实现整数求和:
样例输入
3 4
样例输出
7
1 2 3 4 5 6 7 8 |
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; cout << a + b; return 0; } |