Skip to content

Instantly share code, notes, and snippets.

Desolve

Block or report user

Report or block Desolve

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View 0001 Two Sum.java
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if(map.containsKey(complement)) {
return new int[] { map.get(complement), i};
}
map.put(nums[i], i);
}
View 0001 Two Sum.py
class Solution:
def twoSum(self, nums: 'List[int]', target: 'int') -> 'List[int]':
numMap = {}
for i in range(len(nums)):
if numMap.__contains__(target-nums[i]):
return [numMap.get(target-nums[i]), i]
else:
numMap[nums[i]] = i
@Desolve
Desolve / 0014 Longest Common Prefix.py
Last active Jun 28, 2019
0014 Longest Common Prefix
View 0014 Longest Common Prefix.py
class Solution:
def longestCommonPrefix(self, strs: 'List[str]') -> 'str':
if not strs: return ""
s1 = min(strs)
s2 = max(strs)
for i, c in enumerate(s1):
if c != s2[i]:
return s1[:i]
return s1
View 0014 Longest Common Prefix.java
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0) return "";
String pre = strs[0];
int i = 1;
while(i < strs.length){
while(strs[i].indexOf(pre) != 0)
pre = pre.substring(0, pre.length() - 1);
i++;
}
View 0015 3Sum.java
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new LinkedList<>();
for (int i = 0; i < nums.length-2; i++) {
if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
int j = i + 1, k = nums.length - 1, target = 0 - nums[i];
while (j < k) {
if (nums[j] + nums[k] == target) {
res.add(Arrays.asList(nums[i], nums[j], nums[k]));
View 0015 3Sum.py
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) < 3:
return []
nums.sort()
res = set()
View 0021 Merge Two Sorted Lists.java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
@Desolve
Desolve / 0021 Merge Two Sorted Lists.py
Last active Jun 30, 2019
0021 Merge Two Sorted Lists
View 0021 Merge Two Sorted Lists.py
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
dum = ListNode(None)
prev = dum
@Desolve
Desolve / 0026 Remove Duplicates from Sorted Array.java
Created Jul 1, 2019
0026 Remove Duplicates from Sorted Array
View 0026 Remove Duplicates from Sorted Array.java
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length <= 1) return nums.length;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
@Desolve
Desolve / 0026 Remove Duplicates from Sorted Array.py
Created Jul 1, 2019
0026 Remove Duplicates from Sorted Array
View 0026 Remove Duplicates from Sorted Array.py
class Solution:
def removeDuplicates(self, nums):
if not nums: return 0
i, j = 0, 1
while j < len(nums):
if nums[i] != nums[j]:
i += 1
nums[i] = nums[j]
j += 1
return i + 1
You can’t perform that action at this time.