Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1543 from kabirdhillon7/kabirdhillon7-…
Browse files Browse the repository at this point in the history
…patch-2

344-Reverse-String.swift
  • Loading branch information
Ahmad-A0 authored Dec 22, 2022
2 parents bd959bc + d5743b0 commit 8ed7092
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cpp/283-Move-Zeroes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int left = 0;

for(int i = 0; i < nums.size(); i++){
if(nums[i] != 0){
nums[left++] = nums[i];
}
}

for(left; left < nums.size(); left ++){
nums[left] = 0;
}
}
};
41 changes: 41 additions & 0 deletions cpp/682-Baseball-Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int> stack;
int sum = 0;

for (int i = 0; i < ops.size(); i++){
if (ops[i] == "+"){
int first = stack.top();
stack.pop();

int second = stack.top();

stack.push(first);

stack.push(first + second);

sum += first + second;
}

else if (ops[i] == "D"){
sum += 2 * stack.top();
stack.push(2 * stack.top());
}

else if (ops[i] == "C"){
sum -= stack.top();
stack.pop();
}

else{
sum += stoi(ops[i]);
stack.push(stoi(ops[i]));
}
}

return sum;


}
};
20 changes: 20 additions & 0 deletions swift/169-Majority-Element.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
guard !nums.isEmpty else {
return 0
}

var seen = [Int : Int]()

for num in nums {
let newOccurance = seen[num, default: 0] + 1
seen[num] = newOccurance

if newOccurance > nums.count / 2 {
return num
}
}

return -1
}
}
16 changes: 16 additions & 0 deletions swift/344-Reverse-String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
func reverseString(_ s: inout [Character]) {
var left: Int = 0
var right: Int = s.count - 1

while left < right {
let temp: Character = s[left]

s[left] = s[right]
s[right] = temp

left += 1
right -= 1
}
}
}

0 comments on commit 8ed7092

Please sign in to comment.