day06-Java methods and arrays (2) homework

1. Given an integer array nums and a target value target, please find the two integers whose sum is the target value in the array,
and return their array index, assuming that each input will only correspond to one answer
( Because it is two numbers, there are two subscript values, so this method returns an int [], which contains the array index corresponding to the two integers)

2. Given an array, move the elements in the array to the right by k positions, where k is a non-negative number.

Example
Input: [6, 7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
right cyclic shift one: [7 , 1,2,3,4,5,6]
Rotate right to rotate 2 bits: [6,7,1,2,3,4,5]
Rotate right to move 3 steps: [5,6,7,1 , 2,3,4]

answer:

1.

 

 

 

 

1  package com.day006;
 2  
3  import java.util.Arrays;
 4  
5  / * 
6  * 1. Given an integer array nums and a target value target,
 7  * Please find the sum in the array as the target value The two integers,
 8  * and return their array subscripts, assuming that each input will only correspond to one answer
 9     (because it is two numbers, so there are two subscript values, so this method returns an int [],
 10           contains array indices corresponding to two integers)
 11   * / 
12  public  class Demo1 {
 13      public  static  void main (String [] args) {
 14          int [] arr = {1,5,4,6,3 };
 15          int [] a = findAdd (arr, 10 );
16         System.out.println(Arrays.toString(a));
17     }
18     
19     public static int[] findAdd(int[] arr, int target) {
20         int[] printarr = new int[2];
21         for(int i = 0; i < arr.length; i++) {
22             for(int j = i+1; j< arr.length; j++) {
23                 if(arr[j] == target - arr[i]) {
24                     printarr[0] = i;
25                     printarr[1] = j;
26                 }
27             }
28         }
29         return printarr;
30     }
31 }

2.

 

 

 

 

 

 

1  package com.day006;
 2  
3  import java.util.Arrays;
 4  
5  / * 
6  * 2. Given an array, move the elements in the array to the right by k positions, where k is a non-negative number.
7  
8  Example  
 9  Input: [1,2,3,4,5,6,7] and k = 3
 10  Output: [5,6,7,1,2,3,4]
 11  Explanation:
 12  cycles to the right Move 1 bit: [7,1,2,3,4,5,6]
 13  Cycle right to move 2 bits: [6,7,1,2,3,4,5]
 14  Cycle right to move 3 steps: [5,6,7,1,2,3,4]
 15   * / 
16  public  class Demo2 {
 17  
18      public  static  void main (String [] args) {
 19         int [] a = {1,2,3,4,5,6,7 };
 20          // Call the moveCycle method and use the string move to achieve the output 
21          String move = Arrays.toString (moveCycle (a, 3 ));
 22          System.out.println (move);
 23      }
 24      
25      public  static  int [] moveCycle ( int [] arr, int step) {
 26          int len = arr.length; // array length of arr array
 27          // new steparr Array, used to hold the array after moving the position 
28          int [] steparr = new  int [len];
 29          for ( inti = 0; i <len; i ++ ) {
 30              if ((i + step)> (len-1 )) {
 31                  // If the index value of the steparr array (i + step) is greater than the index value of the array arr array,
 32                  // The element loops to the front, and the length of the array of arr is subtracted,
 33                  // The index value of steparr becomes (i + step-len) 
34                  steparr [i + step-len] = arr [i];
 35              }
 36              else {
 37                  // index value of steparr array 
38                  steparr [i + step] = arr [i];
 39              }
 40              
41          }
 42          return steparr;
 43      }
44 
45 }

 

Guess you like

Origin www.cnblogs.com/dust2017/p/12705267.html