瀏覽標籤:

LeetCode

[C#][LeetCode][Easy] 561. Array Partition I

問題:

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

心得:
看到這題我第一個反應就是去找linq有沒有辦法分割陣列,然後再加總= =
不過LINQ效能沒有很好,看到最佳解答有提供一個不錯的方法。

我的答案:

最佳解答:

       

[C#][LeetCode][Easy] 617. Merge Two Binary Trees

問題:

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

心得:

這題就是把兩個二元樹合併,我就想著最簡單遞迴來解決,看了最佳解答才發現如果為一邊null的話就可以直接用另外一邊來return更有效率,學習了。
我的答案:

最佳解答:

 

       

[C#][LeetCode][Easy] 167. Two Sum II – Input array is sorted

心得

這題我本來是想用兩個for迴圈解決的

但發現會Time Limit Exceeded於是乎看了一下Solutions他們是用一個while迴圈來解答,題目要求index1<index2且不能使用重複的元素,所以這題如果用while的話宣告兩個變數分別是left與right就可以只用一個迴圈就解決。

問題

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution and you may not use the same element twice.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

答案

 

       

[C#][LeetCode][Easy] 409. Longest Palindrome

心得

獲得字串並判斷字串內的字母能組成最長的回文大小為多少,例如傳入asdasdAAAvzxh則答案為9,字串為asdAvAdsa,其中v可替換成任意一位使用字母。

問題

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa" is not considered a palindrome here.

Note:
Assume the length of given string will not exceed 1,010.

Example:

答案

 

       

[C#][LeetCode][Easy] 349. Intersection of Two Arrays

心得

題目要求找出兩個int[]交集的數字有哪些且不能重複並輸出。

問題

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

答案

  1. .NET提供的方法
  2. 上面那個方法太賤了,於是乎自己硬幹了一次

     
       

[C#][LeetCode][Easy] 383. Ransom Note

心得

這題要比對字串變數ransomNote是否存在於字串變數magazine裡。

問題

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom
note can be constructed from the magazines ; otherwise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

Note:
You may assume that both strings contain only lowercase letters.

答案

  1. 先將所有字母的數量統計在比較
  2. 方法1的簡化版
  3. 這方法是在解答內看到的,解題思路與方法1差不多但是速度快了很多。

     
       

[C#][LeetCode][Easy] 500. Keyboard Row

心得

鍵盤共有三行[qwertyuiop, asdfghjkl, zxcvbnm],找出在同行字母拼湊出來的單字。

問題

Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.

keyboard

 

Example 1:

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

答案

       

[C#][LeetCode][Easy] 455. Assign Cookies

心得

題目詢問說共可以滿足幾個小孩所期望的蛋糕數,例如總共有三個小孩A, B, C,A希望獲得1片蛋糕;B希望獲得2片蛋糕;C希望獲得3片蛋糕,而總共有兩塊蛋糕Z, X,Z可以切成1片,X可以切成兩片,這樣的話答案是2,因為最多只能滿足兩個小孩的需求。依照這個思路去撰寫Code就容易得多了。

問題

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

Note:
You may assume the greed factor is always positive.
You cannot assign more than one cookie to one child.

Example 1:

Example 2:

答案

 

       

[C#][LeetCode][Easy] 492. Construct the Rectangle

心得

簡單來說就是輸入數字x,找出x的因數並找出相乘會等於x的兩個數字且兩數字相減最接近0的組合,大的放前面小的放後面以int[]的型態回傳。

問題

For a web developer, it is very important to know how to design a web page’s size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:

You need to output the length L and the width W of the web page you designed in sequence.

Example:

Note:

  1. The given area won’t exceed 10,000,000 and is a positive integer
  2. The web page’s width and length you designed must be positive integers.

答案

  1. 我的笨方法
  2. Top Solution的神方法

     
       

[C#][LeetCode][Easy] 485. Max Consecutive Ones

心得

傳入一個二進位的陣列,找出1最多連續出現的次數。

問題

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

Note:

  • The input array will only contain 0 and 1.
  • The length of input array is a positive integer and will not exceed 10,000

答案