2017年11月30日 星期四

2017年11月27日 星期一

[LeetCode] 237. Delete Node in a Linked List

轉自LeetCode

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
<Solution>

[LeetCode] 283. Move Zeroes

轉自LeetCode

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.
<Solution>

2017年11月25日 星期六

[LeetCode] 220. Contains Duplicate III

轉自LeetCode

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.

<Solution>

2017年11月24日 星期五

[LeetCode] 219. Contains Duplicate II

轉自LeetCode

Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.

 

Example 1:

Input: nums = [1,2,3,1], k = 3
Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1
Output: true

Example 3:

Input: nums = [1,2,3,1,2,3], k = 2
Output: false

 

Constraints:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109
  • 0 <= k <= 105

<Solution>

2017年11月19日 星期日

[LeetCode] 217. Contains Duplicate

轉自LeetCode

Given an array of integers, 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.

<Solution>

[LeetCode] 204. Count Primes

轉自LeetCode

Description:
Count the number of prime numbers less than a non-negative number, n.
<Solution>