forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#1543 from kabirdhillon7/kabirdhillon7-…
…patch-2 344-Reverse-String.swift
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
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
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; | ||
} | ||
} | ||
}; |
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
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; | ||
|
||
|
||
} | ||
}; |
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
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 | ||
} | ||
} |
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
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 | ||
} | ||
} | ||
} |