Skip to content

Commit efb5249

Browse files
committed
first commit
0 parents  commit efb5249

File tree

67 files changed

+2105
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2105
-0
lines changed

.idea/leet code.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Easy/1. Two Sum.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
3+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
4+
You can return the answer in any order.
5+
6+
*/
7+
8+
/*
9+
https://leetcode.com/problems/two-sum/discuss/2388388/100-fastest-solution-java-script-solution-0n
10+
11+
*/
12+
13+
/* TIME COMPLEXITY IS O(logn) */
14+
15+
/*
16+
17+
Example 1:
18+
19+
Input: nums = [2,7,11,15], target = 9
20+
Output: [0,1]
21+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
22+
Example 2:
23+
24+
Input: nums = [3,2,4], target = 6
25+
Output: [1,2]
26+
Example 3:
27+
28+
Input: nums = [3,3], target = 6
29+
Output: [0,1]
30+
*/
31+
32+
/**
33+
* @param {number[]} nums
34+
* @param {number} target
35+
* @return {number[]}
36+
*/
37+
var twoSum = function (nums, target) {
38+
var s = 0; //initilize a pointer to 0
39+
for (let i = 1; i < nums.length; i++) { //itterate 1 to length
40+
if ((nums[s] + nums[i]) == target) { //check the pointer is equal to the target number or not
41+
return [s, i]; //return if both sum are equal to the target
42+
}
43+
if (i == nums.length - 1) { //if i equal to the length-1 then reinitilize it's to s+1
44+
i = s + 1;
45+
s++;
46+
}
47+
}
48+
}
49+
var nums = [3, 2, 4], target = 6;
50+
console.log(twoSum(nums, target));

Easy/125. Valid Palindrome.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
125. Valid Palindrome
3+
https://leetcode.com/problems/valid-palindrome/
4+
*/
5+
6+
/* TIME COMPLEXITY O(N) */
7+
8+
/*
9+
Example 1:
10+
11+
Input: s = "A man, a plan, a canal: Panama"
12+
Output: true
13+
Explanation: "amanaplanacanalpanama" is a palindrome.
14+
Example 2:
15+
16+
Input: s = "race a car"
17+
Output: false
18+
Explanation: "raceacar" is not a palindrome.
19+
*/
20+
21+
/**
22+
* @param {string} s
23+
* @return {boolean}
24+
*/
25+
26+
//Create a function to check that the whole string is palindrom or not
27+
//This function is the main function to check our string is valid palindrom or not
28+
function check_palindrom(s) {
29+
let pointer = s.length - 1; // Make a pointer which is start form length
30+
for (let i = 0; i < s.length / 2; i++, pointer--) {
31+
if (s[i] != s[pointer]) { // Check is the Ith possition value and pointer possition value is equal or not
32+
return false; // return false if any of character is not equal we don't require to itterate whole string
33+
}
34+
}
35+
return true; // Return true if all condition is satisfy
36+
}
37+
38+
// Method 1
39+
// This Below method is basically used for non-regex method.
40+
41+
var isPalindrome = function (s) {
42+
// Make a array which is used to store all non-alphanumeric characters.
43+
s = s.toLowerCase(); // Convert whole string to lower case.
44+
var ansArray = [];
45+
for (let i = 0; i < s.length; i++) {
46+
// The bellow condition is used to check that the number should be a-z or 0-9
47+
if ((s.charCodeAt(i) >= 97 && s.charCodeAt(i) <= 122) || (s.charCodeAt(i) >= 48 && (s.charCodeAt(i) <= 57))) {
48+
ansArray.push(s[i]) // If the condition is true then add this value to the array
49+
}
50+
}
51+
// Here The join function is used to convert array to string.
52+
return check_palindrom(ansArray.join(""));
53+
};
54+
55+
56+
// Method 2
57+
// Below method is basically used for use regex
58+
59+
var isPalindrome = function (s) {
60+
// the below replace method is basically used to remove all non-alphanumeric character
61+
// And the lower function is used for convert whole string to lower case
62+
s = s.replace(/[^A-Za-z0-9]/g, '').toLowerCase();
63+
// Convert whole string to lower case.
64+
// Here The join function is used to convert array to string.
65+
return check_palindrom(s);
66+
};
67+
68+
console.log(isPalindrome("0P"));
69+
console.log(isPalindrome("A man, a plan, a canal: Panama"));
70+
// char Code
71+
// a = 97
72+
// z = 122
73+
// A = 65
74+
// Z = 90
75+
// 0 = 48
76+
// 9 = 57

Easy/13. Roman to Integer.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* 13. Roman to Integer
2+
https://leetcode.com/problems/roman-to-integer/
3+
*/
4+
5+
/* TIME COMPLEXITY IS O(N) */
6+
7+
/*
8+
Example 1:
9+
10+
Input: s = "III"
11+
Output: 3
12+
Explanation: III = 3.
13+
Example 2:
14+
15+
Input: s = "LVIII"
16+
Output: 58
17+
Explanation: L = 50, V= 5, III = 3.
18+
Example 3:
19+
20+
Input: s = "MCMXCIV"
21+
Output: 1994
22+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
23+
*/
24+
25+
var romanToInt = function (s) {
26+
// Create A Map
27+
const map = {
28+
'I': 1,
29+
'V': 5,
30+
'X': 10,
31+
'L': 50,
32+
'C': 100,
33+
'D': 500,
34+
'M': 1000
35+
}
36+
let ans = 0;
37+
for (let i = 0; i < s.length; i++) {
38+
const cur = map[s[i]], next = map[s[i + 1]];
39+
//check that the current indext value is smaller than the next index value or not if the current value smaller than next value return current value as minus(-) value.
40+
ans += cur < next ? -cur : cur;
41+
}
42+
return ans;
43+
};
44+
romanToInt("MCMXCIV");
45+
//Input :MCMXCIV
46+
//Output:1994

Easy/14. Longest Common Prefix.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
14. Longest Common Prefix
3+
4+
https://leetcode.com/problems/longest-common-prefix/
5+
6+
https://leetcode.com/problems/longest-common-prefix/discuss/2396181/easiest-java-script-solution-on2
7+
*/
8+
9+
/* TIME COMPLEXITY IS O(N2) */
10+
11+
/*
12+
Example 1:
13+
14+
Input: strs = ["flower","flow","flight"]
15+
Output: "fl"
16+
Example 2:
17+
18+
Input: strs = ["dog","racecar","car"]
19+
Output: ""
20+
Explanation: There is no common prefix among the input strings.
21+
*/
22+
23+
/**
24+
* @param {string[]} strs
25+
* @return {string}
26+
*/
27+
var longestCommonPrefix = function (strs) {
28+
var firstIndexStr = strs[0], ans = "";
29+
if (strs[0] == "") return ""; //if string contain blank
30+
if (strs.length == 1) return strs[0] //return if string contain only one word
31+
for (let i = 0; i < firstIndexStr.length; i++) { //this loop is used for check the firstWord all character with it's respective to other word character.
32+
var count = 1; //use count veriable to check that all character satisfy with all word character or not.
33+
for (let j = 1; j < strs.length; j++) {
34+
if (strs[j][i] == firstIndexStr[i]) { //here we check string First Word with other character words
35+
count++; //if the character match with other character than increment to +1
36+
}
37+
}
38+
if (count == strs.length) { //check that all word character satisfy with all character or not.
39+
ans += firstIndexStr[i];
40+
} else {
41+
return ans.length > 0 ? ans : ""; //chect that the ans length should be greater than 1 than return ans otherwise return "";
42+
}
43+
}
44+
return ans;
45+
};
46+
console.log(longestCommonPrefix(["flower", "flower", "flower", "flower"]));

Easy/15. 3Sum.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var threeSum = function (nums) {
6+
nums.sort((a, b) => a - b);
7+
8+
let res = [];
9+
for (let i = 0; i < nums.length - 2; i++) {
10+
11+
// skipping the duplicate elements
12+
if (i > 0 && nums[i] == nums[i - 1]) continue;
13+
let j = i + 1;
14+
let k = nums.length - 1;
15+
16+
// Two sum approach
17+
while (j < k) {
18+
let sum = nums[j] + nums[k];
19+
20+
if (sum == -nums[i]) {
21+
res.push([nums[i], nums[j], nums[k]]);
22+
// skipping the duplicate elements
23+
while (nums[j] == nums[j + 1]) j++;
24+
while (nums[k] == nums[k - 1]) k--;
25+
k--;
26+
j++;
27+
}
28+
else if (sum > -nums[i]) {
29+
k--;
30+
}
31+
else {
32+
j++;
33+
}
34+
}
35+
}
36+
return res;
37+
};
38+
39+
console.log(threeSum([-1, 0, 1, 2, -1, -4]));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
1752. Check if Array Is Sorted and Rotated
3+
https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/
4+
5+
*/
6+
7+
/* TIME COMPLEXITY O(N) */
8+
9+
/*
10+
Example 1:
11+
12+
Input: nums = [3,4,5,1,2]
13+
Output: true
14+
Explanation: [1,2,3,4,5] is the original sorted array.
15+
You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].
16+
Example 2:
17+
18+
Input: nums = [2,1,3,4]
19+
Output: false
20+
Explanation: There is no sorted array once rotated that can make nums.ś
21+
22+
*/
23+
24+
/**
25+
* @param {number[]} nums
26+
* @return {boolean}
27+
*/
28+
var check = function (nums) {
29+
let count = 0; // Make a count veriable
30+
for (let i = 1; i < nums.length; i++) {
31+
if (nums[i - 1] > nums[i]) { // Check that if the i-1 index value should be greater then i possition value
32+
count++;
33+
}
34+
}
35+
if (nums[nums.length - 1] > nums[0]) { // this condition is basically used for to check last index value to first index value.
36+
count++;
37+
}
38+
return count <= 1; // return if the count is 0 or 1 then it will true or false.
39+
};
40+
console.log(check([2, 1, 3, 4]));

0 commit comments

Comments
 (0)