LeetCode (used to call i has 1337 code) is a social platform for preparing IT technical interviews. We strive to provide you with the best learning experience in preparing interviews for companies in the IT industry.
To be successful in a technical interview, we believe it is mainly repeating these three important steps:
Code. Read. Discuss.
We strive to provide you the LeetCode platform as the ultimate solution for preparing technical interviews.
1. Code -> Code solution using the Online Judge system.
2. Read -> Read high quality article featuring in-depth thought process. Also read other LeetCoders’ code.
3. Discuss -> Discuss your thoughts about the problem with other LeetCoders.
We hope that through our platform, you will grow into a LeetCoder. Not only will you be successful in all of your interviews, and most importantly, you will be a better coder in general !
public class AddBinary {
public String addBinary(String a, String b) {
int i = a.length() - 1;
int j = b.length() - 1;
int da = 0;
int db = 0;
int adv = 0;
StringBuffer result = new StringBuffer();
while (i >= 0 && j >= 0) {
da = a.charAt(i--) == '0' ? 0 : 1;
db = b.charAt(j--) == '0' ? 0 : 1;
int d = da + db + adv;
result.append(d % 2 == 0 ? '0' : '1');
adv = d >> 1;
}
if (i >= 0) {
while (i >= 0) {
da = a.charAt(i--) == '0' ? 0 : 1;
int d = da + adv;
result.append(d % 2 == 0 ? '0' : '1');
adv = d >> 1;
}
} else if (j >= 0) {
while (j >= 0) {
db = b.charAt(j--) == '0' ? 0 : 1;
int d = db + adv;
result.append(d % 2 == 0 ? '0' : '1');
adv = d >> 1;
}
}
if (adv == 1) {
result.append('1');
}
return result.reverse().toString();
}
}
S算法问题: Add Two Numbers 问题描述:
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
public class AddTwoNumbers {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null)
return l2;
if (l2 == null)
return l1;
ListNode head = new ListNode(0);
ListNode cur = head;
int plus = 0;
while (l1 != null && l2 != null) {
int sum = l1.val + l2.val + plus;
plus = sum / 10;
sum = sum % 10;
cur.next = new ListNode(sum);
cur = cur.next;
l1 = l1.next;
l2 = l2.next;
}
if (l1 != null) {
if (plus != 0) {
cur.next = addTwoNumbers(l1, new ListNode(plus));
} else {
cur.next = l1;
}
} else if (l2 != null) {
if (plus != 0) {
cur.next = addTwoNumbers(l2, new ListNode(plus));
} else {
cur.next = l2;
}
} else if (plus != 0) {
cur.next = new ListNode(plus);
}
public class Anagrams {
public ArrayList<String> anagrams(String[] strs) {
ArrayList<String> ret = new ArrayList<String>();
ArrayList<String> ar = new ArrayList<String>();
for (String s : strs) {
char[] c = s.toCharArray();
Arrays.sort(c);
ar.add(new String(c));
}
int[] list = new int[strs.length];
int tmp = 0;
for (int i = 0; i < ar.size(); i++) {
if (list[i] == 0) {
list[i] = 1;
tmp = 1;
for (int j = i + 1; j < ar.size(); j++) {
if (list[j] == 0 && ar.get(i).equals(ar.get(j))) {
list[j] = 1;
tmp++;
}
}
if (tmp == 1) {
list[i] = 0;
}
}
}
for (int i = 0; i < list.length; i++) {
if (list[i] == 1)
ret.add(strs[i]);
}
return ret;
}
}
S算法问题: Balanced Binary Tree 问题描述:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which
the depth of the two subtrees of every node never differ by more than 1.
S算法问题: Best Time to Buy and Sell Stock II 问题描述:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like
(ie, buy one and sell one share of the stock multiple times).
However, you may not engage in multiple transactions at the same time
(ie, you must sell the stock before you buy again).