讨论/技术交流/交流|大佬,为啥同样的代码提交的结果和写在IDEA里的结果不同/
交流|大佬,为啥同样的代码提交的结果和写在IDEA里的结果不同

image.png
题目是上面这个
IDEA里的代码

import java.util.*;

class AuthenticationManager {
    int a;
    List<Integer> tim=new ArrayList<>();
    List<String> id = new ArrayList<>();
    public AuthenticationManager(int timeToLive) {
        a=timeToLive;
    }

    public void generate(String tokenId, int currentTime) {
        for (int i = 0; i < id.size(); i++) {
            if(tim.get(i)+a<=currentTime)
            {
                tim.remove(i);
                id.remove(i);
                i--;
            }
        }
        id.add(tokenId);
        tim.add(currentTime);
    }

    public void renew(String tokenId, int currentTime) {
        for (int i = 0; i < id.size(); i++) {
            if(tim.get(i)+a<=currentTime)
            {
                tim.remove(i);
                id.remove(i);
                i--;
            }
        }
        for (int i = 0; i < id.size(); i++) {
            if(id.get(i)==tokenId)
            {
                if(tim.get(i)+a>currentTime)
                {
                    tim.set(i,currentTime);
                }
                else
                {
                    tim.remove(i);
                    id.remove(i);
                    i--;
                }
                break;
            }
        }
    }

    public int countUnexpiredTokens(int currentTime) {
        for (int i = 0; i < id.size(); i++) {
            if(tim.get(i)+a<=currentTime)
            {
                tim.remove(i);
                id.remove(i);
                i--;
            }

        }
        int ans=0;
        for (int i = 0; i < id.size(); i++) {
            if(tim.get(i)+a>=currentTime)
            {
                ans=ans+1;
            }
        }
        return ans;
    }
}

public class questionThirteen {
    public static void main(String[] args) {

        AuthenticationManager l=new AuthenticationManager(13);
        l.renew("ajvy",1);
        int d=l.countUnexpiredTokens(3);
        int v=l.countUnexpiredTokens(4);
        System.out.print(d);
        System.out.print(v);
        l.generate("fuzxq",5);
        l.generate("izmry",7);
        l.renew("puv",12);
        l.generate("ybiqb",13);
        l.generate("gm",14);
        int r=l.countUnexpiredTokens(15);
        int q=l.countUnexpiredTokens(18);
        int z=l.countUnexpiredTokens(19);
        System.out.print(r);
        System.out.print(q);
        System.out.print(z);
        l.renew("ybiqb",21);
        int s=l.countUnexpiredTokens(23);
        int t=l.countUnexpiredTokens(25);
        int y=l.countUnexpiredTokens(26);
        System.out.print(s);
        System.out.print(y);
        System.out.print(t);
        l.generate("aqdm",28);
        System.out.println(l.countUnexpiredTokens(29));
        l.renew("puv",30);
    }
}

提交的代码

import java.util.*;
class AuthenticationManager {
    int a;
    List<Integer> tim=new ArrayList<>();
    List<String> id = new ArrayList<>();
    public AuthenticationManager(int timeToLive) {
        a=timeToLive;
    }

    public void generate(String tokenId, int currentTime) {
        for (int i = 0; i < id.size(); i++) {
            if(tim.get(i)+a<=currentTime)
            {
                tim.remove(i);
                id.remove(i);
                i--;
            }
        }
        id.add(tokenId);
        tim.add(currentTime);
    }

    public void renew(String tokenId, int currentTime) {
        for (int i = 0; i < id.size(); i++) {
            if(tim.get(i)+a<=currentTime)
            {
                tim.remove(i);
                id.remove(i);
                i--;
            }
        }
        for (int i = 0; i < id.size(); i++) {
            if(id.get(i)==tokenId)
            {
                if(tim.get(i)+a>currentTime)
                {
                    tim.set(i,currentTime);
                }
                else
                {
                    tim.remove(i);
                    id.remove(i);
                    i--;
                }
                break;
            }
        }
    }

    public int countUnexpiredTokens(int currentTime) {
        for (int i = 0; i < id.size(); i++) {
            if(tim.get(i)+a<=currentTime)
            {
                tim.remove(i);
                id.remove(i);
                i--;
            }

        }
        int ans=0;
        for (int i = 0; i < id.size(); i++) {
            if(tim.get(i)+a>=currentTime)
            {
                ans=ans+1;
            }
        }
        return ans;
    }
}

/**
 * Your AuthenticationManager object will be instantiated and called as such:
 * AuthenticationManager obj = new AuthenticationManager(timeToLive);
 * obj.generate(tokenId,currentTime);
 * obj.renew(tokenId,currentTime);
 * int param_3 = obj.countUnexpiredTokens(currentTime);
 */

代码都一样,IDEA里的测试用例也是按照给的写的,但是
在IDEA里的结果
image.png
提交的结果
image.png
一个是004332222
一个是004332211

共 0 个回复
暂无回复