Here my solution: form the upper side and reuse it.
Assuming string concatenation has linear execution time, this solution should be O(middle*num). Please correct me if I'm wrong
functiondiamond(num){if(num%2<=0)returnnull;letresult="";constmiddle=Math.floor(num/2)+1;letlines=[]for(leti=0;i<middle;i++){lines[i]="";for(letj=0;j<num;j++){lines[i]+=j>=middle-1-i&&j<=middle-1+i?"*":"";}result+=lines[i]+"\n";}// reuse the trianglefor(leti=lines.length-2;i>=0;i--){result+=lines[i]+"\n";}returnresult;}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here my solution: form the upper side and reuse it.
Assuming string concatenation has linear execution time, this solution should be O(middle*num). Please correct me if I'm wrong