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 !
class Solution:
def addBinary(self, a, b):
return bin(int(a, 2) + int(b, 2)).split('b')[1]
def addBinary(self, a, b):
"""Sometimes built-in function cheats too much.
"""
res, carry, len_a, len_b, i = "", 0, len(a), len(b), 0
for i in range(max(len_a, len_b)):
sum = carry
if i < len_a:
sum += int(a[-(i + 1)])
if i < len_b:
sum += int(b[-(i + 1)])
carry = sum / 2
sum = sum % 2
res = "{0}{1}".format(sum, res)
if carry == 1:
res = "1" + res
return res
# def addBinary(self, a, b):
# """Using carry without sum is also fine. But the readability sucks.
# """
# res, carry, len_a, len_b, i = "", 0, len(a), len(b), 0
# for i in range(max(len_a, len_b)):
# if i < len_a:
# carry += int(a[-(i + 1)])
# if i < len_b:
# carry += int(b[-(i + 1)])
# res = "{0}{1}".format(carry % 2, res)
# carry = carry / 2
# if carry == 1:
# res = "1" + res
# return res
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.
Sort the string to see if they're anagrams.
Solution 1 is simpler than 2.
解法(Python)
代碼:
class Solution:
def anagrams(self, strs):
anagram_map, res = {}, []
for str in strs:
sorted_str = ("").join(sorted(str))
if sorted_str in anagram_map:
anagram_map[sorted_str].append(str)
else:
anagram_map[sorted_str] = [str]
for anagrams in anagram_map.values():
if len(anagrams) > 1:
res += anagrams
return res
# def anagrams(self, strs):
# """List comprehension may be more elegant but less readable here.
# """
# anagram_map = {}
# for str in strs:
# sorted_str = ("").join(sorted(str))
# if sorted_str in anagram_map:
# anagram_map[sorted_str].append(str)
# else:
# anagram_map[sorted_str] = [str]
# return [anagram for anagrams in anagram_map.values() if len(anagrams) > 1 for anagram in anagrams]
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).