diff --git a/Distinct Subsequences.py b/Distinct Subsequences.py index 3ef3b6c..e7e2591 100644 --- a/Distinct Subsequences.py +++ b/Distinct Subsequences.py @@ -1,4 +1,17 @@ class Solution: + def numDistinct(self, S, T): + n, m = len(S), len(T) + f = [0] * (m + 1) + f[0] = 1 + for i in xrange(n): + for j in xrange(m - 1, -1, -1): + if S[i] == T[j]: + f[j + 1] += f[j] + return f[m] + +class Solution: + ''' The original solution is to use a 2D array. + ''' def numDistinct(self, S, T): ways = [[0 for j in range(len(S) + 1)] for i in range(len(T) + 1)] for i in range(len(S) + 1): @@ -8,4 +21,4 @@ def numDistinct(self, S, T): ways[i][j] = ways[i][j - 1] if T[i - 1] == S[j - 1]: ways[i][j] += ways[i - 1][j - 1] - return ways[len(T)][len(S)] \ No newline at end of file + return ways[len(T)][len(S)] diff --git a/Excel Sheet Column Number.py b/Excel Sheet Column Number.py new file mode 100644 index 0000000..c715f7c --- /dev/null +++ b/Excel Sheet Column Number.py @@ -0,0 +1,7 @@ +class Solution: + def titleToNumber(self, s): + num = 0 + for i in s: + num *= 26 + num += ord(i) - 64 + return num diff --git a/Largest Number.py b/Largest Number.py new file mode 100644 index 0000000..e18d155 --- /dev/null +++ b/Largest Number.py @@ -0,0 +1,5 @@ +class Solution: + def largestNumber(self, num): + num = [str(x) for x in num] + num.sort(cmp = lambda x, y: cmp(y+x, x+y)) + return ''.join(num).lstrip('0') or '0' diff --git a/Number of 1 Bits.py b/Number of 1 Bits.py new file mode 100644 index 0000000..960e0bc --- /dev/null +++ b/Number of 1 Bits.py @@ -0,0 +1,7 @@ +class Solution: + def hammingWeight(self, n): + count = 0 + while n: + count += 1 + n = (n - 1) & n + return count diff --git a/Remove Nth Node From End of List.py b/Remove Nth Node From End of List.py index 582119d..4ca1817 100644 --- a/Remove Nth Node From End of List.py +++ b/Remove Nth Node From End of List.py @@ -1,11 +1,12 @@ class Solution: def removeNthFromEnd(self, head, n): - fast, slow, prev = head, head, None - while n > 0: - fast, n = fast.next, n - 1 - while fast != None: - fast, slow, prev = fast.next, slow.next, slow - if prev == None: - return head.next - prev.next = prev.next.next - return head \ No newline at end of file + dummy = ListNode(-1) + dummy.next = head + left, right = dummy, dummy + while n: + right, n = right.next, n-1 + while right.next: + right = right.next + left = left.next + left.next = left.next.next + return dummy.next diff --git a/Reverse Words in a String.py b/Reverse Words in a String.py index cf23aff..0cd492d 100644 --- a/Reverse Words in a String.py +++ b/Reverse Words in a String.py @@ -1,3 +1,3 @@ class Solution: def reverseWords(self, s): - return " ".join(filter(lambda x: x, reversed(s.split(" ")))) \ No newline at end of file + return " ".join(filter(None, reversed(s.split(" ")))) diff --git a/Word Break II.py b/Word Break II.py index 97f94b4..680eccf 100644 --- a/Word Break II.py +++ b/Word Break II.py @@ -4,7 +4,6 @@ def wordBreak(self, s, dict): f = [False for _ in xrange(n)] trace = [[False] * n for _ in xrange(n)] for i in xrange(n): - trace.append([]) if s[:i+1] in dict: f[i] = True trace[0][i] = True