Press "Enter" to skip to content

Posts tagged as “easy”

花花酱 LeetCode 234. Palindrome Linked List

题目大意:检查一个单向链表是不是回文。

Problem:

https://leetcode.com/problems/palindrome-linked-list/description/

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

Idea:

  1. use fast / slow pointers to find the middle node and see whether the list has odd/even number of elements.
  2. Reverse the right half the list, and compare with the left half

E.g.
1->2->3->4->3->2->1->null
fast = null
slow = 4
slow->next = 3
reverse(slow->next)
null<-3<-2<-1 compare with 1->2->3->…

Solution: 1

Time complexity: O(n)

Space complexity: O(1)

C++

 

 

 

花花酱 LeetCode 796. Rotate String

题目大意:给你两个字符串A, B, 问能否通过旋转A得到B。

Problem:

https://leetcode.com/problems/rotate-string/description/

We are given two strings, A and B.

shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A.

Note:

  • A and B will have length at most 100.

Solution 1: Brute Force

Time complexity: O(n^2)

Space complexity: O(1)

C++

 

花花酱 LeetCode 206. Reverse Linked List

题目大意:反转一个单向链表

Problem:

https://leetcode.com/problems/reverse-linked-list/description/

Reverse a singly linked list.

Solution 1:

Tracking prev / curr / next node

Time complexity: O(n)

Space complexity: O(1)

C++

Java

Python 3

 

花花酱 LeetCode 521. Longest Uncommon Subsequence I

题目大意:求2个字符串最长的不同子序列的长度。

Problem:

https://leetcode.com/problems/longest-uncommon-subsequence-i/description/

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.

The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn’t exist, return -1.

Example 1:

Note:

  1. Both strings’ lengths will not exceed 100.
  2. Only letters from a ~ z will appear in input strings.

Idea:

If two strings are the same, then the longest uncommon sequence does not exist, return -1.

e.g. aaa vs aaa, return -1

Otherwise, the longer string is always a uncommon sequence of the shorter one.

e.g. aaab vs aaa, return 4

Solution 1:

Time complexity: O(n)

Space complexity: O(1)

C++

Java

Python3

 

 

花花酱 LeetCode 620. Not Boring Movies

Problem:

https://leetcode.com/problems/not-boring-movies/description/

X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating the movies’ ratings and descriptions.

Please write a SQL query to output movies with an odd numbered ID and a description that is not ‘boring’. Order the result by rating.

For example, table cinema:

For the example above, the output should be:

MySQL