[英]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. 给定一个以升序排列的整数nums数组,请找到给定目标值的开始和结束位置。
Example:Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] 示例:输入:nums = [5,7,7,8,8,10],目标= 8输出:[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 ). System.out.println(index)-我的代码给了我想要的输出。(如果我省略return语句)。
I got an error in last line return index_arr
. 我在最后一行return index_arr
。 Error: Incompatiable types:Required int[] Found Java.lang.Integer. 错误:类型不兼容:必需int []找到Java.lang.Integer。
Then I searching how to convert Integer to int and found .int.Value
use to convert Integer to int. 然后,我搜索了如何将Integer转换为int并发现.int.Value
用于将Integer转换为int。 When I use it in my code I got another error unrecognizable command. 当我在代码中使用它时,出现了另一个无法识别的错误命令。 How could I convert Integer to int[]? 如何将Integer转换为int []?
I would start with a firstIndex
and a lastIndex
initialized to -1
. 我将从firstIndex
和lastIndex
初始化为-1
。 Then iterate the nums
array searching for target
. 然后迭代nums
数组以搜索target
。 When found, if the firstIndex
is -1
initialize both it and the lastIndex
- otherwise update only the lastIndex
. 当找到时,如果firstIndex
为-1
,则初始化它和lastIndex
否则仅更新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. 您尝试返回Integer[]
的数组,但是您的方法返回int[]
,因此出现编译错误。
To fix it you should change the return type of searchRange(...)
to Integer[]
. 要修复它,您应该将searchRange(...)
的返回类型更改为Integer[]
。
return type of searchRange
is int
array, but you are returning Integer
array. searchRange
返回类型是int
数组,但是您正在返回Integer
数组。
Convert
ArrayList<Integer>
toint[]
by using java-8 streams and return 使用java-8流将ArrayList<Integer>
转换为int[]
并返回
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[]? 如何将Integer转换为int []?
You cann't do that. 你不能那样做。 If you want to convert Integer[]
to int[]
, try do it like this in java8: 如果要将Integer[]
转换为int[]
,请尝试在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. 仅针对leetcode 34,尝试使用O(lgn)
的时间复杂度的代码,其中采用了二进制搜索。
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. 嗨,您可以将Integer数组更改为String,然后更改为charArrays,最后更改为int [],您的方法希望将其作为返回值。 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; 包com.stackoverflow;
import java.util.ArrayList; 导入java.util.ArrayList;
public class Solution34 { 公共课程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 如果您使用的是Java 8或更高版本,则可以使用此代码
package com.stackoverflow; 包com.stackoverflow;
import java.util.ArrayList; 导入java.util.ArrayList;
public class Solution34 { 公共课程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);
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.