diff --git a/Sample.java b/Sample.java deleted file mode 100644 index f5c45b5f..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach \ No newline at end of file diff --git a/findDisappearedNumbers.java b/findDisappearedNumbers.java new file mode 100644 index 00000000..99e282ed --- /dev/null +++ b/findDisappearedNumbers.java @@ -0,0 +1,54 @@ +// Time Complexity : +// Space Complexity : +// Did this code successfully run on Leetcode : +// Any problem you faced while coding this : + +// Question https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ +// Your code here along with comments explaining your approach +/** + * Time complexity : O(n) where n is number of elements in array + * Space complexity : O(n) + * Did this code successfully run on Leetcode : yes + */ +class Solution { + public List findDisappearedNumbers(int[] nums) { + HashMap map = new HashMap<>(); + // Iterate over array nums and store in map to show its presence by marking as true + for(int i=0;i result = new ArrayList<>(); + for(int i=1;i<=nums.length;++i){ + // if map does not contain any value from array it means it is not present in array + if(!map.containsKey(i)){ + result.add(i); + } + } + return result; + } +} +/** + * Time complexity : O(n) + * Space complexity : O(1) + * Did this code successfully run on Leetcode : yes + * + */ +class Solution { + public List findDisappearedNumbers(int[] nums) { + for(int i=0;i0){ + nums[idx] = - nums[idx]; + } + } + List result = new ArrayList<>(); + for(int i=0;i0){ + result.add(i+1); + } + } + return result; + } +} \ No newline at end of file diff --git a/gameOfLife.java b/gameOfLife.java new file mode 100644 index 00000000..8c9314ce --- /dev/null +++ b/gameOfLife.java @@ -0,0 +1,47 @@ +// Question https://leetcode.com/problems/game-of-life/ +/** + * Time complexity : O(n*m) + * Space complexity : O(1) + * Did this code successfully run on Leetcode : yes + */ +class Solution { + // 0->1 + int[][] dirs; + public void gameOfLife(int[][] board) { + this.dirs = new int[][]{{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}}; + int m = board.length; + int n = board[0].length; + for(int i=0;i3)){ + board[i][j] = 2; // alive will convert into dead + } + } + } + for(int i=0;i=0 && nr=0 && nc