Problem:
Return an array that is "left shifted" by one -- so {6, 2, 5, 3} returns {2, 5, 3, 6}. You may modify and return the given array, or return a new array.
shiftLeft({6, 2, 5, 3}) → {2, 5, 3, 6}
shiftLeft({1, 2}) → {2, 1}
shiftLeft({1}) → {1}
Solution:
public int[] shiftLeft(int[] nums) { int[] foo; foo = new int[nums.length]; if (nums.length == 0) return foo; for (int i = 0; i < nums.length-1; i++) { if (i > 0) foo[i] = nums[i+1]; } if (nums.length > 1) foo[0] = nums[1]; foo[nums.length-1] = nums[0]; return foo; }
public int[] shiftLeft(int[] nums) {
ReplyDeleteint len = nums.length;
int temp = 0;
for(int i=0; i<len-1; i++){
temp = nums[i];
nums[i]= nums[i+1];
nums[i+1] = temp;
}
return nums;
}
Swamp more simplier than other codes!
Deletepublic int[] shiftLeft(int[] nums) {
ReplyDeleteint[] newArray;
int lastNum;
if(nums.length == 0) {
return nums;
} else {
newArray = nums;
lastNum = nums[0];
for(int i = 0; i < nums.length - 1; i++) {
newArray[i] = nums[i + 1];
}
newArray[newArray.length -1] = lastNum;
}
return newArray;
}
public int[] shiftLeft(int[] nums) {
ReplyDeleteif (nums.length <= 0) {
return nums;
}
int[] array = new int[nums.length];
int first = nums[0];
for (int i = 0, j = 1; j < nums.length; i++, j++) {
array[i] = nums[j];
}
array[array.length - 1] = first;
return array;
public int[] shiftLeft(int[] nums) {
ReplyDeleteif(nums.length <= 1) return nums;
int tmp = nums[0];
for(int i = 0; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
nums[nums.length - 1] = tmp;
return nums;
}
public int[] shiftLeft(int[] nums)
ReplyDelete{
int i,j;
//int k=nums[0];
int l=nums.length;
int a[]=new int[l];
if(l==0)
{
return a;
}
int k=nums[0];
for(i=0;i<1;i++)
{
for(j=0;j<l-1;j++)
{
a[j]= nums[j+1];
}
a[j]=k;
}
return a;
}
public int[] shiftLeft(int[] nums) {
ReplyDeleteint temp=0;
if(nums.length<1)
return nums;
temp=nums[0];
for(int i=0;i<nums.length-1;i++){
nums[i]=nums[i+1];
}
nums[nums.length-1]=temp;
return nums;
}
public int[] shiftLeft(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length - 1; i++)
{
int temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
}
return nums;
}
public int[] shiftLeft(int[] nums) {
ReplyDeleteif(nums.length<=1){
return nums;
}
int[] newNums = new int[nums.length];
newNums[nums.length-1]=nums[0];
for(int i=0; i<nums.length-1; i++){
newNums[i] = nums[i+1];
}
return newNums;
}
public int[] shiftLeft(int[] nums) {
ReplyDeleteif(nums.length > 0) {
int temp=nums[0];
for(int i=0; i < nums.length-1; i++){
if(nums[i] != nums[nums.length-1]){
nums[i]=nums[i+1];
}
}
nums[nums.length-1] = temp;
}
return nums;
}
With Java Stream and Collections
ReplyDeletepublic int[] shiftLeft(int[] nums) {
List numbers = Arrays.stream(nums).boxed().collect(Collectors.toList());
Collections.rotate(numbers, -1);
return numbers.stream().mapToInt(i->i).toArray();
}
public int[] shiftLeft(int[] nums) {
ReplyDeleteif(nums.length==0)
return nums;
int[] t=new int[nums.length];
t[nums.length-1]=nums[0];
for(int i=0;i<nums.length-1;i++){
t[i]=nums[i+1];
}
return t;
}