- 博客(111)
- 收藏
- 关注
原创 LeetCode-951.翻转等价二叉树
题目:代码:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullpt
2021-08-16 20:32:17
149
原创 LeetCode-91.解码方法
题目:代码:class Solution {public: int numDecodings(string s) { int n=s.size(); int temp,a,b; if(s[0]=='0') return 0; a=1; if(n==1) return 1; if(s[1]=='0'){ if(s[0]=='1'||s[0]=='2') b=1;
2021-04-21 14:38:33
788
原创 LeetCode-129.求根到叶子节点数字之和
题目:代码:class Solution {public: void dfs(TreeNode * root,int curval,int &ans){ curval=curval*10+root->val; if(root->left==NULL&&root->right==NULL){ ans+=curval; return; } if
2020-10-29 14:46:02
147
原创 LeetCode-1365.有多少小于当前数字的数字
题目:代码:class Solution {public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int count[101]={0}; int n=nums.size(); for(int i=0;i<n;i++){ count[nums[i]]++; } int tem
2020-10-26 10:39:20
96
原创 LeetCode-845.数组中的最长山脉
题目:代码:class Solution {public: int longestMountain(vector<int>& A) { int n=A.size(); if(n==0) return 0; int danzeng[n],danjian[n]; danzeng[0]=1; danjian[n-1]=1; for(int i=1;i<n;i++){
2020-10-25 11:01:25
65
原创 LeetCode-486.预测赢家
题目:https://leetcode-cn.com/problems/predict-the-winner/代码:class Solution {public: bool PredictTheWinner(vector<int>& nums) { int n=nums.size(); int arr[n][n]; for(int i=0;i<n;i++) arr[i][i]=nums[i]; f
2020-10-22 23:33:59
151
原创 LeetCode-704.二分查找
题目:代码:class Solution {public: int search(vector<int>& nums, int target) { int begin=0,end=nums.size()-1; int mid; while(begin<=end){ mid=begin+(end-begin)/2; if(nums[mid]==target) return
2020-10-22 20:53:58
69
原创 LeetCode-763.划分字母区间(2020.10.22打卡题)
题目:代码:class Solution {public: vector<int> partitionLabels(string S) { int begin[26],end[26]; for(int i=0;i<26;i++){ begin[i]=-1; end[i]=-1; } for(int i=0;i<S.size();i++){
2020-10-22 20:46:47
49
原创 LeetCode-925.长按键入
题目:代码:class Solution {public: bool isLongPressedName(string name, string typed) { int m=name.size(),n=typed.size(); int i=0,j=0,count1=0,count2=0; bool ans; while(i<m&&j<n){ if(name[i]!=type
2020-10-21 20:53:04
85
原创 LeetCode-1442.形成两个异或相等数组的三元组数目
题目:代码:class Solution {public: int countTriplets(vector<int>& arr) { int n=arr.size(); for(int i=1;i<n;i++){ arr[i]=arr[i]^arr[i-1]; } int ans=0; for(int i=0;i<n;i++){ i
2020-10-19 20:26:51
73
原创 LeetCode-844.比较含退格的字符串
题目:代码:class Solution {public: bool backspaceCompare(string S, string T) { int i = S.length() - 1, j = T.length() - 1; int skipS = 0, skipT = 0; while (i >= 0 || j >= 0) { while (i >= 0) {
2020-10-19 17:17:38
65
原创 LeetCode-605.种花问题
题目:代码:class Solution {public: bool canPlaceFlowers(vector<int>& flowerbed, int n) { int ans=0; int temp=1; for(int i=0;i<flowerbed.size();i++){ if(flowerbed[i]==0) temp++; else{
2020-10-18 23:18:56
91
原创 LeetCode-面试题10.01.合并排序的数组
题目:代码:class Solution {public: void merge(vector<int>& A, int m, vector<int>& B, int n) { int a=m+n; while(m>0||n>0){ if(m==0){ A[a-1]=B[n-1]; n--; }
2020-10-18 20:51:02
103
原创 LeetCode-1191.K次串联后的最大子数组
题目:代码:class Solution {public: int kConcatenationMaxSum(vector<int>& arr, int k) { int mod=1e9+7,n=arr.size(); int qz=0,hz=0,zj=0; int temp1=0,temp2=0; int qzmin=0; int sum=0; for(int i=0;i&l
2020-10-18 19:10:27
96
原创 LeetCode-1424.对角线遍历2
题目:代码:class Solution {public: vector<int> findDiagonalOrder(vector<vector<int>>& nums) { vector<int> ans; vector<vector<int>> temp(200000); for(int i=0;i<nums.size();i++){
2020-10-18 17:11:03
60
原创 LeetCode-977.有序数组的平方
题目:代码:class Solution {public: vector<int> sortedSquares(vector<int>& A) { int begin=0,end=A.size(); vector<int> ans(end); int num=end-1; while(begin<end){ if(A[begin]*A[begin]>
2020-10-16 23:48:18
150
原创 LeetCode-1029.两地调度
题目:代码:class Solution {public: int twoCitySchedCost(vector<vector<int>>& costs) { int N=costs.size(),ans=0; vector<int> arr(N); for(int i=0;i<N;i++){ ans+=costs[i][0]; arr[i]=
2020-08-09 21:53:08
107
原创 LeetCode-470.用 Rand7() 实现 Rand10()
题目:代码:class Solution {public: int rand10() { int row, col, idx; do { row = rand7(); col = rand7(); idx = col + (row - 1) * 7; } while (idx > 40); return 1 + (idx - 1) % 10;
2020-07-29 21:43:31
115
原创 LeetCode-372.超级次方
题目:代码:class Solution {public: int superPow(int a, vector<int>& b) { int x=1140; int temp=0; for(int i=0;i<b.size();i++){ temp=(temp*10+b[i])%x; } a=a%1337; if(a==0) return 0;
2020-07-24 22:53:30
105
原创 LeetCode-1014.最佳观光组合
题目:代码:class Solution {public: int maxScoreSightseeingPair(vector<int>& A) { int ans=A[0]+A[1]-1,m=A[0]; for(int i=2;i<A.size();i++){ m=max(m,A[i-1]+i-1); ans=max(ans,m+A[i]-i); }
2020-07-15 23:06:34
72
原创 LeetCode-169.多数元素
题目:代码:(投票算法)class Solution {public: int majorityElement(vector<int>& nums) { int ans=nums[0],count=1; for(int i=1;i<nums.size();i++){ if(nums[i]==ans) count++; else count--; if(count&
2020-07-15 22:35:22
94
原创 LeetCode-997.找到小镇的法官
题目:代码:class Solution {public: int findJudge(int N, vector<vector<int>>& trust) { int m=trust.size(); int arr[N+1]; for(int i=0;i<=N;i++) arr[i]=0; for(int j=0;j<m;j++){ arr[trust[j][
2020-06-15 15:02:47
154
原创 LeetCode-119.杨辉三角
题目:代码:class Solution {public: int gcd(int a,int b){ if(a*b==0) return a+b; else if(a>=b) return gcd(b,a%b); else return gcd(a,b%a); } vector<int> getRow(int rowIndex) { vector<int> ans(rowIndex
2020-06-15 11:10:59
71
原创 LeetCode-1287.有序数组中出现次数超过25%的数
题目:代码:class Solution {public: int findSpecialInteger(vector<int>& arr) { //这个题注意非递减就行 for(int i=0,j=arr.size()/4;j<arr.size();i++,j++){ if(arr[i]==arr[j]) return arr[i]; } return arr[0];
2020-05-28 17:49:10
113
原创 LeetCode-1342.将数字变成0的操作次数
题目:代码:class Solution {public: int numberOfSteps (int num) { int ans=0; if(num==0) return 0; while(num>0){ ans=num%2+ans+1; num/=2; } return ans-1; }};
2020-05-28 17:47:53
115
原创 LeetCode-974.和可被K整除的子数组
题目:代码:class Solution {public: int subarraysDivByK(vector<int>& A, int K) { int mem[K]; for(int i=0;i<K;i++) mem[i]=0; A[0]=A[0]%K; if(A[0]<0) A[0]+=K; mem[A[0]]++; for(int i=1;i<A.s
2020-05-27 16:11:49
70
原创 LeetCode-287.寻找重复数
题目:代码:class Solution {public: int findDuplicate(vector<int>& nums) { /** 快慢指针思想, fast 和 slow 是指针, nums[slow] 表示取指针对应的元素 注意 nums 数组中的数字都是在 1 到 n 之间的(在数组中进行游走不会越界), 因为有重复数字的出现, 所以这个游走必然是成环的, 环的入口就是重复的元素,
2020-05-26 20:40:06
90
原创 LeetCode-877.石子游戏
题目:代码:class Solution {public: bool stoneGame(vector<int>& piles) { return true; }};
2020-05-23 17:49:10
97
原创 LeetCode-823.带因子的二叉树
代码:class Solution {public: int binarysearch(vector<int>& A,int target,int begin,int end){ if(begin>end) return -1; int mid=(begin+end)/2; if(A[mid]==target) return mid; else if(A[mid]>target) return bi.
2020-05-23 17:21:25
171
原创 LeetCode-600.不含连续1的非负整数
题目:代码:class Solution {public: int fib(int n) { if (n <= 0) return 1; if (n == 1) return 2; int a = 1; int b = 2; int c = 3; int i = n - 2; while(i--) { a = b; b = c; c = a + b; } r
2020-05-23 16:34:27
308
原创 LeetCode-883.三维形体投影面积
题目:代码:class Solution {public: int projectionArea(vector<vector<int>>& grid) { int n=grid.size(); int memi[n],memj[n]; for(int i=0;i<n;i++){ memi[i]=0; memj[i]=0; } i
2020-05-22 18:20:51
116
原创 LeetCode-740.删除与获得点数
题目:代码:class Solution {public: int deleteAndEarn(vector<int>& nums) { int dp[10001]={0}; for(int i=0;i<nums.size();i++){ dp[nums[i]]++; } for(int i=2;i<=10000;i++){ dp[i]=max(dp
2020-05-22 16:48:38
153
原创 LeetCode-105.从前序遍历和中序遍历构造二叉树
题目:代码:/** * 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 {private: unordered_map<int,
2020-05-22 15:26:06
255
原创 LeetCode-643.子数组的最大平均数
题目:代码:class Solution {public: double findMaxAverage(vector<int>& nums, int k) { for(int i=1;i<nums.size();i++){ nums[i]=nums[i]+nums[i-1]; } int m=nums[k-1]; for(int i=0;i+k<nums.size();i+
2020-05-21 16:40:16
122
原创 LeetCode-540.有序数组中的单一元素
题目:代码:(二分查找的思想)class Solution {public: int singleNonDuplicate(vector<int>& nums) { int begin=0,end=nums.size()-1; int mid; while(begin<end){ mid=(begin+end)/2; if(mid%2==0){
2020-05-18 20:57:03
126
原创 LeetCode-1437.是否所有1都至少相隔k个单位
题目:代码:class Solution {public: bool kLengthApart(vector<int>& nums, int k) { int before=-100000; for(int i=0;i<nums.size();i++){ if(nums[i]==1){ if(i-before<=k){ return
2020-05-18 16:57:01
112
原创 LeetCode-935.骑士拨号器
题目:代码:class Solution {public: int knightDialer(int N) { if(N==1) return 10; int mod=1e9+7; long int a1=1,a2=1,a4=1,a0=1; long int b1,b2,b4,b0; for(int i=2;i<=N;i++){ b1=(a2+a4)%mod;
2020-05-18 16:45:44
124
原创 LeetCode-1323.6和9组成的最大数字
题目:代码:class Solution {public: int maximum69Number (int num) { int ans=num; int temp=0; while(ans>0){ temp*=10; temp+=ans%10; ans/=10; } bool flag=true; while(tem
2020-05-18 16:12:28
128
原创 LeetCode-860.柠檬水找零
题目:代码:class Solution {public: bool lemonadeChange(vector<int>& bills) { int len=bills.size(); if(len==0) return true; int five=0,ten=0; for(int i=0;i<len;i++){ if(bills[i]==5){
2020-05-15 22:47:41
245
原创 LeetCode-991.坏了的计算器
题目:代码:class Solution {public: int brokenCalc(int X, int Y) { if(X>=Y) return X-Y; int ans=0; while(Y>X){ if(Y%2==0){ Y/=2; ans++; } else{
2020-05-14 23:31:52
83
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人