Skip to content

Commit

Permalink
add move zero problem
Browse files Browse the repository at this point in the history
  • Loading branch information
sagivo committed May 13, 2018
1 parent 758edcc commit 546aa53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This is my way of saying - change your interview style. There are lots of smart
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unique binary search trees](https://leetcode.com/problems/unique-binary-search-trees/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/unique_bsts.rb) |
| [House Robber](https://leetcode.com/problems/house-robber/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/house-robber.rb) |
| [Decode Ways](https://leetcode.com/problems/decode-ways) | [click](https://github.com/sagivo/algorithms/blob/master/src/decode_ways.rb) |
| [Coin change](https://leetcode.com/problems/coin-change/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/coin-change.rb) |
| [Decode Strings](https://leetcode.com/problems/decode-string/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/decode_string.rb) |
| [Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/description/) | [click](https://github.com/sagivo/algorithms/blob/master/src/wiggle_subsequence.rb) |
Expand Down Expand Up @@ -50,6 +51,7 @@ This is my way of saying - change your interview style. There are lots of smart
| [Countingsort algorithm](http://en.wikipedia.org/wiki/Counting_sort) | [click](https://github.com/sagivo/algorithms/blob/master/src/counting_sort.rb) |
| [Shellsort algorithm](http://en.wikipedia.org/wiki/Shellsort) | [click](https://github.com/sagivo/algorithms/blob/master/src/shell_sort.rb) |
| [Knapsack problem](http://en.wikipedia.org/wiki/Knapsack_problem) | [click](https://github.com/sagivo/algorithms/blob/master/src/knapsack.rb), [click](https://github.com/sagivo/algorithms/blob/master/src/knapsack2.rb) |
| [Move Zeroes](https://leetcode.com/problems/move-zeroes) | [click](https://github.com/sagivo/algorithms/blob/master/src/move_zeroes.rb) |
| [Longest common subsequence problem](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) | [click](https://github.com/sagivo/algorithms/blob/master/src/longest_common_subsequence.rb) , [click](https://github.com/sagivo/algorithms/blob/master/src/longest_increasing_subsequence.rb) |
| [Monty Hall Problem](https://en.wikipedia.org/wiki/Monty_hall_problem) | [click](https://github.com/sagivo/algorithms/blob/master/src/monty_hall.rb) |
| [Eucliden and Extended Eucliden algorithm](http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm) | [click](https://github.com/sagivo/algorithms/blob/master/src/gcd.rb) |
Expand Down
16 changes: 16 additions & 0 deletions move_zeroes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# problem: https://leetcode.com/problems/move-zeroes
# @param {Integer[]} nums
# @return {Void} Do not return anything, modify nums in-place instead.
def move_zeroes(nums)
i = 0
nums.each do |n|
if n != 0
nums[i] = n
i += 1
end
end
(i...nums.size).each {|indx| nums[indx] = 0}
return nums
end

p move_zeroes([0,1,0,3,12]) #=> [1,3,12,0,0]

0 comments on commit 546aa53

Please sign in to comment.