Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A collection of solutions I've come up with for various coding challenges.

NotificationsYou must be signed in to change notification settings

kembreyfarquhar/ChallengeSolutions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 

Repository files navigation

This is a small, yet growing, collection of solutions that I've created for various code challenges. I made this repository with intentions to 1) have an organized collection of my solutions, 2) force myself to explain said solutions, ensuring that I understand why/how the solutions works, and 3) to revisit said solutions at a later date, utilizing my ever-growing coding abilities to determine if/how I could solve them in a better/more efficient manner. 💃

created by@kembreyfarquhar

Table of Contents

JavaScriptPythonPHP
Two SumTwo SumTwo Sum
Reverse IntegerReverse Integer
Palindrome NumberPalindrome NumberPalindrome Number
Twin PrimesMerge Two Sorted Lists
Longest StringLongest Common Prefix
Reverse StringLongest Substring Without Repeating Characters
Reverse NumberRemove Element
Reverse Case
Valid Parentheses
Stack Machine

Two Sum

👀See JavaScript Solution

👀See Python Solution

👀See PHP Solution

Task:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

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

For Example:

InputOutput
nums =[2, 7, 11, 15], target =9[0, 1]

Note:

Make sure you are returning theindicies of the values, not the values themselves.

Link to Challenge:https://leetcode.com/problems/two-sum/

Reverse Integer

👀See JavaScript Solution

👀See Python Solution

Task

Given a 32-bit signed integer, reverse digits of an integer.

For Example:

InputOutput
123321
-123-321
12021

Note:Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Link to Challenge:https://leetcode.com/problems/reverse-integer/

Palindrome Number

👀See JavaScript Solution

👀See Python Solution

👀See PHP Solution

Task

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

For Example:

InputOutput
121True
-121False
10False

Explanation for 2nd example: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string? (I took up this challenge and solved it without converting the int to a string).

Link to Challenge:https://leetcode.com/problems/palindrome-number/

Twin Primes

👀See JavaScript Solution

Task:

A twin prime is a prime number that differs from another prime number by two. Write a function called isTwinPrime which takes an integer and returns true if it is a twin prime, or false if it is not.

For Example:

  • 5 is a prime, and 5 + 2 = 7, which is also a prime, so returnstrue.

  • 9 is not a prime, and so does not need checking, so it returnsfalse.

  • 7 is a prime, but 7 + 2 = 9, which is not a prime. However, 7 - 2 = 5, which is a prime, so it returnstrue.

  • 23 is a prime, but 23 + 2 is 25, which is not a prime. 23 - 2 is 21, which isn't a prime either, so 23 is not a twin prime, returnfalse.

Longest String

👀See JavaScript Solution

Task:

Write a function that takes an array of strings and return the longest string in the array.

For Example:

conststrings1=["short","really, really long!","medium"];console.log(longestString(strings1));// <--- 'really, really long!'

Edge case: If you had an array which had two "longest" strings of equal length, your function should just return the first one.

Reverse String

👀See JavaScript Solution

Task:

Write a function called reverseString that accepts a string and returns a reversed copy of the string.

For Example:

InputOutput
'hello world''dlrow olleh'
'asdf''fdsa'
'CS rocks!''!skcor SC'

Reverse Number

👀See JavaScript Solution

Task:

Write a function called reverseNumber that reverses a number.

For Example:

InputOutput
1234554321
555555

Reverse Case

👀See JavaScript Solution

Task:

Write a function that takes in a string, reverses the 'casing' of that string, and returns the "reversed-casing" string.

For Example:

conststring="HELLO world!";console.log(reverseCase(string));// <--- hello WORLD!

Valid Parentheses

👀See JavaScript Solution

Task

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

An input string is valid if:

  • Open brackets must be closed by the same type of brackets.
  • Open brackets must be closed in the correct order.
  • Note that an empty string is also considered valid.

For Example:

InputOutput
"()"true
"()[]{}"true
"(]"false
"([)]"false
"{[]}"true

Link to Challenge:https://leetcode.com/problems/valid-parentheses/

Longest Common Prefix

👀See Python Solution

Task

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 "".

For Example:

InputOutput
["flower", "flow", "flight"]"fl"
[ "dog","racecar","car" ]""

Note:

All given inputs are in lowercase letters a-z.

Link to Challenge:https://leetcode.com/problems/longest-common-prefix/

Merge Two Sorted Lists

👀See Python Solution

Task

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

For Example:

InputOutput
1->2->4, 1->3->41->1->2->3->4->4

Note:

These are linked lists, not arrays.

Link to Challenge:https://leetcode.com/problems/merge-two-sorted-lists/

Longest Substring Without Repeating Characters

👀See Python Solution

Task

Given a string, find the length of the longest substring without repeating characters.

For Example:

InputOutput
"abcabcbb"3
"bbbbbb"1
"pwwkew"3

Link to Challenge:https://leetcode.com/problems/longest-substring-without-repeating-characters/

Remove Element

👀See Python Solution

Task

Given an array nums and a value val, remove all instances of that valuein-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

For Example:

InputOutput
nums = [3,2,2,3], val = 32
nums = [0,1,2,2,3,0,4,2], val = 25

Link to Challenge:https://leetcode.com/problems/remove-element/

Stack Machine

👀See JavaScript Solution

Task:

A stack machine is a simple system that performs arithmetic operations on an input string of numbers and operators. It contains a stack that can store an arbitrary number of 12-bit unsigned integers. Initially the stack is empty. The machine processes a string of characters in the following way:

  • the characters of the string are processed one by one

  • if the current character is a digit[0-9], the machine pushes the value of that digit onto its stack

  • if the current character is+, the machine pops the two topmost values from its stack, adds them and pushes the result onto the stack

  • if the current character is*, the machine pops the two topmost values from its stack, multiplies them and pushes the result onto the stack

  • after the machine has processed the whole string it returns the topmost value of its stack as the result

  • the machine reports an error if any operation it performs (addition or multiplication) results in an overflow

  • the machine reports an error if it tries to pop an element from its stack when the stack is empty, or if the stack is empty after the machine has processed the whole string.

For example, given the string"13+62*7+*" the machine will perform the following operations:

charactercommentstack
empty
'1'push 1 onto the stack1
'3'push 3 onto the stack1, 3
'+'perform addition4
'6'push 6 onto the stack4, 6
'2'push 2 onto the stack4, 6, 2
'*'perform multiplication4, 12
'7'push 7 onto the stack4, 12, 7
'+'perform addition4, 19
'*'perform multiplication76

The machine will return76 as the result as it is the topmost element of its stack.Write a function that, given a string S consisting of N characters containing input for the stack machine, returns the result the machine would return if given this string. The function should return -1 if the machine would report an error when processing the string.For example, given StringS = "13+62*7+*" the function should return76, as explained in the example above. Given StringS = "11++" the function should return-1.

Assume that:

  • the length of S is within the range[0..200,000]

  • string S consists only of characters[0-9],+ and/or*.

About

A collection of solutions I've come up with for various coding challenges.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp