Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions 1.Two-sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int x = nums[i];
if (map.containsKey(target - x)) {
return new int[]{map.get(target - x), i};
}
map.put(x, i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
23 changes: 23 additions & 0 deletions 12.Integer to Roman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public String intToRoman(int num) {
Map<Integer, String> map = new HashMap();
map.put(1, "I"); map.put(5, "V"); map.put(10, "X");
map.put(50, "L"); map.put(100, "C"); map.put(500, "D"); map.put(1000, "M");
map.put(4, "IV"); map.put(9, "IX"); map.put(40, "XL"); map.put(90, "XC");
map.put(400, "CD"); map.put(900, "CM");

int[] sequence = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

StringBuffer sb = new StringBuffer();
for (int i = 0; i<sequence.length; i++) {
int base = sequence[i];

while (num >= base) {
sb.append(map.get(base));
num -= base;
}
}

return sb.toString();
}
}
22 changes: 22 additions & 0 deletions 2.Add-Two-Numbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q= l2, curr = dummyHead;
int carry = 0;
while (p != null || q!= null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int digit = carry + x + y;
carry = digit / 10;
curr.next = new ListNode(digit % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
}
15 changes: 15 additions & 0 deletions 3.Longest_Substring.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Solution {
public int lengthOfLongestSubstring(String s) {
int[] charMap = new int[256];
Arrays.fill(charMap, -1);
int i = 0, maxLen = 0;
for (int j = 0; j < s.length(); j++) {
if (charMap[s.charAt(j)] >= i) {
i = charMap[s.charAt(j)] + 1;
}
charMap[s.charAt(j)] = j;
maxLen = Math.max(j - i + 1, maxLen);
}
return maxLen;
}
}
23 changes: 23 additions & 0 deletions 4.Median_of_two_Sorted_array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int p1 = 0, p2 = 0, pos = 0;
int ls1 = nums1.length, ls2 = nums2.length;
int[] all_nums = new int[ls1+ls2];
double median = 0.0;
while (p1 < ls1 && p2 < ls2){
if (nums1[p1] <= nums2[p2])
all_nums[pos++] = nums1[p1++];
else
all_nums[pos++] = nums2[p2++];
}
while (p1 < ls1)
all_nums[pos++] = nums1[p1++];
while (p2 < ls2)
all_nums[pos++] = nums2[p2++];
if ((ls1 + ls2) % 2 == 1)
median = all_nums[(ls1 + ls2) / 2];
else
median = (all_nums[(ls1 + ls2) / 2] + all_nums[(ls1 + ls2) / 2 - 1]) / 2.0;
return median;
}
}
29 changes: 29 additions & 0 deletions 5.Longest_Palindrome_Substring.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Solution {
public String longestPalindrome(String s) {
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
// aba
int len1 = expandAroundCenter(s, i, i);
// bb
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}

private int expandAroundCenter(String s, int left, int right) {
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
L--;
R++;
}
return R - L - 1;
}




13 changes: 13 additions & 0 deletions 7.Reverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int reverse(int x) {
if (x == 0) return 0;
long res = 0;
while (x != 0) {
res = res * 10 + x % 10;
if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE)
return 0;
x /= 10;
}
return (int) res;
}
}
24 changes: 24 additions & 0 deletions 8.String_to_Integer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {
private static final int maxDiv10 = Integer.MAX_VALUE / 10;
public int myAtoi(String str) {
int i = 0, n = str.length();
while (i < n && Character.isWhitespace(str.charAt(i)))
i++;
int sign = 1;
if (i < n && str.charAt(i) == '+')
i++;
else if (i < n && str.charAt(i) == '-') {
sign = -1;
i++;
}
int num = 0;
while (i < n && Character.isDigit(str.charAt(i))) {
int digit = Character.getNumericValue(str.charAt(i));
if (num > maxDiv10 || num == maxDiv10 && digit >= 8)
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
num = num * 10 + digit;
i++;
}
return sign * num;
}
}
66 changes: 66 additions & 0 deletions 9.Palindrome-Number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class Solution {
public boolean isPalindrome(int x) {
if (x < 0)
return false;
int temp = x;
int len = 0;
while (temp != 0) {
temp /= 10;
len ++;
}
temp = x;
int left, right;
for (int i = 0; i < len / 2; i++) {
right = temp % 10;
left = temp / (int) Math.pow(10, len - 2 * i - 1);
left = left % 10;
if (left != right)
return false;
temp /= 10;
}
return true;
}



public boolean isPalindrome(int x) {
if (x < 0) return false;
int div = 1;
while ( x / div >= 10) {
div *= 10;
}
while (x !=0) {
int l = x / div;
int r = x % 10;
if (l != r) return false;
// Remove left and right number
x = (x % div) / 10;
div /= 100;
}
return true;
}
}





class Solution {
public boolean isPalindrome(int x) {
int r,s=0,number=x;
if(number<0){
return false;
}
while (number!=0){
r=number%10;
s= s*10 +r;
number/=10;
}
if (s==x){
return true;
}
else {
return false;
}
}
}