This file contains hidden or 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
| class Solution { | |
| public int[] singleNumber(int[] nums) { | |
| Set<Integer> set = new HashSet<>(); | |
| for(Integer i: nums){ | |
| if(!set.add(i)){ | |
| set.remove(i); | |
| } | |
| } | |
| int j = 0; |
This file contains hidden or 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
| class Solution { | |
| public List<List<Integer>> zigzagLevelOrder(TreeNode root) { | |
| List<List<Integer>> finalList = new ArrayList(); | |
| if(root == null){ | |
| return finalList; | |
| } | |
| List<TreeNode> list = new ArrayList<>(); |
This file contains hidden or 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
| class Solution { | |
| public ListNode removeElements(ListNode head, int val) { | |
| if (head != null) { | |
| ListNode prev = null; | |
| ListNode node = head; | |
| while (node != null) { | |
| if (node.val == val) { | |
| if (prev == null) { | |
| head = node.next; | |
| } else { |
This file contains hidden or 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
| class Solution { | |
| public int[] topKFrequent(int[] nums, int k) { | |
| if (k == nums.length) { | |
| return nums; | |
| } | |
| Map<Integer, Integer> count = new HashMap(); | |
| for (int n: nums) { |
This file contains hidden or 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
| class Solution { | |
| public double myPow(double x, int n) { | |
| double ans = 1; | |
| if(n<0){ | |
| x = 1/x; | |
| } | |
| while(n!=0){ | |
| if(n%2!=0){ | |
| ans *= x; |
This file contains hidden or 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
| class Solution { | |
| public String reverseWords(String s) { | |
| List<String> list = Arrays.asList(s.split(" ")) | |
| .stream() | |
| .filter(s1 -> !s1.isEmpty()) | |
| .collect(Collectors.toList()); | |
| if (list.size() == 0) { | |
| return s.trim(); | |
| } |
This file contains hidden or 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
| public class Solution { | |
| // you need treat n as an unsigned value | |
| public int reverseBits(int n) { | |
| int ans = 0; | |
| for (int i = 0; i < 32; i++) { | |
| ans <<= 1; | |
| ans = ans | (n & 1); | |
| n >>= 1; | |
| } | |
| return ans; |
This file contains hidden or 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
| public List<List<Integer>> subsets(int[] nums) { | |
| List<List<Integer>> result = new ArrayList(); | |
| result.add(new ArrayList<>()); | |
| for (int num : nums) { | |
| List<List<Integer>> subList = new ArrayList(); | |
| for (List<Integer> current : result) { | |
| List list = new ArrayList<>(current); | |
| list.add(num); | |
| subList.add(list); | |
| } |
This file contains hidden or 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
| public List<List<Integer>> threeSum(int[] nums) { | |
| Map<Integer, Set<Integer>> map = new HashMap(); | |
| List<List<Integer>> threeSumList = new ArrayList(); | |
| Set<Map<Integer, Integer>> threeSumSet = new HashSet<>(); | |
| for (int i = 0; i < nums.length; i++) { | |
| putOnMap(i, nums[i], map); | |
| } | |
| for (int j = 0; j < nums.length; j++) { |
This file contains hidden or 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
| class Solution { | |
| public int islandPerimeter(int[][] grid) { | |
| int p = 0; | |
| for(int j=0; j<grid.length; j++){ | |
| for(int i=0; i<grid[0].length; i++){ | |
| if(grid[j][i] == 1){ | |
| p += dfs(j,i,grid); | |
| } | |
| } | |
| } |
NewerOlder