好久没有参加比赛了,过年以及疫情结束,让我的编程活动大幅减少。这周末为了测试codegeex决定参加一次周赛。
目标还是签到题,这次提交了,结果不正确。比上次有进步。但是对于测试codegeex的目标来说,结果不太满意。签到题目在这里 https://leetcode.cn/problems/find-the-array-concatenation-value/
一开始让codegeex写代码,发现连我都看不下去啦,不是很沾边。于是就试了一下chatGPT,对话式的提示会好一点,但是效率也不高,纠正语法错误是可以的。
不过对于我这种不记忆语法,编译时各种报错的选手来说,AI小助手的代码片段里的语法提示也是有用的。
下面这段代码是是我觉得逻辑过关的一段代码,但是很遗憾,leetcode中提交错误。关键是错误的结果很诡异,是我理解不了的错误。
class Solution {
public long findTheArrayConcVal(int[] nums) {
if(nums.length == 1)
return nums[0];
ArrayList<Integer> numsList = new ArrayList<>();
for( int i = 0; i< nums.length; i++){
numsList.add(nums[i]);
}
long concatValue = 0;
int l = 0;
while( numsList.size()>=2){
l = (int)Math.floor(Math.log10(Math.abs(numsList.get(numsList.size()-1))))+1;
concatValue += (long)numsList.get(numsList.size()-1)+ numsList.get(0)*10^l;
numsList.remove(numsList.size()-1);
numsList.remove(0);
}
if( !numsList.isEmpty()){
concatValue += (long)numsList.get(0);
numsList.remove(0);
}
return concatValue;
}
}
我特地选了最简单的用例【7,4】,但是我的代码跑出来的结果是【7,5】
这个逻辑上就震惊了我啊!然后我在IDE里借助输出进行排查,结果发现还是有错误,Java里乘方是不可以用^来表示的,需要用Math.pow(a,b)来表示。终于在经历了漫长的2个小时之后,终于找到了正确的答案。
class Solution {
public long findTheArrayConcVal(int[] nums) {
ArrayList<Integer> numsList = new ArrayList<>();
for( int i = 0; i< nums.length; i++){
numsList.add(nums[i]);
}
long concatValue = 0;
int l = 0;
int i = 0;
int j = 0;
while( numsList.size()>=2){
l = (int)Math.floor(Math.log10(Math.abs(numsList.get(numsList.size()-1))))+1;
i = numsList.get(0)*(int)Math.pow(10,l);
j = numsList.get(numsList.size()-1);
concatValue += (long) i + j;
numsList.remove(numsList.size()-1);
numsList.remove(0);
}
if( numsList.size() != 0){
concatValue += (long)numsList.get(0);
numsList.remove(0);
}
return concatValue;
}
}
结果还不错,5ms,40.9MB。
看来对于不经常写代码的人,语法就是一个巨大的瓶颈,而AI助手目前看来还是不能很好的解决这个问题。至少对我来说,它们还是不够智能。还是要自己多加练习才行,靠AI是靠不住的。