-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_01.java
More file actions
88 lines (82 loc) · 2.16 KB
/
Copy pathDay_01.java
File metadata and controls
88 lines (82 loc) · 2.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Two Sum
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> mpp = new HashMap<>();
for(int i=0;i<nums.length;i++){
int leftsum = target - nums[i];
if(mpp.containsKey(leftsum)) return new int[]{mpp.get(leftsum),i};
mpp.put(nums[i],i);
}
return new int[]{-1,-1};
}
}
// Sort Colors --> Using Dutch National Flag Algorithm
class Solution {
public void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public void sortColors(int[] nums) {
int n = nums.length;
int low = 0, mid = 0, high = n - 1;
while(mid<=high){
if(nums[mid] == 0){
swap(nums,low,mid);
low++;
mid++;
}
else if(nums[mid] == 1){
mid++;
}
else{
swap(nums,mid,high);
high--;
}
}
}
}
// Pascal's Triangle
class Solution {
public List<Integer> rowgenerate(int n){
List<Integer> list = new ArrayList<>();
int ans = 1;
list.add(ans);
for(int i=0;i<n;i++){
ans = (ans*(n-i))/(i+1);
list.add(ans);
}
return list;
}
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ans = new ArrayList<>();
for(int i=0;i<numRows;i++){
ans.add(rowgenerate(i));
}
return ans;
}
}
// Set Matrix Zeroes
class Solution {
public void setZeroes(int[][] matrix) {
int n = matrix.length;
int m = matrix[0].length;
boolean[] row = new boolean[n];
boolean[] col = new boolean[m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(matrix[i][j] == 0){
row[i] = true;
col[j] = true;
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(row[i] || col[j]){
matrix[i][j] = 0;
}
}
}
}
}