Skip to content

Instantly share code, notes, and snippets.

@AnjaliManhas
AnjaliManhas / Calculator.java
Last active April 10, 2020 06:34
Class Calculator calculates basic operations, namely, addition, subtraction, multiplication and division of two numbers.
package com.company;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Calculator {
public static void main(String[] args) {
@AnjaliManhas
AnjaliManhas / Application.java
Last active April 10, 2020 19:23
User Management in Java- This program enters users details (name, address, city, date of birth, phone number, email Id). It then maps phone numbers to names with the help of HashMap. Finally, it tells the name of the user which is searched by entering the phone number.
package com.company;
import java.util.HashMap;
import java.util.Map;
import java.util.HashMap;
public class Application {
static Map<String, User> userStorage = new HashMap<>();
public static void main(String[] args) {
System.out.println("****************Diamond Store************");
@AnjaliManhas
AnjaliManhas / Application.java
Created April 11, 2020 10:55
Product Management in Java- This program enters users and products details. It then maps users' phone numbers to their names and products' ids to the other product details ( product name, product price, product color) with the help of HashMap. Finally, it tells the name of the user and product details when searched with the help of phone number …
package com.company;
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Application {
@AnjaliManhas
AnjaliManhas / gist:ebd50a6fe15b2cbbb7a1a1603952fbc9
Created April 26, 2020 08:33
Two Sum Problem- LeetCode- Given an array of integers of the two numbers such that they add up to a specific target. You may assume that each input would have one solution, and you may not use the same element twice.
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> arr;
for (int i=0; i < nums.size(); i++){
int rem = target - nums[i];
for (int j=0; j < nums.size(); j++){
if (rem == nums[j] && j!=i){
arr.push_back(i);
arr.push_back(j);
@AnjaliManhas
AnjaliManhas / TwoSumJava.txt
Created April 26, 2020 08:36
Two Sum LeetCode- Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice.
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i]) && map.get(target - nums[i]) != i) {
@AnjaliManhas
AnjaliManhas / ReverseBinaryTreeTraversal.java
Last active April 28, 2020 14:05
Binary Tree Reverse Level Order Traversal- LeetCode: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
@AnjaliManhas
AnjaliManhas / ReverseBinaryTreeTraversal2.java
Last active April 28, 2020 14:05
Binary Tree Reverse Level Order Traversal- LeetCode: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
@AnjaliManhas
AnjaliManhas / removeElement.java
Created May 6, 2020 12:55
Remove Element- LeetCode: Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new l…
class Solution {
public int removeElement(int[] nums, int val) {
int i, count= 0;
for(i=0; i< nums.length; i++){
if(nums[i] != val){
nums[count] = nums[i];
count++;
}
}
return count;
@AnjaliManhas
AnjaliManhas / removeDuplicated.java
Created May 6, 2020 13:13
Remove Duplicates from Sorted Array LeetCode- Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
class Solution {
public int removeDuplicates(int[] nums) {
int i=0;
int j=1;
int temp=1;
while(j<nums.length)
{
if(nums[i]==nums[j])
{
j++;
@AnjaliManhas
AnjaliManhas / longestPrefix.java
Created May 6, 2020 13:41
Longest Common Prefix Leetcode- Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "".
public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length==0||strs==null) return "";
String prefix = strs[0];
for(int i = 1;i<strs.length;i++){
if(strs[i] == prefix) continue;
int k = 0;
while(k<prefix.length()&&k<strs[i].length()&&prefix.charAt(k)==strs[i].charAt(k)){
k++;
}