Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

1. Open brackets must be closed by the same type of brackets.

2. Open brackets ...

想法:

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

Write a function to find the longest common prefix string amonst an array of strings.

If there is no common prefix, return an empty string "".

想法:

逐一比對字串的開頭字母

如果一樣,比對下一個字母;如果不一樣,將第一個字串的此字設為'\0',並回傳此字串。

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

題目

  Given an integer n, return the number of trailing zeroes in n!.

想法

  要求出尾端有幾個 0,不用把階層求出來,只要算當中有幾個 5,5x2 即會產生 0,但要注意 25 有兩個 5,以此類推 125...。

階層 2 是不缺少的,因此在意 5 就好,舉例,25!,除以 5 表示有 5 個 5,但其實,25 有 6 個 5,必須將 25/5 = 5 後再檢查一次。

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

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are  R  (Right).  L(Left),U(Up) andD(down). The output should be true or false representing whether the robot makes a circle.

Example 1:


Input: "UD"
Output: true

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

題目

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

    For example,

    Given nums = [0, 1, 3] return 2.

    Note:

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

題目

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example 1:

Input: [1,2,3]
Output: 6

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

題目

Given an array of intergers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

想法

一開始覺得可以檢查這個數字,在先前有沒有出現過,但是複雜度過高。

若可以先排序,就可以看到相同數字會相鄰出現。

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

題目

Given a non-negative integernum, repeatedly add all its digits until the result has only one digit.

For example:

Givennum = 38, the process is like:3 + 8 = 11,1 + 1 = 2. Since2has only one digit, return it.

Follow up:

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

題目

You are given a string representing an attendance record for a student. The record only contains the following three characters:


  • 'A': Absent.

  • 'L': Late.

  • 'P': Present.
  • Davis 發表在 痞客邦 留言(0) 人氣()

    題目

    Given an array of sizen, find the majority element. The majority element is the element that appearsmore than⌊ n/2 ⌋times.

    You may assume that the array is non-empty and the majority element always exist in the array.

    題意

    一個陣列中,找出出現超過⌊ n/2 ⌋次的數字。

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

    題目

      Given a roman numberal, convert it to an interger.

    Input is guaranteed to be within the range from 1 to 3999.

    題意

      給羅馬數字,輸出數字總和

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

    Question

      Given a binary tree, return the preorder traversal of its nodes' values.

    For example:

    Given binary tree  {1,#,2,3},

       1

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

    1 2 3 4
    Blog Stats
    ⚠️

    成人內容提醒

    本部落格內容僅限年滿十八歲者瀏覽。
    若您未滿十八歲,請立即離開。

    已滿十八歲者,亦請勿將內容提供給未成年人士。