This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer