class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
int temp;
while(stack2.empty()){
while(!stack1.empty()){
temp = stack1.top();
stack1.pop();
stack2.push(temp);
}
}
temp = stack2.top();
stack2.pop();
return temp;
}
private:
stack<int> stack1;
stack<int> stack2;
};
void sortage(int ages[],int length){
if (ages==NULL || length <0){
return;
}
int oldest = 99;
int array[oldest+1];
for (int i =0; i<oldest;i++){
array[i] = 0;
}
for (int j = 0; j<length; j++){
int temp_age = ages[j];
if(temp_age<0 ||temp_age>99){
cout<<"wrong ages"<<endl;
}
array[temp_age] ++;
}
int index;
for (int i = 0; i<oldest; i++){
for (int j = 0; j<numbers; j++){
ages[index] = i;
index ++;
}
}
}
class Solution { //在這裏採用的是自底而上的方法
public:
int Fibonacci(int n) {
int array[2] = {0,1};
if (n<2){
return array[n];
}
else{
long long one =0;
long long two =1;
long long sum =0;
for (long long i = 2;i<=n;i++){
sum = one + two;
one = two;
two = sum;
}
return sum;
}
}
————————————————
版權聲明:本文爲CSDN博主「ttomchy」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ttomchy/article/details/104595222
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if(rotateArray.empty()){
return 0;
}
int index1 = 0;
int index2 = rotateArray.size()-1;
int midindex = index1;
while (rotateArray[index1] > rotateArray[index2]){
midindex = (index1 + index2 ) /2;
if (index2 - index1 ==1){
midindex = index2;
break;
}
else if (rotateArray[index1] == rotateArray[index2] &&
rotateArray[index1] == rotateArray[midindex]){
return minorder(rotateArray,index1,index2);
}
else if (rotateArray[midindex] > rotateArray[index2]){
index1 = midindex ;
}
else if (rotateArray[midindex] <= rotateArray[index2]){
index2 = midindex ;
}
}
return rotateArray[midindex];
}
int minorder(vector<int> rotateArray,int index1,int index2){
int temp = rotateArray[index1];
for (int i =index1+1; i<=index2;i++){
if (rotateArray[i]<temp){
temp = rotateArray[i];
}
}
return temp;
}
};
————————————————
版權聲明:本文爲CSDN博主「ttomchy」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ttomchy/article/details/104594220
class Solution {
public:
int jumpFloor(int number) {
int array[2] = {0,1};
if (number<2){
return array[number];
}
else{
long long one =1;
long long two =1;
long long sum =0;
for (long long i = 2;i<=number;i++){
sum = one + two;
one = two;
two = sum;
}
return sum;
}
}
};
————————————————
版權聲明:本文爲CSDN博主「ttomchy」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ttomchy/article/details/104595941
class Solution {
public:
int NumberOf1(int n) {
int count = 0;
while(n){
count ++;
n = (n-1) &n;
}
return count;
}
};
————————————————
版權聲明:本文爲CSDN博主「ttomchy」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
劍指 offer 計算連表倒數第 k 個節點
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
long count =0;
ListNode* node = pListHead;
while(node != NULL){
node = node->next;
count ++;
}
if (pListHead ==NULL || k ==0 || k> count){
return NULL;
}
ListNode* first = pListHead;
ListNode* second = pListHead;
for (int i = k-1; i>0;i--){
if (first ->next!= NULL){
first = first->next;
}
}
while (first ->next!= NULL){
first = first->next;
second = second->next;
}
return second;
}
};
劍指 offer 反轉鏈表
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if (pHead ==NULL){
return NULL;
}
ListNode *pre = NULL;
ListNode *cur = pHead;
ListNode *temp = NULL;
while (cur != NULL){
temp = cur->next;//在這裏暫存一下下一個指針
cur->next = pre;//把當前的值指向前一個值
pre = cur;//前一個值向後走一步
cur = temp;// 把下一個值付給當前的值
}
return pre;
}
};
劍指 offer 鏈表合併
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
if (pHead1 ==NULL){
return pHead2;
}
if (pHead2 ==NULL){
return pHead1;
}
ListNode * result;
if (pHead1->val < pHead2->val){
result = pHead1;
result->next = Merge(pHead1->next,pHead2);
}
else{
result = pHead2;
result->next = Merge(pHead1,pHead2->next);
}
return result;
}
};
劍指 offer 判斷一棵樹是否包含另一棵樹
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
if (pRoot1 == NULL || pRoot2 ==NULL){
return false;
}
bool result= false;
if (pRoot1->val ==pRoot2->val){
result = treeonecntainstree2(pRoot1,pRoot2);
}
if (!result){
result = HasSubtree(pRoot1->left,pRoot2);
}
if (!result){
result = HasSubtree(pRoot1->right,pRoot2);
}
return result;
}
bool treeonecntainstree2(TreeNode* pRoot1, TreeNode* pRoot2){
if (pRoot2==NULL){
return true;
}
if (pRoot1 ==NULL){
return false;
}
if (pRoot1->val != pRoot2->val){
return false;
}
return treeonecntainstree2(pRoot1->left,pRoot2->left) && treeonecntainstree2(pRoot1->right,pRoot2->right) ;}
};
劍指 offer 樹的鏡像
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if (pRoot ==NULL){
return;
}
TreeNode *temp ;
temp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = temp;
if(pRoot->left!=NULL){
Mirror(pRoot->left);
}
if (pRoot->right!=NULL){
Mirror(pRoot->right);
}
}
};
劍指 offer 計算棧中最小的數據
class Solution {//在這裏利用一個輔助棧來解決問題
public:
void push(int value) {
datastack.push(value);
if (minstack.empty()){
minstack.push(value);
}
if (value <= minstack.top()){
minstack.push(value);
}
}
void pop() {
if (datastack.empty()){
return;
}
if (minstack.top() == datastack.top() ){
minstack.pop();
}
datastack.pop();
}
int top() {
return datastack.top();
}
int min() {
return minstack.top();
}
private:
stack<int> datastack;
stack<int> minstack;
};
劍指 offer 從上到下打印二叉樹
vector<TreeNode*> data;
vector<int> result;
data.push_back(root);
if (root == nullptr) return result;
while (!data.empty()){
TreeNode *temp = data[0];
result.push_back(temp->val);
data.erase(data.begin());
if(temp->left){
data.push_back(temp->left);
}
if(temp->right){
data.push_back(temp->right);
}
}
return result;
劍指 offer 從上到下打印二叉樹 (二)
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
vector<int> nodevalue;
deque<TreeNode*> dequenode;
if (root ==nullptr) {
return nodevalue;
}
dequenode.push_back(root);
while (dequenode.size()){
TreeNode* temp_node = dequenode.front();
nodevalue.push_back(temp_node->val);
dequenode.pop_front();
if(temp_node->left !=NULL){
dequenode.push_back(temp_node->left);
}
if(temp_node->right!=NULL){
dequenode.push_back(temp_node->right);
}
}
return nodevalue;
}
};
樹的遍歷
void preordersearch(Tree * t){
if(t == NULL){
return;
}
cout<<t->val<<endl;//先序遍歷,根左右
preordersearch(t->left);
preordersearch(t->right);
}
void inorder(Tree *t){ //中序遍歷,左中右
if(t==NULL){
return;
}
preordersearch(t->left);
cout<< t->val<<endl;
preordersearch(t->right);
}
void tailorder(Tree *t){//後序遍歷,左右中
if (t== NULL){
return ;
}
tailorder(t->left);
tailorder(t->right);
cout<<t->val<<endl;
}
劍指 offer 判斷一棵樹是不是後序遍歷
class Solution {
public:
bool VerifySquenceOfBST(vector<int> sequence) {
return bst(sequence, 0, sequence.size() - 1);
}
private:
bool bst(vector<int> seq, int begin, int end){
// 邊界條件
if(seq.empty() || begin > end)
return false;
// 劃分左右子樹,並判斷左右子樹和根節點的關係
int i = begin;
for(; i < end; ++i)
if(seq[i] > seq[end])
break;
int j = i;
for(; j < end; ++j)
if(seq[j] < seq[end])
return false;
// 判斷左子樹是不是二叉搜索樹
bool left = true;
if(i > begin)
left = bst(seq, begin, i - 1);
// 判斷右子樹是不是二叉搜索樹
bool right = true;
if(i < end - 1)
right = bst(seq, i , end - 1);
return left && right;
}
};
c++ 歸併排序
void merge(int array,int L;int mid; int R){
int p1 = L,p2 = mid+1, i=0;
int temp[L+R-1];
while (p1<mid && p2<R){
temp[i++] = array[p1] < array[p2] ? array[p1++] : array[p2++];
}
while (p1<mid){
temp[i++] = array[p1++];
}
while (p2<R){
temp[i++] = array[p2++];
}
for (int i =0; i< R+L-1;i++){
array[L+i] = temp[i];
}
}
void sort(int array[], int L,int R){
if (L<R){
int mid = (L+R)/2;
sort(array,L,mid);
sort(array,mid+1,R);
merge (array,L,mid,R);
}
}
int getlength(ListNode* listnode){
int count =0;
ListNode* temp = listnode;
while(temp!=NULL){
count ++;
temp = temp->next;
}
}
求兩個單鏈表的共同節點
Listnode * getcommon(ListNode* list1, ListNode* list2){
int length1 = getlength(list1);
int length2 = getlength(list2);
if(length1>length2){
ListNode* longlist = list1;
ListNode* shortlist = list2;
int diff = length1 - length2;
} else{
ListNode* longlist = list2;
ListNode* shortlist = list1;
int diff = length2 - length1;
}
for (int i=0;i<diff;i++){
longlist = longlist->next;
}
while (longlist->next!= NULL && shortlist!= NULL && (longlist != shortlist)){
longlist = longlist->next;
shortlist = shortlist->next;
}
return longlist;
}
計算樹的深度
int treedepth(binarytree *tree){
if(tree==NULL){
return 0;
}
int left = treedepth(tree->left);
int right = treedepth(tree->right);
return (left> right) ? left++: right++;
}
快速排序算法的遞歸與非遞歸
//quick sort
void quicksort (int array[], int low, int high){
if(low<high){
int index = getindex(array,low,high);
quicksort(array,index+1,high);
quicksort(array,0,index);
}
}
int getindex(int array[],int low, int high){
int temp = array[low];
while (low<high){
if(array[high] >temp ){
high --;
}
array[low] = array[high];
if(array[low]<temp){
low++;
}
array[high] = array[low];
}
array[low] =temp;
return low;
}
//非遞歸的形式來實現快排
void quichsortnorecusive(int array[],int low,int high[]){
stack<int> s;
s.push(low);
s.push(high);
while(!s.empty()){
int h = s.top(); s.pop();
int l = s.top(); s.pop();
int index= getindex(array,l,h);
if(index-1>l){
s.push(l);//左邊
s.push(index-1);//右邊
}
else(index+1<h){
s.push(index +1);
s.push(h);
}
}
}
樹的遍歷 遞歸與非遞歸實現
void preorder(Tree *tree){
if(t==NULL){
return;
}
cout<<tree->val<<endl;
preorder(tree->left);
preorder(tree->right);
}
void inorder(Tree *tree){
if(tree==NULL){
return;
}
inorder(tree->left);
cout<<tree->val;
inorder(tree->right);
}
void lastorder (Tree *tree){
if (tree== NULL){
return;
}
lastorder(tree->left);
lastorder(tree->right);
cout<<tree->val;
}
void preorder( Tree *tree){
stack<Tree *> s;
while(tree != NULL || (!s.empty())){
if(tree!=NULL){
cout<<tree->val<<endl;
s.push(tree);
tree = tree->left;
}else{
tree = s.top();
s.pop();
tree = tree->right;
}
}
}
void inorder(Tree* tree){
stack<Tree*>s;
while(tree!=NULL || !s.empty()){
if (tree!=NULL){
s.push(tree);
tree = tree->left();
} else{
tree =s.top();
s.pop();
cout<<tree->val;
tree = tree->right;
}
}
}
void lastvisit(Tree *tree){
stack<int> s;
Tree * last = root;
while (tree!=NULL || !s.empty()){
if(tree!=NULL){
s.push(tree);
tree = tree->left;
} else if( tree->right ==NULL || last == tree->right){
tree = s.top();
cout<<tree->val<<endl;
last = tree;
tree =NULL;
} else{
tree = tree->right;
}
}
}
// 從上到下打印二叉樹
void printtree(Tree * tree){
if (tree ==NULL){
return;
}
dequeue<Tree*> s;
s.push_back(tree);
while(!s.empty()){
Tree *temp = s.front();
s.pop_front();
if(tree->left!=NULL){
tree = tree->left;
s.push_back(tree);
}
if(tree->right !=NULL){
tree = tree->right;
s.push_back(tree)
}
}
}
leetcode 62 路徑問題
————————————————
版權聲明:本文爲CSDN博主「ttomchy」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ttomchy/article/details/105023012
class Solution {
public:
int uniquePaths(int m, int n) {
if(m<0 ||n<0){
return 0;
}
long long dp[m][n];
for (int i=0; i<m;i++ ){
for (int j=0;j<n;j++){
dp[i][j]=0;
}
}
for (int i=0;i<m;i++){
dp[i][0]=1;
}
for (int j=0;j<n;j++){
dp[0][j]=1;
}
for (int i=1;i<m;i++){
for (int j=1;j<n;j++){
dp[i][j] = dp[i-1][j]+dp[i][j-1];
}
}
return dp[m-1][n-1];
}
};
leetcode 計算最小路徑
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
if(m<=0||n<=0){
return 0;
}
int dp[m][n];
dp[0][0] = grid[0][0];
for (int i =1;i<m;i++){
dp[i][0]= grid[i][0]+ dp[i-1][0];
}
for (int j=1;j<n;j++){
dp[0][j] = grid[0][j] + dp[0][j-1];
}
for (int i=1;i<m;i++){
for (int j=1;j<n;j++){
dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + grid[i][j];
}
}
return dp[m-1][n-1];
}
};
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
if(m<=0||n<=0){
return 0;
}
int dp[m][n];
dp[0][0] = grid[0][0];
for (int i =1;i<m;i++){
dp[i][0]= grid[i][0]+ dp[i-1][0];
}
for (int j=1;j<n;j++){
dp[0][j] = grid[0][j] + dp[0][j-1];
}
for (int i=1;i<m;i++){
for (int j=1;j<n;j++){
dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + grid[i][j];
}
}
return dp[m-1][n-1];
}
};
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
if(m<=0||n<=0){
return 0;
}
int dp[m][n];
dp[0][0] = grid[0][0];
for (int i =1;i<m;i++){
dp[i][0]= grid[i][0]+ dp[i-1][0];
}
for (int j=1;j<n;j++){
dp[0][j] = grid[0][j] + dp[0][j-1];
}
for (int i=1;i<m;i++){
for (int j=1;j<n;j++){
dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + grid[i][j];
}
}
return dp[m-1][n-1];
}
};
01 揹包問題
狀態轉移方程:
定義f[i][j]:前i個物品,揹包容量j下的最優解
1)當前揹包容量不夠(j < w[i]),爲前i-1個物品最優解:f[i][j] = f[i-1][j]
2)當前揹包容量夠,判斷選與不選第i個物品
選:f[i][j] = f[i-1][j-w[i]] + v[i]
不選:f[i][j] = f[i-1][j]
#include<iostream>
using namespace std;
const int N=1010;
int n,m;
int value[N];
int weight[N];
int f[N][N];
int main(){
cin>>n>>m;
for(int i =1;i<=n;i++){
cin>>weight[i]>> value[i];
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
f[i][j] = f[i-1][j];//左半邊的子集,就是對於不選擇當前物體;
if(j>=weight[i])
{
f[i][j] = max(f[i][j],f[i-1][j-weight[i]] +value[i] );
}
}
}
//m 爲最開始定義的體積的總的質量
cout<< f[n][m]<<endl;
}
————————————————
版權聲明:本文爲CSDN博主「ttomchy」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ttomchy/article/details/105157114
完全揹包問題與01揹包問題分析
/*在這裏對比分析一下 01揹包問題和完全揹包問題的解法
01揹包問題:f[i][j] = max(f[i-1][j],f[i-1][j-v[i]+w[i])
完全揹包問題:f[i][j] = max(f[i-1][j],f[i][j-v[i]+w[i])
//v 代表體積; w 代表價值;
*/
#include<iostream>
using namespace std;
const int N=1010;
int w[N],v[N];
//v 代表體積; w 代表價值;
int f[N][N];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n; i++){
cin >>v[i]>>w[i];
}
for (int i =1;i<=n;i++){
for (int j =1;j<=m;j++){
f[i][j] = f[i-1][j];
if(j>=v[i]){
f[i][j] = max(f[i][j], f[i][j-v[i]]+w[i]);
}
}
}
cout<< f[n][m]<<endl;
}
#include<iostream>
using namespace std;
const int N= 10000;
char A[N],B[N];
int n,m;
int f[N][N];
int main(){
cin>>n>>m;
for (int i =1;i<=n;i++){
cin>> A[i];
}
for (int j =1;j<=m;j++){
cin>> B[j];
}
for (int i =1;i<=n;i++){
for (int j=1;j<=m;j++){
f[i][j] = max(f[i-1][j],f[i][j-1]);
if(A[i]==B[j]){
f[i][j] = max(f[i][j],f[i-1][j-1]+1);
}
}
}
cout<<f[n][m]<<endl;
return 0;
}
leetcode 兩數之和
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
map<int,int> m;
for (int i=0;i<nums.size(); i++){
m[nums[i]] = i;//在這裏進行一遍哈希,把數據存下來
}
vector<int > result;
for (int i =0; i<nums.size(); i++){
if(m.find(target - nums[i])!=m.end() && m[ target - nums[i]] != i) { //在這裏進行一下數據的查找,如果找的到數據,並且不是 i 本身的話 就可以放入結果之中
result.push_back(i);
result.push_back(m[target-nums[i]]);
return result;
}
}
return {};
/*
vector<int> result;
for(int i=0;i<nums.size();i++){
for(int j=i;j<nums.size();j++){
if (nums[i] + nums[j] ==target &&i!=j){
result.push_back(i);
result.push_back(j);
}
}
}
return result;
*/
}
};
// 加油加油 Try to make yourself more excellent.
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
long i =0;
long j = nums.size();
if(j ==0){
return 0;//這裏也要判斷元素的個數
}
if (nums[j-1]<target){
return j; //在這裏也要加入判斷元素的個數
}
while (i<=j){
long mid = (i+j)/2;
if(nums[mid] ==target){
return mid;
}
else if (nums[mid] >target){
j = mid -1;
}
else {
i = mid +1;
}
}
return i;
}
};
//加油加油,try to make yourself more excellent
leetcode 兩數之和
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
//在這裏利用劍指 offer 的方法,使用兩個指針,一個從前向後掃,一個從後向前掃
int N = numbers.size();
vector<int> result;
if(N<2){
return result;
}
int left =0;
int right =N-1;
while (left<right){
if (numbers[left] + numbers[right] == target){
result.push_back(left+1);
result.push_back(right+1);
break;
}
else if(numbers[left] + numbers[right] < target){
left ++;
}
else{
right --;
}
}
return result;
}
};
leetcode 二分查找錯誤版本
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
int firstBadVersion(int n) {
long left = 0;
long right = n;
if(n==0){
return 0;
}
while (left<=right){ //典型的二分查找算法
long mid = (right +left)/2;
if(isBadVersion(mid)){
right = mid-1;
} else{
left = mid +1;
}
}
return left;
}
};
leetcode 27 移除元素
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int size = nums.size();
for (int i =0; i<nums.size(); i++){
if(nums[i] ==val){
nums[i] = INT_MAX;
}
}
nums.erase(remove(nums.begin(),nums.end(),INT_MAX),nums.end());
return nums.size();
}
};// 加油加油,Try to make yourself more excellent...
leetcode 53. 最大子序和
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int size = nums.size();
if(size ==0){
return 0;
}
int dp = nums[0];
int maxdp = nums[0];
for (int i =1; i<size;i++){
if (dp>0){
dp = dp + nums[i];
} else {
dp = nums[i];
}
if (dp > maxdp){
maxdp = dp;
}
}
return maxdp;
}
};
//動態規劃,遞歸公式:dp[i] = data[i] i=0或dp[i-1]<=0
// dp[i-1]+data[i] i!=0且dp[i-1]>0
//由於只需知道前一個情況的dp值,因此可省去dp數組,申請個變量記錄即可
//加油加油 Try to make yourself more excellent
leetcode 860. 檸檬水找零
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int five =0; //在這裏定義一下初始值
int ten =0;
for (auto b:bills){ //開始遍歷數據
if(b==5){
five ++; //如果 遇到了個 5 ; five +1;
}else if(b==10){
ten ++; //如果是10 ; ten +1; fvie -1;
if(five){
five --;
} else{
return false; //如果沒有 five 則 返回 false
}
}
else{
int temp =15;//定義一個臨時變量 15
if(ten){// 如果有10的話
ten--;// ten -1
temp -=10; //temp -10;
}
while (temp && five){ //當temp >0 或者 five 存在時
five --;
temp -=5; //temp 每次減 5
}
if(temp){ //如果最後temp還存在的話,就是無法找開零錢
return false;
}
}
}
return true; //否則的話就返回 true
// 加油加油 。Try to make yourself more excellent
}
};
455. 貪心算法 分發餅乾
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort (g.begin(),g.end());// g 爲 孩子的胃口大小
sort (s.begin(),s.end());// s 爲餅乾的大小
int result =0;
int i =0;
int j =0; // 這道題是一個典型的貪心算法的例子,我們首先將孩子們的期望的大小和餅乾的大小進行一下排序,
// 然後進行遍歷餅乾的大小判斷一下是否符合孩子們的期望不是的話 就向後移動一個指針,如果滿足的話
// 就把 res +1; j+1; 因爲這個孩子的期望已經滿足了
for( int i =0;i<g.size();i++){//s 爲餅乾的大小
while(j< s.size() && s[j]<g[i] ){
j++; // 餅乾向後移動一下
}
if(j< s.size()) {
j++; //這個餅乾已經用完所以要+1
result ++;//最後的結果要 +1;
}
}
return result;
}
};
// 加油加油。 Try to make yourself more excellent...
class Solution {
public:
int jump(vector<int>& nums) {
/*
對於這道題目的話,首先這道題目可以看出來 假設F(x) 爲 能跳到的最遠的距離
那麼 F(x) 爲分段函數,舉個例子
0,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4
第 0個格子只需要 0步就可以跳的到;
第 1到3 個格子需要1 步就可以跳的到
第4 到7 個格子可以兩個步驟就可以跳的到
*/
int left =0; //因此在這裏我們利用一個區間搜索的形式來進行搜索 left左邊界,right 右邊界
int right =0;
int temp =0;
if(nums.size() ==1){
return 0;
}
while (left <=right){//最開始的時候 leftleft==right==0
int max_dist =0; //最開始的時候我們利用一個變量來代表當前區間內最遠能到的距離
for(int i=left;i<=right; i++){
max_dist = max(max_dist, nums[i] + i);
}
left = right +1;//在這裏不斷地更新左邊界和右邊界;
right = max_dist;
temp ++;//步數 +1
if(right >= (int) nums.size()-1) {
break;//當最後的數據已經在我們當前區間能到達的最遠範圍的時候,就停止,用 break;
}
}
return temp;
}
};
//加油,加油,Try to make yourself more excellent...
454. 使用hash 四數相加 II
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map <int ,int> res; //在這裏使用hash map 來進行計算
for(auto a:A){
for (auto b:B){
res[a+b] ++;
}
}
int result =0;
for( auto c:C){
for(auto d:D){
result += res[-c-d]; //在這裏對每個值進行累加 注意這裏的: -C-D
}
}
return result;
}
};
// 加油加油。 Try to make yourself more excellent
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map <int ,int> hash;//在這裏的思想爲: 我們假設一個區間
int result =0;
int s =0;
hash[0] =1;//這裏設置爲 1 的原因是,如果輸入爲 [3,-3], K=0;
// 那麼就需要我們設置 hash[0] =1; 這個初始值了
for(auto x:nums){
s +=x;
if(hash.find(s-k) != hash.end()){
result += hash[s-k];//把前面 hash[s-k] 的次數累加起來
}
hash[s] ++;
}
return result; //返回來結果
}
};
/*假如存在區間[left,right],使得在[left,right]這個區間的子數組的和爲k。換句話說,就是前right項和減去前left項和等於k,即前left項和等於前right項和減去k。
可以這樣做,在掃描數組的同時,假設當前掃到第i位,記錄它的前i項和sum,用該和減去k,即sum-k,判斷sum-k是否爲某個位置的前n項和,若是,更新統計量。
作者:jarvis1890
鏈接:https://leetcode-cn.com/problems/subarray-sum-equals-k/solution/qian-zhui-he-shi-xian-jian-dan-by-jarvis1890/
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
*/
// 加油加油。Try to make yourself more excellent
class Solution {
public:
static bool compare(pair<int, int> left, pair<int,int> right){
return left.second > right.second; //首先在這裏我們定義一個函數用來比較數據,結果爲從大到小排序
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map <int, int> hash;
for (int i =0; i<nums.size(); i++){
hash[nums[i]] ++;// 在這裏利用哈希表進行統計每個數字出現的頻率
}
vector <pair<int,int>> v(hash.begin(),hash.end());// 把hash 的數據放到vector裏面
sort (v.begin(),v.end(),compare); //進行排序,結果爲從大到小排序
vector<int> result;
for(int i =0; i<k; i++){
result.push_back(v[i].first);// 在這裏我們將前 K 個數據進行輸出
}
return result;
}
};
// 加油加油. Try to make yourself more excellent...
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
if(nums1.size() >nums2.size()){ // 在這裏我們設置一下數據,少的那個找出來
return intersect(nums2,nums1);// 交換數據
}
unordered_multiset<int> hash; //在這裏我們選擇multiset
for(auto x : nums1){
hash.insert(x);//插入數據
}
vector<int> result;
for(auto x : nums2){
if(hash.count(x)>0){//如果數據已經存在的話
result.push_back(x); //保存數據
auto it = hash.find(x);// 刪除掉其中一個數據
hash.erase(it);
}
}
return result; //返回結果
}
};
// 加油加油... Try to make yourself more excellent...