Skip to content

Instantly share code, notes, and snippets.

@KimiyukiYamauchi
Created November 25, 2020 01:53
Show Gist options
  • Save KimiyukiYamauchi/e4261b1ce7b49cf6ba04d9f420ba2ae8 to your computer and use it in GitHub Desktop.
Save KimiyukiYamauchi/e4261b1ce7b49cf6ba04d9f420ba2ae8 to your computer and use it in GitHub Desktop.
/**
*
* @param a - 整数型の配列
* @param idx - 削除の開始の添字
* @param n - 削除する要素数
* @return
* 配列aのidxの位置からn個の要素を削除した配列を
* 返す
* 但し、削除できない場合は元の配列を返す
*/
public int [] ex4_14(int [] a, int idx, int n){
int [] ret;
if (n <= 0 || idx < 0) {
ret = new int[a.length];
for (int i = 0; i < a.length; i++) {
ret[i] = a[i];
}
} else {
if (idx + n >= a.length) {
if (n > a.length - idx ) {
ret = new int[a.length - id]
} else {
ret = new int[a.length];
}
} else {
ret = new int[a.length - n];
}
for (int i = 0, j = 0; i < a.length; i++) {
if ( i < idx || i >= idx + n) {
ret[j++] = a[i];
}
}
}
return ret;
}
@s20016
Copy link

s20016 commented Nov 25, 2020

int [] ret = new int[a.length];
int [] ret2 = new int[a.length];
int [] ret3 = new int[0];

for (int i = 0, j = 0; i < a.length; i++) {
	ret[i] = a[i];
if (i < idx || i >= idx + n) {
	ret2[j] = a[i];
	j++;
	}
}

return (n >= a.length) ? ret3 : (n < 0) ? ret : (n + idx < a.length) ? Arrays.copyOfRange(ret2, 0, a.length - n) : ret;

@KimiyukiYamauchi
Copy link
Author

これだと通りますね

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment