Skip to content

Commit aa8fe3d

Browse files
author
Prashant Jain
committed
Adding math
1 parent 127d3a4 commit aa8fe3d

3 files changed

Lines changed: 105 additions & 37 deletions

File tree

src/in/knowledgegate/dsa/linkedlist/PascalTriangle.java

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
import java.util.List;
55

66
/**
7-
* Given an integer numRows, return the first numRows of Pascal's triangle. In
8-
* Pascal's triangle, each number is the sum of the two numbers directly above
9-
* it as shown.
10-
* <p>
7+
* Given an integer numRows, return the first
8+
* numRows of Pascal's triangle. In Pascal's
9+
* triangle, each number is the sum of the two
10+
* numbers directly above it as shown.
11+
*
12+
* 1
13+
* 1 1
14+
* 1 2 1
15+
* 1 3 3 1
16+
* 1 4 6 4 1
17+
* 1 5 10 10 5 1
18+
*
1119
* Example 1: Input: numRows = 5 Output:
1220
* [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
1321
* <p>
@@ -17,22 +25,23 @@
1725
* Constraints: 1 <= numRows <= 30
1826
*/
1927
public class PascalTriangle {
20-
public List<List<Integer>> generate(int numRows) {
21-
List<List<Integer>> result = new ArrayList<>();
22-
List<Integer> prev = new ArrayList<>();
23-
prev.add(1);
24-
result.add(prev);
28+
public List<List<Integer>> generate(int numRows) {
29+
List<List<Integer>> result = new ArrayList<>();
30+
List<Integer> prev = new ArrayList<>();
31+
prev.add(1);
32+
result.add(prev);
2533

26-
for (int i = 1; i < numRows; i++) {
27-
List<Integer> curr = new ArrayList<>();
28-
curr.add(1);
29-
for (int j = 1; j < prev.size(); j++) {
30-
curr.add(prev.get(j) + prev.get(j - 1));
31-
}
32-
curr.add(1);
33-
result.add(curr);
34-
prev = curr;
35-
}
36-
return result;
34+
for (int i = 1; i < numRows; i++) {
35+
List<Integer> curr = new ArrayList<>();
36+
curr.add(1);
37+
for (int j = 1; j < prev.size(); j++) {
38+
int val = prev.get(j) + prev.get(j - 1);
39+
curr.add(val);
40+
}
41+
curr.add(1);
42+
result.add(curr);
43+
prev = curr;
3744
}
45+
return result;
46+
}
3847
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package in.knowledgegate.dsa.math;
2+
3+
/**
4+
* Given an integer x, return true if x is
5+
* palindrome integer.
6+
*
7+
* An integer is a palindrome when it reads
8+
* the same backward as forward.
9+
*
10+
* For example, 121 is a palindrome while 123
11+
* is not.
12+
*
13+
*
14+
* Example 1:
15+
* Input: x = 121
16+
* Output: true
17+
* Explanation: 121 reads as 121 from left to
18+
* right and from right to left.
19+
*
20+
* Example 2:
21+
* Input: x = -121
22+
* Output: false
23+
* Explanation: From left to right, it reads
24+
* -121. From right to left, it becomes 121-.
25+
* Therefore it is not a palindrome.
26+
*
27+
* Example 3:
28+
* Input: x = 10
29+
* Output: false
30+
* Explanation: Reads 01 from right to left.
31+
* Therefore it is not a palindrome.
32+
*
33+
*
34+
* Constraints:
35+
* -231 <= x <= 231 - 1
36+
*/
37+
public class PalindromeNumber {
38+
public boolean isPalindrome(int x) {
39+
if (x < 0) return false;
40+
int rev = 0;
41+
int temp = x;
42+
while (temp > 0) {
43+
int rem = temp % 10;
44+
rev = rev * 10 + rem;
45+
temp = temp / 10;
46+
}
47+
return x == rev;
48+
}
49+
}
Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
11
package in.knowledgegate.dsa.strings;
22

33
/**
4-
* Write a function to find the longest common prefix string amongst an array of
5-
* strings. If there is no common prefix, return an empty string "".
4+
* Write a function to find the longest common
5+
* prefix string amongst an array of strings. If
6+
* there is no common prefix, return an empty
7+
* string "".
68
* <p>
7-
* Example 1: Input: strs = ["flower","flow","flight"] Output: "fl"
9+
* Example 1: Input: strs =
10+
* ["flower","flow","flight"] Output: "fl"
811
* <p>
9-
* Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation:
10-
* There is no common prefix among the input strings.
12+
* Example 2: Input: strs =
13+
* ["dogasdfasdf","racecar","car"] Output: ""
14+
* Explanation:
15+
* There is no common prefix among the input
16+
* strings.
1117
* <p>
1218
* <p>
13-
* Constraints: 1 <= strs.length <= 200 0 <= strs[i].length <= 200 strs[i]
14-
* consists of only lower-case English letters.
19+
* Constraints: 1 <= strs.length <= 200 0 <=
20+
* strs[i].length <= 200 strs[i] consists of only
21+
* lower-case English letters.
1522
*/
1623
public class LongestCommonPrefix {
17-
public String longestCommonPrefix(String[] strs) {
18-
String result = "";
19-
for (int i = 0; i < strs[0].length(); i++) {
20-
for (int j = 1; j < strs.length; j++) {
21-
if (strs[j].length() <= i || strs[0].charAt(i) != strs[j].charAt(i)) {
22-
return result;
23-
}
24-
}
25-
result += strs[0].charAt(i);
24+
public String longestCommonPrefix(String[] strs) {
25+
String result = "";
26+
for (int i = 0; i < strs[0].length(); i++) {
27+
char finding = strs[0].charAt(i);
28+
for (int j = 1; j < strs.length; j++) {
29+
if (strs[j].length() <= i ||
30+
finding != strs[j].charAt(i)) {
31+
return result;
2632
}
27-
return result;
33+
}
34+
result += finding;
2835
}
36+
37+
return result;
38+
}
2939
}

0 commit comments

Comments
 (0)