Thursday, October 27, 2016

Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. First of all, if this string has an odd number of characters, we know already that it isn't valid. class Solution(object): def isValid(self, s): if len(s) % 2 ==...

Wednesday, October 26, 2016

Remove Nth Node from End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Try to do this in one pass. We are given the ListNode class class ListNode(object): def __init__(self, x): ...

Sunday, October 23, 2016

ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: P   A    H   N A P L S  I  I  G Y       I      R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string text, int nRows); convert("PAYPALISHIRING",...

Saturday, October 22, 2016

Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target:...