Complex Number Multiplication
Given two strings representing two complex numbers.
You need to return a string representing their multiplication. Note i2 = -1 according to the definition.
Example 1:
Input: "1+1i", "1+1i" Output: "0+2i" Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.
Example 2:
Input: "1+-1i", "1+-1i" Output: "0+-2i" Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.
Note:
- The input strings will not have extra blank.
- The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.
class Solution(object):
def complexNumberMultiply(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
a=a.rstrip("i").split("+")
b=b.rstrip("i").split("+")
aa=int(a[0])
ab=int(a[1])
ba=int(b[0])
bb=int(b[1])
shishuPart=aa*ba-ab*bb
xushuPart=aa*bb+ab*ba
return str(shishuPart)+"+"+str(xushuPart)+"i"
Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...
Note:
n is positive and will fit within the range of a 32-bit signed integer (n < 231).
Example 1:
Input: 3 Output: 3
Example 2:
Input: 11 Output: 0 Explanation: The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.
class Solution(object):
def findNthDigit(self, n):
"""
:type n: int
:rtype: int
"""
strN=str(n)
length=len(strN)
if length==1:
return n
n=n-9 #1位数的总共有9个,减去这9个数
for i in range(2,length+1):
if n>i*(10**i-10**(i-1)):
n-= i*(10**i-10**(i-1))
else:
if n % i == 0:
numberIndex=int(n/i)
else:numberIndex=int(n/i)+1
charIndex=n%i
number=10**(i-1)+numberIndex-1
if charIndex==0:
return int(str(number)[charIndex-1])
return int(str(number)[charIndex-1])
Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows==0:
return []
if numRows==1:
return [[1]]
if numRows==2:
return [[1],[1,1]]
result=[[1],[1,1]]
for i in range(numRows-2):
length=len(result)
lastElement=result[length-1]
toBeAppended=[1]
for i in range(length-1):
toBeAppended.append(lastElement[i]+lastElement[i+1])
toBeAppended.append(1)
result.append(toBeAppended)
return result
Base 7
Given an integer, return its base 7 string representation.
Example 1:
Input: 100 Output: "202"
Example 2:
Input: -7 Output: "-10"
Note: The input will be in range of [-1e7, 1e7].
class Solution(object):
def convertToBase7(self, num):
"""
:type num: int
:rtype: str
"""
result=""
isNegtive=False
if num < 0:
isNegtive=True
num=-num
Count=1
while 7**Count <= num:
Count+=1
for i in range(Count):
if num>=7**(Count-i-1):
result+=str(int(num/7**(Count-i-1)))
num=num%7**(Count-i-1)
else:
result+="0"
if isNegtive:
return "-"+result
else:
return result
Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).
Each LED represents a zero or one, with the least significant bit on the right.
For example, the above binary watch reads "3:25".
Given a non-negative integer n which represents the number of LEDs that are currently on, return all possible times the watch could represent.
Example:
Input: n = 1
Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
Note:
- The order of output does not matter.
- The hour must not contain a leading zero, for example "01:00" is not valid, it should be "1:00".
- The minute must be consist of two digits and may contain a leading zero, for example "10:2" is not valid, it should be "10:02".
import itertools
class Solution(object):
def readBinaryWatch(self, num):
"""
:type num: int
:rtype: List[str]
这是一个组合的问题
"""
result=[]
combinations=list(itertools.combinations(range(0,10),num))
for i in combinations:
strTime=list("0000000000")
for j in i:
strTime[j]=1
hour=0
minute=0
for j in range(6):
if strTime[j]==1:
minute+=2**j
if minute>=60:
continue
for j in [6,7,8,9]:
if strTime[j]==1:
hour+=2**(j-6)
if hour>=12:
continue
if minute<10:
minute="0"+str(minute)
result.append(str(hour)+":"+str(minute))
return result
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
一个一个往前 , 如果遇到相同的,如abcb,那么从第一个字符截断,变成cb,再往下找。
"""
if s=="":
return 0
length=len(s)
if length==1:
return 1
maxString=1
tmpS=""
for i in s:
tmpS=tmpS+i
setS=set(tmpS)
if len(setS)<len(tmpS): #如果这时候遇到重复的字符串了。
repeatCharIndex=tmpS.index(tmpS[len(tmpS)-1])
tmpS=tmpS[repeatCharIndex+1:]
setS=set(tmpS)
else:
maxString=max(maxString,len(tmpS))
return maxString