简体   繁体   中英

Integer to int[] conversion in Java

Problem:

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

Example:Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4]

My code:

class Solution34{
public int[] searchRange(int[] nums, int target) {
    ArrayList<Integer> index=new ArrayList<>();
    for(int i=0;i<nums.length;i++){
        if(nums[i]==target){
            index.add(i);
        }
    }
    Integer[] index_arr = new Integer[index.size()];
    index_arr = index.toArray(index_arr);

    System.out.println(index);
  return index_arr;
}
}

System.out.println(index) - My code gave me desired output.(If I omit return statement ).

I got an error in last line return index_arr . Error: Incompatiable types:Required int[] Found Java.lang.Integer.

Then I searching how to convert Integer to int and found .int.Value use to convert Integer to int. When I use it in my code I got another error unrecognizable command. How could I convert Integer to int[]?

I would start with a firstIndex and a lastIndex initialized to -1 . Then iterate the nums array searching for target . When found, if the firstIndex is -1 initialize both it and the lastIndex - otherwise update only the lastIndex . Return a new array at the end. Like,

public static int[] searchRange(int[] nums, int target) {
    int firstIndex = -1, lastIndex = -1;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == target) {
            if (firstIndex == -1) {
                lastIndex = firstIndex = i;
            } else {
                lastIndex = i;
            }
        }
    }
    if (firstIndex == -1) {
        return new int[] { -1 };
    }
    return new int[] { firstIndex, lastIndex };
}

You are trying to return an array of Integer[] but your method returns int[] , hence the compilation error.

To fix it you should change the return type of searchRange(...) to Integer[] .

return type of searchRange is int array, but you are returning Integer array.

Convert ArrayList<Integer> to int[] by using java-8 streams and return

 public int[] searchRange(int[] nums, int target) {
        ArrayList<Integer> index=new ArrayList<>();
        for(int i=0;i<nums.length;i++){
            if(nums[i]==target){
                index.add(i);
            }
        }
        System.out.println(index);
      return index.stream().mapToInt(Integer::intValue).toArray();

一种方法是流式处理列表,将Integer转换为int并收集到数组:

return index.stream().mapToInt(Integer::intValue).toArray();

How could I convert Integer to int[]?

You cann't do that. If you want to convert Integer[] to int[] , try do it like this in java8:

int[] intArray = Arrays.stream(index_arr).mapToInt(Integer::intValue).toArray();

Update:

just for the leetcode 34, try this code with a time complexity of O(lgn) , in which the binary search is employed.

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] res=new int[]{Integer.MAX_VALUE,Integer.MIN_VALUE};
        binarySearch(nums,0,nums.length-1,target,res);
        if(res[0]==Integer.MAX_VALUE&&res[1]==Integer.MIN_VALUE)
            return new int[]{-1,-1};
        else
            return res;
    }

    private void binarySearch(int[] nums,int lo,int hi,int t,int[] res){
        if(hi<lo)
            return;
        int mid=lo+(hi-lo)/2;
        if(nums[mid]==t){
            if(mid<res[0])
                res[0]=mid;
            if(res[1]<mid)
                res[1]=mid;
            binarySearch(nums,lo,mid-1,t,res);
            binarySearch(nums,mid+1,hi,t,res);
        }else if(nums[mid]<t){
            binarySearch(nums,mid+1,hi,t,res);
        }else if(t<nums[mid]){
            binarySearch(nums,lo,mid-1,t,res);
        }
    }

}

Hi you can change your Integer array to String and then to charArrays and finally to int[] that your method wants it as return value. Like this :

 public int[] searchRange(int[] nums, int target) {
    ArrayList<Integer> index = new ArrayList<Integer>();
    for (int i = -0; i < nums.length; i++) {
        if (nums[i] == target) {
            index.add(i);
        }
    }
    Integer[] index_arr = new Integer[index.size()];
    index_arr = index.toArray(index_arr);
    String i = index_arr.toString();
    char[] characters = i.toCharArray();
    int[] list = new int[characters.length];

    for (int z = 0; z < characters.length; z++) {
        list[z] = characters[z];
    }

    System.out.println(index);
    return list;
}

If you are using old version of you can do like this

package com.stackoverflow;

import java.util.ArrayList;

public class Solution34 {

public int[] searchRange(int[] nums, int target) {
    ArrayList<Integer> index = new ArrayList<>();
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == target) {
            index.add(i);
        }
    }
    Integer[] index_arr = new Integer[index.size()];
    index_arr = index.toArray(index_arr);

    System.out.println(index);

    int[] int_index_arr = new int[index_arr.length];

    for (int i = 0; i < index_arr.length; i++) {
        int_index_arr[i] = index_arr[i];
    }

    return int_index_arr;
}

public static void main(String[] args) {
    int[] nums = { 5, 7, 7, 8, 8, 10 };
    int target = 8;

    Solution34 s34 = new Solution34();
    int[] result = s34.searchRange(nums, target);
    for (int i : result) {
        System.out.println(i);
    }
}

}

if you are using java 8 or above,you can use this code

package com.stackoverflow;

import java.util.ArrayList;

public class Solution34 {

public int[] searchRange(int[] nums, int target) {
    ArrayList<Integer> index = new ArrayList<>();
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == target) {
            index.add(i);
        }
    }

    return index.stream().mapToInt(x -> x).toArray();
}

public static void main(String[] args) {
    int[] nums = { 5, 7, 7, 8, 8, 10 };
    int target = 8;

    Solution34 s34 = new Solution34();
    int[] result = s34.searchRange(nums, target);
    for (int i : result) {
        System.out.println(i);
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM