目前分類:未分類文章 (30)

瀏覽方式: 標題列表 簡短摘要

g0d2 發表在 痞客邦 留言(0) 人氣()

Shuffle a given array

Given an array, write a program to generate a random permutation of array elements. This question is also asked as “shuffle a deck of cards” or “randomize a given array”.

g0d2 發表在 痞客邦 留言(0) 人氣()

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

g0d2 發表在 痞客邦 留言(0) 人氣()

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.


# Definition for singly-linked list.

g0d2 發表在 痞客邦 留言(0) 人氣()

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

You may assume that the intervals were initially sorted according to their start times.

g0d2 發表在 痞客邦 留言(0) 人氣()

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].

g0d2 發表在 痞客邦 留言(0) 人氣()

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].

g0d2 發表在 痞客邦 留言(0) 人氣()

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

g0d2 發表在 痞客邦 留言(0) 人氣()

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:

g0d2 發表在 痞客邦 留言(0) 人氣()

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

g0d2 發表在 痞客邦 留言(0) 人氣()

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

g0d2 發表在 痞客邦 留言(0) 人氣()

A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.
Source: Mathword(http://mathworld.wolfram.com/Permutation.html)

g0d2 發表在 痞客邦 留言(0) 人氣()

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

g0d2 發表在 痞客邦 留言(0) 人氣()

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

g0d2 發表在 痞客邦 留言(0) 人氣()

 

2016(1-3月) 

g0d2 發表在 痞客邦 留言(0) 人氣()

2016(1-3月) 


电面一轮
* reverse linked list.鏈枃鍘熷垱鑷�1point3acres璁哄潧
* implement circular array

Onsite 四轮
Round 1:
* group anagram
* phone number combination. 涓€浜�-涓夊垎-鍦帮紝鐙鍙戝竷

Round 2:
* check big/small endian. 鍥磋鎴戜滑@1point 3 acres
* move zero to right
* sparse vector storage and product

Round 3: Design post search

Round 4: research. 涓€浜�-涓夊垎-鍦帮紝鐙鍙戝竷

g0d2 發表在 痞客邦 留言(0) 人氣()

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

文章標籤

g0d2 發表在 痞客邦 留言(0) 人氣()

Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

文章標籤

g0d2 發表在 痞客邦 留言(0) 人氣()

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.


# Definition for a point.

文章標籤

g0d2 發表在 痞客邦 留言(0) 人氣()

Fill in the missing code:

def print_directory_contents(sPath):
    """
    This function takes the name of a directory 
    and prints out the paths files within that 
    directory as well as any files contained in 
    contained directories. 

    This function is similar to os.walk. Please don't
    use os.walk in your answer. We are interested in your 
    ability to work with nested structures. 
    """
    fill_this_in

Answer

def print_directory_contents(sPath):
    import os                                       
    for sChild in os.listdir(sPath):                
        sChildPath = os.path.join(sPath,sChild)
        if os.path.isdir(sChildPath):
            print_directory_contents(sChildPath)
        else:
            print(sChildPath)

Example 6.18. Listing Directories

>>> os.listdir("c:\\music\\_singles\\")              1
['a_time_long_forgotten_con.mp3', 'hellraiser.mp3',
'kairo.mp3', 'long_way_home1.mp3', 'sidewinder.mp3', 
'spinning.mp3']
>>> dirname = "c:\\"
>>> os.listdir(dirname)                              2
['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'cygwin',
'docbook', 'Documents and Settings', 'Incoming', 'Inetpub', 'IO.SYS',
'MSDOS.SYS', 'Music', 'NTDETECT.COM', 'ntldr', 'pagefile.sys',
'Program Files', 'Python20', 'RECYCLER',
'System Volume Information', 'TEMP', 'WINNT']
>>> [f for f in os.listdir(dirname)
...     if os.path.isfile(os.path.join(dirname, f))] 3
['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'IO.SYS', 'MSDOS.SYS',
'NTDETECT.COM', 'ntldr', 'pagefile.sys']
>>> [f for f in os.listdir(dirname)
...     if os.path.isdir(os.path.join(dirname, f))]  4
['cygwin', 'docbook', 'Documents and Settings', 'Incoming',
'Inetpub', 'Music', 'Program Files', 'Python20', 'RECYCLER',
'System Volume Information', 'TEMP', 'WINNT']
1 The listdir function takes a pathname and returns a list of the contents of the directory.
2 listdir returns both files and folders, with no indication of which is which.
3 You can use list filtering and the isfile function of the os.path module to separate the files from the folders. isfile takes a pathname and returns 1 if the path represents a file, and 0 otherwise. Here you're using os.path.join to ensure a full pathname, but isfile also works with a partial path, relative to the current working directory. You can use os.getcwd() to get the current working directory.
4 os.path also has a isdir function which returns 1 if the path represents a directory, and 0 otherwise. You can use this to get a list of the subdirectories within a directory.

g0d2 發表在 痞客邦 留言(0) 人氣()

1 2