-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.java
More file actions
50 lines (42 loc) · 1.13 KB
/
day8.java
File metadata and controls
50 lines (42 loc) · 1.13 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
// ques1: 283 . Move zeroes
//link: https://leetcode.com/problems/move-zeroes/description/
class Solution {
public void moveZeroes(int[] nums) {
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
int j=i+1;
while(j<(nums.length-1) && nums[j]==0 )
j++;
if(j<nums.length){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
}
return ;
}
}
// ques2: 189.Rotate array
//link: https://leetcode.com/problems/rotate-array/description/
class Solution {
public void rotate(int[] nums, int k) {
int l=0;
int i;
while(nums.length<k){
k-=nums.length;
}
int[] arr=new int[nums.length+k];
for(i=0;i<nums.length;i++){
arr[i+k]=nums[i];
}
for(i=i;i<arr.length;i++){
arr[l++]=arr[i];
}
for(int j=0;j<nums.length;j++){
nums[j]=arr[j];
}
}
}
// TC: O(n)
//SC: O(n)