insert an element at index into array

given an array, and an element to insert, and the position to insert this element,return a new array with the element inserted

1,2,3,4,5,6 -> 1,2,3,15,4,5,6
 1 public static void main(String[] args) {
 2         int[] org = new int[]{1,2,3,4,5,6} ;
 3         int[] res = insert(org, 15, 3);
 4         print(res);
 5     }
 6
 7     private static int[] insert(int[] org, int val, int insertIndex ) {
 8         //note the way to create array: [# of items] = length; the last index = length - 1
 9         int[] res = new int[org.length+1] ;
10         //find the ending index: anything <= remain the same
11         int pos = Math.min(insertIndex, org.length) ; //3
12         //[1,2,3] remain the same
13         for (int i = 0; i < pos ; i++) {
14             res[i] = org[i] ;
15         }
16         //create the new item
17         res[pos] = val ;
18         //the rest remain the same
19         for (int indexOld = pos; indexOld < org.length; indexOld++) {
20             res[indexOld+1] = org[indexOld] ;
21         }
22         /* the following is wrong: will jump one item
23         for (int i = pos+1; i < org.length ; i++) {
24             res[i] = org[i];
25         }
26         * */
27         return res ;
28     }
29     private static void print(int[] arr){
30         for (int i = 0; i <arr.length ; i++) {
31             System.out.println(arr[i]);
32         }
33     }

原文地址:https://www.cnblogs.com/davidnyc/p/8481739.html

时间: 2024-11-09 02:08:34

insert an element at index into array的相关文章

961. N-Repeated Element in Size 2N Array

961. N-Repeated Element in Size 2N Array 题目概述: 在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次. 返回重复了 N 次的那个元素. 解法一 class Solution { public int repeatedNTimes(int[] A) { int index = 0; Set<Integer> set = new LinkedHashSet<>(); for (int i = 0; i <

[Swift Weekly Contest 116]LeetCode961. 重复 N 次的元素 | N-Repeated Element in Size 2N Array

In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times. Example 1: Input: [1,2,3,3] Output: 3 Example 2: Input: [2,1,2,5,3,2] Output: 2 Example 3: Input: [5,1

[Leetcode]961. N-Repeated Element in Size 2N Array

Easy In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times. Example 1: Input: [1,2,3,3] Output: 3 Example 2: Input: [2,1,2,5,3,2] Output: 2 Example 3: Input:

LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array

原题链接在这里:https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/ 题目: Given an array nums sorted in non-decreasing order, and a number target, return True if and only if target is a majority element. A majority element is

540. Single Element in a Sorted Array(LeetCode)

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,

Single Element in a Sorted Array

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,

540. Single Element in a Sorted Array

问题描述: Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,

【leetcode】961. N-Repeated Element in Size 2N Array

题目如下: In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times. Example 1: Input: [1,2,3,3] Output: 3 Example 2: Input: [2,1,2,5,3,2] Output: 2 Example 3: Input

LeetCode.540.Single Element in a Sorted Array

恩, 沙比提 1 class Solution(object): 2 def singleNonDuplicate(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 xorsum = 0 8 for i in nums: 9 xorsum = xorsum ^ i 10 return xorsum 11