Skip to content

Instantly share code, notes, and snippets.

View Roger7410's full-sized avatar

Roger7410 Roger7410

View GitHub Profile
public class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null || head.next == null) return true;
ListNode slow = head, fast = head;
ListNode cur = head, rev = null;
//Finding mid point with fast slow, while also reversing first half
while(fast != null && fast.next != null){
public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
if(strs.length < 1 || strs == null) return new ArrayList<List<String>>();
Map<String,List<String>> map = new HashMap<>();
for(String s: strs){
char[] chars = s.toCharArray();
Arrays.sort(chars);
String key = String.valueOf(chars);
if(!map.containsKey(key)) map.put(key, new ArrayList<String>());
map.get(key).add(s);
public class Solution {
public boolean wordPattern(String pattern, String str) {
String[] arr= str.split(" ");
HashMap<Character, String> map = new HashMap<Character, String>();
if(arr.length!= pattern.length())
return false;
for(int i=0; i<arr.length; i++){
char c = pattern.charAt(i);
if(map.containsKey(c)){
if(!map.get(c).equals(arr[i]))
//It is a more effictive way than HASHTABLE
public class Solution {
public char findTheDifference(String s, String t) {
char[] ss = s.toCharArray();
char[] tt = t.toCharArray();
char result = 0;
for(int i = 0; i <ss.length; i++){
result ^= ss[i];
result ^= tt[i];
}
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
boolean order = true;
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> tmpList = new ArrayList<>();
public class Solution {
// public List<List<Integer>> levelOrder(TreeNode root) {
// List<List<Integer>> result = new ArrayList<>();
// Queue<TreeNode> queue = new LinkedList<>();
// if(root == null) return result;
// queue.add(root);
// while(!queue.isEmpty()){
// int size = queue.size();
// List<Integer> tmpList = new ArrayList<>();
// for(int i=0; i<size; i++){
public class Solution {
int min = Integer.MAX_VALUE;
int count = 0;
public int minDepth(TreeNode root) {
if(root!=null){
count++;
if(root.left==null && root.right==null){
if(count < min) min = count;
}else{
minDepth(root.left);
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public class Solution {
public int threeSumClosest(int[] nums, int target) {
//each input would have exactly one solution
int result = 0;
Arrays.sort(nums);
//i j k are indexs
int i = 0;
int minCloset = Integer.MAX_VALUE;
while(i < nums.length - 2){
int j = i + 1;
//haha
class Solution{
public List<List<Integer>> threeSum(int[] nums){
List<List<Integer>> results = new ArrayList<>();
if(nums.length < 3) return results;
Arrays.sort(nums);
//i j k are indexs
int i = 0;
while(i < nums.length - 2){
if(nums[i]>0) break;