class Solution {
public:
vector<int>plusOne(vector<int>& digits){int t =1;//这里使用的是高精度加法,模拟进位即可;类似的高精度乘法等for(int i = digits.size()-1; i >=0; i--){int&x = digits[i];
t += x;
x = t %10;
t /=10;}//如果最后一位还有进位if(t){//因为这里是0是高位,所有要挪动一位
digits.push_back(0);for(int i = digits.size()-1; i >0; i--)digits[i]= digits[i-1];
digits[0]=1;}return digits;}};
LeetCode 326. Power of Three
class Solution {
public://这里当然也可以用循环和递归一直除3,时间复杂度差不多
bool isPowerOfThree(int n){return n >0&&1162261467% n ==0;}};
LeetCode 883. Projection Area of 3D Shapes
class Solution {
public:intprojectionArea(vector<vector<int>>& grid){if(grid.empty()|| grid[0].empty())return0;int res =0;int n = grid.size(), m = grid[0].size();//首先转换为二进制,把非0转化为0,0转化为1,然后再取非相加for(int i =0; i < n; i++)for(int j =0; j < m; j++)
res +=!(!grid[i][j]);//取行列的最大值即可for(int i =0; i < n; i++){int h =0;for(int j =0; j < m; j++)h =max(h,grid[i][j]);
res += h;}for(int j =0; j < m; j++){int h =0;for(int i =0; i < n; i++)h =max(h, grid[i][j]);
res += h;}return res;}};
LeetCode 230. Kth Smallest Element in a BST
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/// left = 左子树的个数// 1.if k <= left, return dfs(root->left, k);// 2.if k == left+1, return root->val;// 3.if k > left+1, return dfs(root->right, k - left - 1);
class Solution {
public:intkthSmallest(TreeNode* root,int k){returndfs(root, k);}intdfs(TreeNode* root,int&k){if(!root)return0;int left =dfs(root->left, k);if(k <=0)return left;if(--k ==0)return root->val;returndfs(root->right, k);}};
LeetCode 139. Word Break
// 这里用到了动态规划// 用哈希表来查询
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict){
unordered_set<string> S;for(auto word : wordDict) S.insert(word);int n = s.size();
vector<bool>f(n +1, false);
f[0]= true;for(int i =1; i <= n; i++)for(int j =0; j < i; j++){if(S.count(s.substr(j, i - j))&& f[j]){
f[i]= true;break;}}return f[n];}};
LeetCode 930. Binary Subarrays With Sum
// 这里用了前缀和// 然后把是s[i] - S转换成了f[s[i] - S]
class Solution {
public:intnumSubarraysWithSum(vector<int>& A,int S){int n = A.size();
vector<int>sum(n+1,0),f(n+1,0);for(int i =0; i < n; i++)sum[i+1]= sum[i]+ A[i];
f[0]=1;int res =0;for(int i =1; i <= n; i++){if(sum[i]>= S)res += f[sum[i]- S];
f[sum[i]]++;}return res;}};
LeetCode 228. Summary Ranges
class Solution {
public:
string rangeToString(int st,int ed){if(st == ed)returnto_string(st);returnto_string(st)+"->"+to_string(ed);}
vector<string>summaryRanges(vector<int>& nums){
vector<string> res;if(nums.empty())return res;int st = nums[0], ed = nums[0];for(int i =1; i < nums.size(); i++){int x = nums[i];if(x > ed+1){
res.push_back(rangeToString(st, ed));
st = ed = x;}else ed++;}
res.push_back(rangeToString(st, ed));return res;}};
文章目录Google面试题二LeetCode 66. Plus OneLeetCode 326. Power of ThreeLeetCode 883. Projection Area of 3D ShapesLeetCode 230. Kth Smallest Element in a BSTLeetCode 139. Word BreakLeetCode 930. Binary Subarra...