Guest User

Untitled

a guest
Jan 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. public static int[] post4(int[] nums) {
  2.  
  3. int startPoint = nums.length - 1;
  4. int[] newArray;
  5. int amtOfElements = 0;
  6. int stopIndex = 0;
  7.  
  8. for (int i = startPoint; i >= 0; i--) {
  9.  
  10. if (nums[i] == 4) {
  11. stopIndex = i;
  12. i = 0;
  13. } else {
  14. amtOfElements++;
  15. }
  16. }
  17.  
  18. newArray = new int[amtOfElements];
  19.  
  20. for (int i = 0, j = stopIndex; i < amtOfElements; i++, j++) {
  21. newArray[i] = nums[j + 1];
  22. }
  23.  
  24. return newArray;
  25. }
  26.  
  27. public int[] post4(int[] arr) {
  28. for(int i = arr.length-1; i >= 0; --i) {
  29. if(arr[i] == 4) {
  30. return Arrays.copyOfRange(arr, i+1, arr.length);
  31. }
  32. }
  33. return new int[0]; // or null
  34. }
  35.  
  36. public static int[] post4(int[] nums) {
  37.  
  38. int marker = 4;
  39. return IntStream.range(0, nums.length) //iterate over the indexes to find the marker
  40. .map(i -> nums.length - i - 1) //reverse the order to start from the back
  41. .filter(i -> marker == nums[i]) //find the marker
  42. .mapToObj(skip -> IntStream.of(nums).skip(skip+1).toArray()) //skip the first elements and the marker (+1)
  43. .findFirst() //the new array
  44. .orElse(nums); //the original array if there is no marker, or int[0] - its not that clear from your question
  45. }
  46.  
  47. public int[] post4(int[] nums) {
  48.  
  49. Stack<Integer> stack = new Stack<>();
  50. int i = nums.length - 1;
  51.  
  52. while (nums[i] != 4) {
  53. stack.add(nums[i--]);
  54. }
  55.  
  56. int[] newArray = new int[stack.size()];
  57. int j = 0;
  58.  
  59. while (!stack.isEmpty()) {
  60. newArray[j++] = stack.pop();
  61. }
  62.  
  63. return newArray;
  64. }
Add Comment
Please, Sign In to add comment