Professional Documents
Culture Documents
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
n = len(nums)
indices_to_replace = []
for x in range(n):
if nums[x]==val:
indices_to_replace.append(x)
if len(indices_to_replace)==0:
return n
if len(indices_to_replace)==n:
return 0
for y in range(n-1,-1,-1):
if nums[y]!=val:
z = y +1
while z>y:
z = indices_to_replace.pop(-1)
else:
continue
nums[y], nums[z] = nums[z], nums[y]
if len(indices_to_replace)==0:
return y
Given an integer array nums and an integer val, remove all occurrences of val in
nums in-place. The relative order of the elements may be changed.
Since it is impossible to change the length of the array in some languages, you
must instead have the result be placed in the first part of the array nums. More
formally, if there are k elements after removing the duplicates, then the first k
elements of nums should hold the final result. It does not matter what you leave
beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
Do not allocate extra space for another array. You must do this by modifying the
input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i < actualLength; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1:
Constraints:
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
n = len(nums)
indices_to_replace = []
for x in range(n):
if nums[x]==val:
indices_to_replace.append(x)
if len(indices_to_replace)==0:
return n
if len(indices_to_replace)==n:
return 0
for y in range(n-1,-1,-1):
if nums[y]!=val:
z = y +1
while z>y and len(indices_to_replace)>0:
z = indices_to_replace.pop(-1)
if z>y:
continue
else:
continue
nums[y], nums[z] = nums[z], nums[y]
if len(indices_to_replace)==0:
return y
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
n = len(nums)
indices_to_replace = []
for x in range(n):
if nums[x]==val:
indices_to_replace.append(x)
if len(indices_to_replace)==0:
return n
if len(indices_to_replace)==n:
return 0
for y in range(n-1,-1,-1):
if nums[y]!=val:
z = y +1
while z>y and len(indices_to_replace)>0:
z = indices_to_replace.pop(-1)
if z>y:
return y+1
else:
continue
nums[y], nums[z] = nums[z], nums[y]
if len(indices_to_replace)==0:
return y
Given an array of distinct integers candidates and a target integer target, return
a list of all unique combinations of candidates where the chosen numbers sum to
target. You may return the combinations in any order.
The same number may be chosen from candidates an unlimited number of times. Two
combinations are unique if the frequency of at least one of the chosen numbers is
different.
Example 1:
Constraints:
class Solution:
def combinationSum(self, candidates: List[int], target: int) ->
List[List[int]]:
candidates.sort()
n = len(candidates)
for start in range(n):
start2 = start
while start2<n:
current_count = 0
class Solution:
def combinationSum(self, candidates: List[int], target: int) ->
List[List[int]]:
candidates.sort()
n = len(candidates)
possible_sums = []
possible_sums.append([[]])
possible_1 = []
if 1 in candidates:
possible_1.append([1])
possible_sums.append(possible_1)
if 2 in candidates:
possible_2.append([2])
if 1 in candidates:
possible_2.append([1,1])
possible_sums.append(possible_2)
class Solution:
def combinationSum(self, candidates: List[int], target: int) ->
List[List[int]]:
candidates.sort()
n = len(candidates)
possible_sums = []
possible_sums.append([[]])
possible_1 = []
candidate_exist = [0]*30
for x in range(n):
candidate_exist[candidate[x]] = 1
if candidate_exist[1]==1:
possible_1.append([1])
possible_sums.append(possible_1)
if candidate_exist[2]==1:
possible_2.append([2])
if candidate_exist[1]==1:
possible_2.append([1,1])
possible_sums.append(possible_2)
LAST SUBMISSION
ast Submission
Last Saved Code
class Solution:
def combinationSum(self, candidates: List[int], target: int) ->
List[List[int]]:
candidates.sort()
n = len(candidates)
possible_sums = []
possible_sums.append([[]])
possible_1 = []
candidate_exist = [0]*30
for x in range(n):
candidate_exist[candidate[x]] = 1
if candidate_exist[1]==1:
possible_1.append([1])
possible_sums.append(possible_1)
if candidate_exist[2]==1:
possible_2.append([2])
if candidate_exist[1]==1:
possible_2.append([1,1])
possible_sums.append(possible_2)
def get_combinations(ll1,ll2):
combos = []
for l1 in ll1:
for l2 in ll2:
combos.append(l1+l2)
return combos
for target2 in range(3,target+1):
possible = []
used_num = [0]*30
if candidate_exist[target2]==1:
possible.append([target2])
for add1 in range(target2-1,1,-1):
if used_num[add1]=1:
continue
add2 = target - add1
used_num[add1] = 1
used_num[add2] = 1