-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquaresOfSortedArray.java
More file actions
72 lines (63 loc) · 1.44 KB
/
SquaresOfSortedArray.java
File metadata and controls
72 lines (63 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.lau.leetcode.algorithm_i;
/**
*
* @author lauraPerez
*
* LeetCode problem: Squares of a Sorted Array
*
*
* Status: Accepted
*
*
* Submission Detail:
*
* 137 / 137 test cases passed.
*
* Status: Accepted
*
* Runtime: 528 ms
*
* Memory Usage: 43.6 MB
*
*
* Accepted Solutions Runtime Distribution:
*
* Your runtime beats 5.02 %
* of java submissions.
*
*
* Accepted Solutions Memory Distribution
*
* Your memory usage beats 93.57 %
* of java submissions.
*
*/
public class SquaresOfSortedArray {
public int[] sortedSquares(int[] nums) {
int[] result = new int[nums.length];
int i = nums.length -1;
while(i >= 0) {
int val = nums[i] * nums[i];
result[i] = val;
i--;
}
return sortBubble(result);
}
public int[] sortBubble(int[] numberArray) {
boolean done = false;
while (!done) {
done = true;
int itemsToBeSorted = numberArray.length - 1;
for (int i = 0; i < itemsToBeSorted; i++) {
if (numberArray[i] > numberArray[i + 1]) {
int smaller = numberArray[i + 1];
numberArray[i + 1] = numberArray[i];
numberArray[i] = smaller;
done = false;
}
}
itemsToBeSorted--;
}
return numberArray;
}
}