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 !
This is Part 1 (Java Solution)
S算法问题: Add Binary
问题描述:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
S算法问题: Anagrams
问题描述:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
算法问题推荐解法来了...
Sort the string to see if they're anagrams.
Solution 1 is simpler than 2.
解法(Java)
代码: |
import java.util.ArrayList;
import java.util.Arrays;
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).