21BCS7586 PriyanshuMalik Day 3
21BCS7586 PriyanshuMalik Day 3
Day – 3 Java
Aim:
1) Metll Test Authenticate 1
2) Metll Test Authenticate 2
3) Remove Duplicates from Sorted Array
4) Compare Version Numbers
5) Add Binary
6) Zigzag Conversion
7) Permutations
8) Maximum Product Subarray e
number.toCharArray()) {
if (Character.isDigit(ch)) { uniqueDigits.add(ch);
}
}
return uniqueDigits.size();
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
2) public static int countDigitsAppearingOnce(String number)
{
HashMap<Character, Integer> digitFrequency = new HashMap<>();
3) class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int j = 1;
for(int i = 1; i < nums.size(); i++){
if(nums[i] != nums[i - 1]){
nums[j] = nums[i];
j++;
}
} return
j;
}
};
4) class Solution {
public:
int compareVersion(string version1, string version2) {
int i = 0, j = 0;
int len1 = version1.length(), len2 = version2.length();
int num1 = 0, num2 = 0;
while (i < len1 or j < len2) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
while (i < len1 and version1.at(i) != '.') num1 =
num1 * 10 + (version1.at(i++) - '0');
while (j < len2 and version2.at(j) != '.')
num2 = num2 * 10 + (version2.at(j++) - '0');
reverse(begin(ans), end(ans));
return ans;
} };
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6) class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1) return s;
vector<string> rows(min(numRows,
int(s.size())));
int curRow=0; bool
goingDown=false;
for(char c: s){
rows[curRow] += c; if(curRow==0 ||
curRow == numRows-1)
goingDown =! goingDown; curRow
+= goingDown?1: -1;
} string
ret;
for(string row: rows) ret += row;
return ret;
}
};
7)
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums.length == 1) {
List<Integer> singleList = new
ArrayList<>(); singleList.add(nums[0]);
res.add(singleList);
return res;
}
res.addAll(perms);
}
return res;
}
}
8)
class Solution {
public int maxProduct(int[] nums) {
if (nums.length == 0) {
return 0;
}
maxSoFar = tempMaxSoFar;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
result = Math.max(maxSoFar, result);
}
return result;
}
}
Output
1.
2.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3.
4.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5.
6.
7.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
8.