Description:

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Example 1:

Input: head = [1,1,2,3,3]
Output: [1,2,3]

Solution 1:

Time complexity O(N). We would need both prev and curNode. If prev’s value is equivalent to curNode’s value, set prev.next to curNode.next. Otherwise, move prev to prev.next.

--

--

Description:

Given two binary strings a and b, return their sum as a binary string.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Solution 1:

Time complexity O(max(len(A), len(B))). Python use ord() to change character into ASCII code. And str() would convert int to string. If you want to use chr(), change the str() part to chr(bitsum%2 + ord(‘0’)).

--

--

Description:

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Solution 1:

Dynamic Programming. Time complexity O(N), N= Length of nums. In Dynamic Proggramming, solution of i+1 step is close related to the solution of i step and i+1 value. In this problem, we have to consider if i+1 value is the new head of a contiguous subarray. The head of a contiguous subarray would fulfill the requirement that its value x would greater than the summation of x and tempMax. And each time, we have to check if the updated tempMax is greater than the res value.

--

--

Description:

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

Example 1:

Input: nums = [1,3,5,6], target = 5
Output: 2

Example 2:

Input: nums = [1,3,5,6], target = 2
Output…

--

--

Description:

Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.

Example 1:

Input: l1 = [1,2,4], l2 = [1,3,4]
Output: [1,1,2,3,4,4]

Solution 1:

Time complexity O(A+B), A = Number of nodes in L1, B= Number of nodes in L2. The prevNode is used to keep track of the previous Node. Because node in linked list only have the information of the node value, and next node’s memory address. Mind that Python would pass by reference, when assigning object.

--

--

Description:

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

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

Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Solution 1:

Time complexity O(S),S stands for the strs List’s total number of characters . The worst case would be the strings in the List are all the same.

--

--

Sherlock Chiou

Sherlock Chiou

Be Effective and Optimize for Learning