-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay16.java
More file actions
45 lines (45 loc) · 1.16 KB
/
Copy pathDay16.java
File metadata and controls
45 lines (45 loc) · 1.16 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
bonus problem
// problem 1:https://leetcode.com/problems/rotate-array/description/
class Solution {
public void rotate(int[] nums, int k) {
int numsSize=nums.length,i;
int arr[]=new int[numsSize];
while(numsSize<k){
k-=numsSize;
}
if(numsSize>k){
for(i=0;i<numsSize-k;i++){
arr[k+i]=nums[i];
}
for(i=0;i<k;i++){
arr[i]=nums[numsSize-k+i];
}
for(i=0;i<numsSize;i++){
nums[i]=arr[i];
}
}
}
}
sc:O(n)
tc:O(n)
approach: created a new array and store value before k and after k
// problem 2:https://leetcode.com/problems/first-unique-character-in-a-string/description/
class Solution {
public int firstUniqChar(String s) {
int arr[]=new int [26];
int i;
for(i=0;i<s.length();i++){
int x=s.charAt(i)-'a';
arr[x]++;
}
for(i=0;i<s.length();i++){
if(arr[s.charAt(i)-'a']==1){
return i;
}
}
return -1;
}
}
sc:O(n)
tc:O(n)
approach: created an array of size 26 and store the frequency of each character.