头像

zdj


访客:1528

离线:4天前



zdj
4个月前
class Solution {
    public double Power(double base, int exponent) {
        boolean flag = true;
        if(exponent < 0){
            flag = false;
            exponent = -1 * exponent;
        }
        double res = 1;
        while(exponent != 0){
            if((exponent & 1) != 0){
                res = res*base;
            }
            exponent = exponent >> 1;
            base = base*base;
        }
        if(flag)return res;
        else return 1/res;

  }
}



zdj
4个月前

class Solution {
public ListNode copyRandomList(ListNode head) {
if(head == null)
return null;
ListNode p = head;
while(p != null){
ListNode np = new ListNode(p.val);
ListNode next = p.next;
p.next = np;
np.next = next;
p = next;
}
p = head;
while(p != null){
if(p.random != null){
p.next.random = p.random.next;
}
p = p.next.next;
}
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
for(p = head;p != null;p = p.next){
curr.next = p.next;
curr = curr.next;
p.next = p.next.next;
}
return dummy.next;
}
}




zdj
5个月前

include [HTML_REMOVED]

using namespace std;
const int N = 1000;
int a[N];
int direct[N];

int main(){
int n;
int length;
int t;
int ball[N];
cin>>n>>length>>t;
for(int i = 0; i < n; i){
direct[i] = 1;
}
for(int i = 0;i < n;i
){
int x;
cin>>x;
ball[i] = x;
}
for(int i = 0;i < t; i){
for(int j = 0;j < n;j
){
bool flag = true;
for(int k = 0;k < n;k ){
if(k != j && ball[k] == ball[j]){
flag = false;
direct[k] = -1 * direct[k];
}
}
}
for(int j = 0;j < n;j
){
if(ball[j] == 0 || ball[j] == length)direct[j] = -1 * direct[j];

    }
    for(int j = 0;j < n;j ++){
        ball[j] += direct[j];
    }


}
for(int i = 0;i < n;i ++)cout<<ball[i]<<" ";

}



活动打卡代码 AcWing 836. 合并集合

zdj
7个月前

include[HTML_REMOVED]

using namespace std;
const int N = 100010;
int p[N];//返回父节点的数组
int n,m;

int find(int x){
if(p[x] != x) p[x] = find(p[x]);//优化:压缩路径,直接让子节点指向最终节点
return p[x];//返回自己的父节点

// while(p[x] != x){
//     p[x] = find(p[x]);
//     x = p[x];

// }
// return x;

}
int main(){
cin>>n>>m;
for(int i = 1; i <= n; i++) p[i] = i;//每个数的集合等于他们自己
while(m–){
string s;
cin>>s;
int x,y;
if(s == “Q”){

        cin >> x >> y;
        if(find(x) == find(y))puts("Yes");
        else puts("No");
    }
    if(s =="M"){
        cin >> x >> y;
        p[find(x)] = find(y);
    }
}

}



活动打卡代码 AcWing 835. Trie字符串统计

zdj
7个月前
#include<iostream>
using namespace std;
const int N = 100010;
int son[N][26];
int cnt[N];
int idx = 0;
void insert(string s){
    int p = 0;
    for(int i = 0; i < s[i];i++){
        int u = s[i] - '0';
        if(!son[p][u])son[p][u] = ++idx;
        p = son[p][u];
    }
    cnt[p]++;
}
int query(string s){
    int p = 0;
    for(int i = 0; i < s[i]; i++){
        int u = s[i]- '0';
        if(!son[p][u])return 0;
        p = son[p][u];
    }
    return cnt[p];
}
int main(){
    int n;
    cin>>n;
    while(n--){
        string s;
        cin>>s;
        string str;
        if(s == "I"){
            cin>>str;
            insert(str);
        }
        if(s == "Q"){

            cin>>str;
            cout<<query(str)<<endl;
        }




    }



}






//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~


活动打卡代码 AcWing 831. KMP字符串

zdj
7个月前
#include<iostream>
using namespace std;
const int N = 100010,M = 10010;
char p[M],q[N];

int m,n;
int ne[N];
int main(){
    cin>>n>>p + 1>> m>> q + 1;
    for(int i = 2,j = 0; i <= n;i++){
            while(j && p[i] != p[j+1])j = ne[j];//找到前面的最大前缀和后缀相等的那个index
            if(p[i] == p[j + 1])j ++;
            ne[i] = j;
    }
    for(int i = 1,j = 0;i <= m;i++){
        while(j && q[i] != p[j+1])j = ne[j];
        if(q[i] == p[j + 1])j++;
        if(j == n){
            cout<< i -j <<" ";
            j = ne[j];
        }
    }

}
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~


活动打卡代码 AcWing 154. 滑动窗口

zdj
8个月前

include[HTML_REMOVED]

using namespace std;
const int N = 1000010;
int q[N],a[N],hh,tt;
int main(){
int n , k;
cin >> n >> k;
hh = 0,tt = -1;
for(int i = 0;i < n;i )cin>>a[i];
for(int i = 0;i < n;i
){
if(hh <= tt && i - k + 1 > q[hh])hh;
while(hh <= tt && a[q[tt]] >= a[i])tt–;
q[
tt] = i;
if(i + 1 >= k)cout<<a[q[hh]]<<” “;

}
puts("");
hh = 0,tt = -1;
for(int i = 0;i < n;i ++)cin>>a[i];
for(int i = 0;i < n;i ++){
    if(hh <= tt && i - k + 1 > q[hh])hh++;
    while(hh <= tt && a[q[tt]] <= a[i])tt--;
    q[++tt] = i;
    if(i + 1 >= k)cout<<a[q[hh]]<<" ";

}

}



活动打卡代码 AcWing 830. 单调栈

zdj
8个月前
#include<iostream>
using namespace std;
const int N = 100010;
int stk[N],tt,n;
int main(){
    cin>>n;
    for(int i = 0; i < n;i ++){
        int x;
        cin>>x;
        while(tt&&stk[tt]>=x)tt--;
        if(!tt)cout<<-1<<' ';
        else cout<<stk[tt]<<" ";
        stk[++tt] = x;

    }

}


活动打卡代码 AcWing 829. 模拟队列

zdj
8个月前
#include<iostream>
using namespace std;
const int N =100010;
int q[N],hh,tt;
void add(int x){
    tt++;
    q[tt] = x;
}
void pop(){
    hh++;
}
int query(){
    return q[hh];
}
bool empty(){
    if(q[hh])return false;
    else return true;
}
int main(){
    int m;
    cin>>m;
    tt = -1;
    hh = 0;
    while(m--){
        string s;
        cin>>s;
        if(s=="pop"){
            pop();
        }else if(s=="push"){
            int x;
            cin>>x;
            add(x);
        }else if(s=="empty"){
            if(empty())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }else if(s=="query"){
            cout<<query()<<endl;
        }

    }


}


活动打卡代码 AcWing 828. 模拟栈

zdj
8个月前
#include<iostream>
using namespace std;
const int N = 100010;
int stack[N],tt;
void init(){
    tt = 0;
}
void push(int x){
    tt++;
    stack[tt] = x;
}
void pop(){
    tt--;
}
int query(){
    return stack[tt];
}
bool empty(){
    if(!tt)return true;
    else return false;
}
int main(){
    int m;
    cin>>m;
    init();
    while(m--){
        string s;
        cin>>s;
        if(s=="push"){
            int x;
            cin>>x;
            push(x);
        }else if(s=="query"){
            cout<<query()<<endl;
        }else if(s=="pop") pop();
        else if(s=="empty"){
            if(empty())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
}