Last active
July 30, 2018 15:17
-
-
Save TonyRenHK/d4eb38edc1f09d45ec8da61d87f52359 to your computer and use it in GitHub Desktop.
Java Study Note
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
//HashMap: | |
HashMap<Integer, Integer> IntegerMap = new HashMap<Integer, Integer>(); | |
IntegerMap.put(1,9); | |
//For primitive types: | |
int[] myIntArray = new int[3]; | |
int[] myIntArray = {1,2,3}; | |
int[] myIntArray = new int[]{1,2,3}; | |
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
import java.io.*; | |
import java.util.*; | |
// 15. 3Sum | |
/* https://leetcode.com/problems/3sum/description/ | |
Given array nums = [-1, 0, 1, 2, -1, -4], | |
A solution set is: | |
[ | |
[-1, 0, 1], | |
[-1, -1, 2] | |
] | |
*/ | |
class Solution { | |
public static List<List<Integer>> threeSum(int[] nums) { | |
List<List<Integer>> ReturnList = new ArrayList<List<Integer>>(); | |
Set<String> UniqueSet = new HashSet<String>(); | |
Arrays.sort(nums); | |
System.out.println(nums.length); | |
for(int i =0; i <nums.length; i++){ | |
System.out.println("current Number = " + nums[i]); | |
int CurrentNum = 0- nums[i] ; | |
int left = (i==(nums.length-1)) ? nums.length-1: i+1; | |
int right = nums.length-1; | |
System.out.println("left:" + left + ", right :" +right ); | |
while(left < right){ | |
int sumNum = nums[left] + nums[right]; | |
if( sumNum == CurrentNum){ | |
String KeyStr = String.valueOf(nums[i]) | |
+ String.valueOf(nums[left]) | |
+String.valueOf(nums[right]); | |
//System.out.println("KeyStr : "+ KeyStr); | |
//System.out.println("find" + nums[i]+","+ nums[left] +","+ nums[right]); | |
// find | |
if(!UniqueSet.contains(KeyStr)){ | |
ReturnList.add(Arrays.asList(nums[i], nums[left] , nums[right])); | |
UniqueSet.add(KeyStr); | |
} | |
left++; | |
}else if(sumNum >CurrentNum){ | |
right--; | |
}else if(sumNum <CurrentNum){ | |
left++; | |
} | |
} | |
} | |
return ReturnList; | |
} | |
public static void main(String[] args) { | |
//int[] InputArray = new int[]{-1, 0, 1, 2, -1, -4}; | |
int[] InputArray = new int[]{-10,5,-11,-15,7,-7,-10,-8,-3,13,9,-14,4,3,5,-7,13,1,-4,-11,5,9,-11,-4,14,0,3,-10,-3,-7,10,-5,13,14,-5,6,14,0,5,-12,-10,-1,-11,9,9,1,-13,0,-13,-1,4,0,-7,8,3,14,-15,-9,-10,-3,0,-15,-1,-2,6,9,11,6,-14,1,1,-9,-14,6,7,10,14,2,-13,-13,8,6,-6,8,-9,12,7,-9,-11,4,-4,-4,4,10,1,-12,-3,-2,1,-10,6,-13,-3,-1,0,11,-5,0,-2,-11,-6,-9,11,3,14,-13,0,7,-14,-4,-4,-11,-1,8,6,8,3 }; | |
// | |
System.out.println( threeSum(InputArray) ); | |
System.out.println("****** end ******" ); | |
} | |
} | |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
import java.lang.*; | |
/* | |
https://leetcode.com/problems/trapping-rain-water/solution/ | |
*/ | |
public class Solution { | |
public static int trap(int[] height) { | |
int ans = 0; | |
int size = height.length; | |
for (int i = 1; i < size - 1; i++) { | |
int max_left = 0, max_right = 0; | |
for (int j = i; j >= 0; j--) { | |
//Search the left part for max bar size | |
System.out.println( "max_left : "+max_left + | |
" j :" +j ); | |
max_left = Math.max(max_left, height[j]); | |
} | |
for (int j = i; j < size; j++) { | |
//Search the right part for max bar size | |
max_right = Math.max(max_right, height[j]); | |
} | |
System.out.println( "max_left : "+max_left + | |
" max_right :" +max_right ); | |
ans += Math.min(max_left, max_right) - height[i]; | |
} | |
return ans; | |
} | |
public static void main(String[] args) { | |
try { | |
int[] nums={2,0,2,1,1,0}; | |
int[] arr = {0,1,0,2,1,0,1,3,2,1,2,1}; | |
System.out.println( "Final : "+trap(arr) ); | |
System.out.println("*▶☆✡ ✦ ✧ ✩ ✪ ✫ ✬ ✭ ✮ ✯ ✰ ⁂" ); | |
} catch (Exception e) { | |
} | |
} | |
} | |
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
import java.io.*; | |
import java.util.*; | |
/* 46. Permutations | |
https://leetcode.com/problems/permutations/description/ | |
*/ | |
class Solution { | |
public static List<List<Integer>> permute2(int[] arr) { | |
System.out.println("****** start ******" ); | |
List<List<Integer>> res = new ArrayList<List<Integer>>(); | |
//corner | |
if ( arr == null || arr.length == 0 ){ | |
return res; | |
} | |
//core | |
List<Integer> curList = new ArrayList<>(); | |
helper(res, curList, arr); | |
return res; | |
} | |
public static void helper(List<List<Integer>> res, | |
List<Integer> curList, int[] arr) | |
{ | |
System.out.println( "***** new enter *****" ); | |
//System.out.println( arr ); | |
//base | |
if ( curList.size() == arr.length ){ | |
res.add(new ArrayList<Integer>(curList)); | |
return ; | |
} | |
//current | |
for ( int i = 0; i < arr.length; i++ ){ | |
if ( curList.contains(arr[i]) ){ | |
continue; | |
} | |
curList.add(arr[i]); | |
System.out.println( arr[i] + "**" +curList.toString() ); | |
//next | |
helper(res, curList, arr); | |
curList.remove(curList.size() - 1); | |
} | |
System.out.println("************"); | |
} | |
public static List<List<Integer>> permute(int[] nums) { | |
List<List<Integer>> res = new ArrayList<>(); | |
List<Integer> first = new ArrayList<>(); | |
first.add(nums[0]); | |
res.add(first); | |
for(int i = 1; i < nums.length; i++) { | |
System.out.println("current nums[i] : "+nums[i]); | |
List<List<Integer>> newRes = new ArrayList<>(); | |
for(List<Integer> temp : res) { | |
int size = temp.size() + 1; | |
System.out.println(temp + "size : "+nums[i]); | |
for(int j = 0; j < size; j++) { | |
List<Integer> item = new ArrayList<>(temp); | |
item.add(j, nums[i]); | |
System.out.println( item ); | |
newRes.add(item); | |
} | |
} | |
res = newRes; | |
} | |
System.out.println("##################"); | |
return res; | |
} | |
public static void main(String[] args) { | |
int[] nums={ 1,2,3 }; | |
//System.out.println( permute(nums) ); | |
List<Integer> first = new ArrayList<>(); | |
first.add(1); | |
List<Integer> item = new ArrayList<>(first); | |
item.add(0, 3); | |
System.out.println( item ); | |
System.out.println("****** end ******" ); | |
} | |
} |
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
import java.io.*; | |
import java.util.*; | |
/* 841. Keys and Rooms | |
Input: [[1,3],[3,0,1],[2],[0]] | |
Output: false | |
Explanation: We can't enter the room with number 2. | |
*/ | |
class Solution { | |
public static boolean canVisitAllRooms(List<List<Integer>> rooms) { | |
//System.out.println( rooms.size() ); | |
boolean[] arrFlag = new boolean[rooms.size()]; | |
Stack<Integer> NewStack = new Stack<Integer>(); | |
NewStack.push(0); | |
arrFlag[0] = true; | |
while(!NewStack.empty()){ | |
Integer NewIndex = NewStack.pop(); | |
//using get | |
List<Integer> CurrentList = rooms.get(NewIndex); | |
for(int i=0; i<CurrentList.size();i++ ){ | |
if( arrFlag[CurrentList.get(i)] != true){ | |
System.out.println("Push : "+ CurrentList.get(i) ); | |
arrFlag[CurrentList.get(i)] = true; | |
NewStack.push(CurrentList.get(i)); | |
//first time enter | |
} | |
} | |
} | |
Boolean Alltrue= true; | |
for(boolean Flag: arrFlag){ | |
System.out.println(" flag : "+ Flag ); | |
if(!Flag){ | |
Alltrue = false; | |
} | |
} | |
return Alltrue; | |
} | |
public static void main(String[] args) { | |
List<List<Integer>> Inputlist = new ArrayList<List<Integer>>(); | |
Inputlist.add(Arrays.asList(1,3)); | |
Inputlist.add(Arrays.asList(3,0,1)); | |
Inputlist.add(Arrays.asList(2)); | |
Inputlist.add(Arrays.asList(0)); | |
System.out.println( canVisitAllRooms( Inputlist ) ); | |
System.out.println("****** end ******" ); | |
} | |
} |
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
import java.io.*; | |
import java.util.*; | |
/* 91. Decode Ways | |
https://leetcode.com/problems/decode-ways/description/ | |
*/ | |
class Solution { | |
public static int numDecodings(String s) { | |
if(s == null || s.length() == 0) { | |
return 0; | |
} | |
int n = s.length(); | |
System.out.println("n Length : " + n ); | |
int[] dp = new int[n+1]; | |
dp[0] = 1; | |
dp[1] = s.charAt(0) != '0' ? 1 : 0; | |
for(int i = 2; i <= n; i++) { | |
// 2 3 | |
int first = Integer.valueOf(s.substring(i-1, i)); | |
int second = Integer.valueOf(s.substring(i-2, i)); | |
System.out.println(" i : "+ i + " first : " + first +" second :"+second); | |
if(first >= 1 && first <= 9) { | |
dp[i] += dp[i-1]; | |
int m = i-1; | |
System.out.println("dp["+ i +"] += dp["+m+"];" + "dp["+m+"] = "+dp[i-1] ); | |
} | |
if(second >= 10 && second <= 26) { | |
dp[i] += dp[i-2]; | |
int m = i-2; | |
System.out.println("dp["+ i +"] += dp["+m+"];" + "dp["+m+"] = "+dp[i-2] ); | |
} | |
System.out.println(" $$$$$$$$$$$$$$$$$ " ); | |
} | |
System.out.println(" dp[1] : " +dp[1]); | |
System.out.println(" dp[2] : " +dp[2]); | |
return dp[n]; | |
} | |
public static void main(String[] args) { | |
System.out.println( numDecodings("226") ); | |
System.out.println("****** end ******" ); | |
} | |
} |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
/* | |
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. | |
You may assume no duplicates in the array. | |
Here are few examples. | |
[1,3,5,6], 5 -> 2 | |
[1,3,5,6], 2 -> 1 | |
[1,3,5,6], 7 -> 4 | |
[1,3,5,6], 0 -> 0 | |
https://github.com/leetcoders/LeetCode-Java/blob/master/SearchInsertPosition.java | |
*/ | |
public class Solution { | |
public static int BinarySearch(int arr[],int lengthNumber, int targetNumber){ | |
int max = lengthNumber; | |
int min = 0; | |
while( min < lengthNumber ) { | |
int mid = (max+min)/2; | |
// case 1: found | |
if(arr[mid]==targetNumber){ | |
return mid; | |
} | |
//case 2 large | |
if(arr[mid] > targetNumber){ | |
max=mid-1; | |
}else{ | |
min= mid+1; | |
} | |
//case 3small | |
} | |
return min; | |
} | |
public static void main(String[] args) { | |
try { | |
int TempArr[] = {1,3,5,6}; | |
System.out.println( BinarySearch(TempArr,TempArr.length,2) ); | |
System.out.println("***********************" ); | |
} catch (Exception e) { | |
} | |
} | |
} | |
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
import java.io.*; | |
import java.util.*; | |
class Solution { | |
public static void main(String[] args) { | |
String[] InputList = {"eat", "tea", "tan", "ate", "nat", "bat"}; | |
for (String s : InputList) { | |
for (char c : s.toCharArray()) { | |
// change char to int | |
System.out.println( c-'a' ); | |
} | |
} | |
StringBuilder sb=new StringBuilder("Hello "); | |
sb.append("Java");//now original string is changed | |
sb.append("gigigi"); | |
sb.append("sssqwqrefds"); | |
System.out.println(sb); | |
System.out.println("****** end ******"); | |
} | |
} |
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
import java.io.*; | |
import java.util.*; | |
// 518. Coin Change 2 | |
//https://leetcode.com/problems/coin-change-2/description/ | |
/* | |
Input: amount = 5, coins = [1, 2, 5] | |
Output: 4 | |
*/ | |
class Solution { | |
public static int change(int amount, int[] coins) { | |
List<Integer> AmountArray = new ArrayList<Integer>(Collections.nCopies(amount+1, 0)); | |
for(int i=1; i <AmountArray.size(); i++){ | |
//AmountArray[i] = Integer.MIN_VALUE; | |
AmountArray.set( i, Integer.MIN_VALUE); | |
} | |
System.out.println("****** 0000 ******" ); | |
for(int i=0; i <AmountArray.size(); i++){ | |
System.out.println("i : "+i+" "+AmountArray.get(i)); | |
} | |
for(int i=1; i <AmountArray.size(); i++){ | |
System.out.println("current amount i : "+i+" "+AmountArray.get(i)); | |
Boolean Have0Case = false; | |
for(int j=0;j<coins.length ;j++){ | |
if(coins[j] <= i){ | |
int diff = i - coins[j]; | |
int previousValue =AmountArray.get(diff); | |
if(diff==0){ | |
Have0Case= true; | |
} | |
//previousValue = previousValue+1; | |
System.out.println(i +"-"+ coins[j]+" = diff=" +diff + | |
"then : AmountArray.get("+diff+ ") :"+ AmountArray.get(diff) ); | |
if(previousValue > AmountArray.get(i) ){ | |
//Find max | |
AmountArray.set(i,previousValue); | |
} | |
} | |
} | |
if(Have0Case){ | |
AmountArray.set(i,AmountArray.get(i) +1); | |
} | |
System.out.println("max value "+ AmountArray.get(i) ); | |
} | |
System.out.println("****** 888 ******" ); | |
for(int i=0; i <AmountArray.size(); i++){ | |
System.out.println("i : "+i+" = "+AmountArray.get(i)); | |
} | |
return 0; | |
} | |
public static int change2(int amount, int[] coins) { | |
int[] dp = new int[amount + 1]; | |
dp[0] = 1; | |
for (int coin : coins){ | |
System.out.println("current coin : " + coin ); | |
for (int i = 0; i <= amount - coin; ++i){ | |
int temp =i + coin; | |
dp[i + coin] += dp[i]; | |
System.out.println("dp["+temp+"] += dp["+i+"]; Then: dp["+i+"]=" + dp[i] | |
+ ",dp["+temp+"] = "+dp[i + coin] ); | |
//System.out.println("i + coin : " + temp +",dp[i:"+i+"] =" + dp[i] | |
// + ",dp["+temp+"] = "+dp[i + coin] ); | |
} | |
System.out.println("✪✪✪✪✪✪✪✪✪✪✪✪✪✪" ); | |
} | |
return dp[amount]; | |
} | |
public static void main(String[] args) { | |
int[] coinsArray = new int[]{1, 2, 5}; | |
System.out.println( change2(5,coinsArray) ); | |
System.out.println("****** end ******" ); | |
int x = 60984; | |
int y = 1000; | |
// print the larger number between x and y | |
//System.out.println("Math.max(" + x + "," + y + ")=" + Math.max(x, y)); | |
} | |
} |
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
import java.io.*; | |
import java.util.*; | |
// 322. Coin Change Dynamic programming | |
//https://leetcode.com/problems/coin-change/solution/ | |
class Solution { | |
public static int coinChange(int[] coins, int amount) { | |
int[] dp = new int[amount + 1]; | |
// dp length is 12 | |
//System.out.println(coins.length ); | |
for (int i = 1; i <= amount; i++) {// i from 1-12 | |
dp[i] = Integer.MAX_VALUE; | |
System.out.println("current amount i is " + i ); | |
for (int j = 0; j < coins.length; j++){ | |
// coins length is 3 | |
//System.out.println("current count is " + coins[j]); | |
if (i >= coins[j] && dp[i - coins[j]] != Integer.MAX_VALUE){ | |
int dex = i - coins[j]; | |
// current Amount - current coin | |
System.out.println("current count is " + coins[j] +" current dp is " + | |
dp[i] + " and " + | |
" dp["+dex+"] : " +dp[i - coins[j]] ); | |
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); | |
// why need to add 1, including this time | |
} | |
} | |
System.out.println("dp["+i+"]=" + dp[i] ); | |
System.out.println("▶▶▶▶▶▶▶▶▶▶▶▶▶▶▶▶▶▶▶▶▶▶▶"); | |
} | |
System.out.println("****** #### ******" ); | |
for(int m=0; m<dp.length;m++){ | |
System.out.println(m+" : "+ dp[m] ); | |
} | |
System.out.println("****** #### ******" ); | |
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount]; | |
} | |
public static void main(String[] args) { | |
int[] coinsArray = new int[]{1, 2, 5}; | |
System.out.println( coinChange(coinsArray,11) ); | |
System.out.println("****** end ******" ); | |
} | |
} |
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
import java.io.*; | |
import java.util.*; | |
/* | |
Java Hashtable class | |
*/ | |
class Solution { | |
public static void main(String[] args) { | |
Hashtable<Integer,String> hm=new Hashtable<Integer,String>(); | |
hm.put(100,"Amit"); | |
hm.put(102,"Ravi"); | |
hm.put(101,"Vijay"); | |
hm.put(101,"GiiGi"); | |
hm.put(103,"Rahul"); | |
System.out.println("Values now 1: "+ hm); | |
for(Map.Entry<Integer,String> m:hm.entrySet()){ | |
System.out.println(m.getKey()+" "+m.getValue()); | |
System.out.println( hm.get(m.getKey()) ); | |
System.out.println("**********"); | |
} | |
} | |
} |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
/* | |
* To execute Java, please define "static void main" on a class | |
* named Solution. | |
* | |
* If you need more classes, simply define them inline. | |
*/ | |
class Solution { | |
private static String reverse(String str, StringBuffer sb) { | |
if(str.length()==0) { | |
return sb.toString(); | |
} | |
sb.append(str.charAt(str.length()-1)); | |
return reverse(str.substring(0, str.length()-1), sb); | |
} | |
/* | |
public static void main(String[] args) { | |
StringBuffer sb = new StringBuffer(); | |
System.out.println(reverse("h12345dyedd67a89", sb)); | |
String s = "Strings are immutable"; | |
char result = s.charAt(8); | |
System.out.println(s[8]); | |
} | |
*/ | |
public static void main(String []args) { | |
try{ | |
Stack<String> NewStack = new Stack<String>(); | |
NewStack.push("Test111"); | |
System.out.println( NewStack); | |
String NewString = NewStack.pop(); | |
System.out.println(NewString); | |
}catch(Exception e){ | |
} | |
} | |
} | |
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
import java.io.*; | |
import java.util.*; | |
/* | |
Java 49. Group Anagrams | |
Input: ["eat", "tea", "tan", "ate", "nat", "bat"], | |
Output: | |
[ | |
["ate","eat","tea"], | |
["nat","tan"], | |
["bat"] | |
] | |
*/ | |
class Solution { | |
public static List<List<String>> groupAnagrams(String[] strs) { | |
List<List<String>> ReturnList = new ArrayList<>(); | |
HashMap<String,List<String>> MappingTable =new HashMap<String,List<String>>(); | |
for(int i=0;i< strs.length ;i++){ | |
//System.out.println("strs : " + strs[i]); | |
String CurrentString = strs[i]; | |
ArrayList<String> listofChar = new ArrayList<String>(); | |
for( int j =0; j< CurrentString.length(); j++ ){ | |
String str = Character.toString(CurrentString.charAt(j) );// (String) CurrentString.charAt(j); | |
listofChar.add(str); | |
} | |
Collections.sort(listofChar); | |
String keyStr = String.join("", listofChar); | |
//System.out.println( strs[i] +" keyStr: " + keyStr); | |
if(MappingTable.containsKey(keyStr) ){ | |
MappingTable.get(keyStr).add(strs[i]); | |
}else{ | |
List<String> Newlist = new ArrayList<>(); | |
Newlist.add(strs[i]); | |
MappingTable.put( keyStr, Newlist); | |
} | |
} | |
for(Map.Entry<String,List<String>> m:MappingTable.entrySet()){ | |
System.out.println(m.getKey()+" "+m.getValue()); | |
List<String> TempList = m.getValue(); | |
ReturnList.add(TempList); | |
//System.out.println( hm.get(m.getKey()) ); | |
System.out.println("**********"); | |
} | |
return ReturnList; | |
} | |
public static void main(String[] args) { | |
String[] InputList = {"eat", "tea", "tan", "ate", "nat", "bat"}; | |
List<List<String>> OutputList = groupAnagrams(InputList); | |
System.out.println("****** end ******"); | |
} | |
} |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
public class Solution { | |
public static class NewObject{ | |
public int Data; | |
public NewObject(){ | |
Data=3; | |
} | |
public void SetNum( int InputNum){ | |
Data=InputNum; | |
} | |
} | |
public static class LinkedList { | |
Node head; | |
} | |
public static class Node { | |
public int data; | |
public Node Next; | |
public Node(int d){ | |
data = d; | |
Next= null; | |
} | |
public void SetNextNode(Node NewNode){ | |
Next = NewNode; | |
} | |
} | |
public static void main(String[] args) { | |
try { | |
LinkedList llist = new LinkedList(); | |
llist.head = new Node(1); | |
Node second = new Node(2); | |
Node third = new Node(3); | |
//second.next = third; | |
second.SetNextNode(third); | |
llist.head.SetNextNode(second); | |
NewObject newObj = new NewObject (); | |
System.out.println("***********************"+newObj.Data); | |
NewObject newObj2 = new NewObject (); | |
newObj2.SetNum(33); | |
System.out.println(newObj.Data +"***********************"+newObj2.Data); | |
} catch (Exception e) { | |
} | |
} | |
} | |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
/* | |
* To execute Java, please define "static void main" on a class | |
* named Solution. | |
* | |
* If you need more classes, simply define them inline. | |
*/ | |
class Solution { | |
public static void main(String[] args) { | |
try { | |
//queue | |
PriorityQueue < String > queue = new PriorityQueue < String > (); | |
queue.add("Amit"); | |
queue.add("Vijay"); | |
queue.add("Karan"); | |
queue.add("Jai"); | |
queue.add("Rahul"); | |
System.out.println("head:" + queue.element()); | |
//System.out.println("head:" + queue.peek()); | |
//System.out.println("iterating the queue elements:"); | |
queue.remove(); | |
System.out.println("head:" + queue.element()); | |
queue.remove(); | |
System.out.println("head:" + queue.element()); | |
queue.remove(); | |
System.out.println("head1:" + queue.element()); | |
queue.remove(); | |
System.out.println("head2:" + queue.poll()); | |
queue.remove(); | |
System.out.println("head:" + queue.peek()); | |
if(queue.peek() == null ){ | |
System.out.println("null of queue.poll() " ); | |
} | |
} catch (Exception e) { | |
} | |
} | |
} |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
class Solution { | |
public static void main(String[] args) { | |
try { | |
//queue | |
//http://www.codejava.net/java-core/collections/java-queue-collection-tutorial-and-examples | |
Queue<String> queueNames = new PriorityQueue<>(); | |
queueNames.add("Dale"); | |
queueNames.add("Bob"); | |
queueNames.add("Frank"); | |
queueNames.add("Alice"); | |
queueNames.add("Eric"); | |
queueNames.add("Cole"); | |
queueNames.add("John"); | |
queueNames.forEach(name -> System.out.println(name)); | |
System.out.println("**********"); | |
Queue<String> queueNames2 = new LinkedList<>(); | |
queueNames2.add("Dale"); | |
queueNames2.add("Bob"); | |
queueNames2.add("Frank"); | |
queueNames2.add("Alice"); | |
queueNames2.add("Eric"); | |
queueNames2.add("Cole"); | |
queueNames2.add("John"); | |
for (String name : queueNames2) { | |
System.out.println(name); | |
} | |
} catch (Exception e) { | |
} | |
} | |
} |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
class Solution { | |
public static void main(String[] args) { | |
try { | |
// create a linked list | |
//LinkedList ll = new LinkedList<>(); | |
LinkedList<String> ll = new LinkedList<>(); | |
// add elements to the linked list | |
ll.add("F"); | |
ll.add("B"); | |
ll.add("D"); | |
ll.add("E"); | |
ll.add("C"); | |
ll.addLast("Z"); | |
ll.addFirst("A"); | |
ll.add(1, "A2"); | |
System.out.println("Original contents of ll: " + ll); | |
// remove elements from the linked list | |
ll.remove("F"); | |
ll.remove(2); | |
System.out.println("Contents of ll after deletion: " + ll); | |
// remove first and last elements | |
ll.removeFirst(); | |
ll.removeLast(); | |
System.out.println("ll after deleting first and last: " + ll); | |
// get and set a value | |
Object val = ll.get(2); | |
ll.set(2, (String) val + " Changed"); | |
System.out.println("ll after change: " + ll); | |
} catch (Exception e) { | |
} | |
} | |
} |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
/* | |
* To execute Java, please define "static void main" on a class | |
* named Solution. | |
* | |
* If you need more classes, simply define them inline. | |
*/ | |
class Solution { | |
private static String reverse(String str, StringBuffer sb) { | |
if(str.length()==0) { | |
return sb.toString(); | |
} | |
sb.append(str.charAt(str.length()-1)); | |
return reverse(str.substring(0, str.length()-1), sb); | |
} | |
/* | |
public static void main(String[] args) { | |
StringBuffer sb = new StringBuffer(); | |
System.out.println(reverse("h12345dyedd67a89", sb)); | |
String s = "Strings are immutable"; | |
char result = s.charAt(8); | |
System.out.println(s[8]); | |
} | |
*/ | |
public static void main(String []args) { | |
try{ | |
Stack<String> NewStack = new Stack<String>(); | |
NewStack.push("Test111"); | |
System.out.println( NewStack); | |
String NewString = NewStack.pop(); | |
System.out.println(NewString); | |
}catch(Exception e){ | |
} | |
} | |
} | |
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
import java.io.*; | |
import java.util.*; | |
/* | |
Java join Collections.sort | |
*/ | |
class Solution { | |
public static void main(String[] args) { | |
String str= "adc"; | |
for(var i=0;i<str.length();i++){ | |
int ascii = (int) str.charAt(i); | |
System.out.println(ascii+" || "+ str.charAt(i) ); | |
} | |
ArrayList<String> listofcountries = new ArrayList<String>(); | |
listofcountries.add("India"); | |
listofcountries.add("US"); | |
listofcountries.add("China"); | |
listofcountries.add("Denmark"); | |
/*Unsorted List*/ | |
System.out.println("Before Sorting:"); | |
for(String counter: listofcountries){ | |
System.out.println(counter); | |
} | |
System.out.println("*********************"); | |
/* Sort statement*/ | |
Collections.sort(listofcountries); | |
/* Sorted List*/ | |
System.out.println("After Sorting:"); | |
for(String counter: listofcountries){ | |
System.out.println(counter); | |
} | |
System.out.println("*********************"); | |
//Join | |
String joined = String.join(" and ", listofcountries); | |
System.out.println(joined); | |
List<String> list = Arrays.asList("foo", "bar", "baz"); | |
String joined2 = String.join(" and ", list); | |
// initializing unsorted int array | |
int iArr[] = {2, 1, 9, 6, 4}; | |
// let us print all the elements available in list | |
for (int number : iArr) { | |
System.out.println("Number = " + number); | |
} | |
// sorting array | |
Arrays.sort(iArr); | |
// let us print all the elements available in list | |
System.out.println("The sorted int array is:"); | |
for (int number : iArr) { | |
System.out.println("Number = " + number); | |
} | |
} | |
} |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
public class Solution { | |
public void sort(int arr[]){ | |
int ArrayLength = arr.length; | |
System.out.println("length : "+ArrayLength); | |
for(int i=0; i<ArrayLength; i++){ | |
for(int j=i+1; j<ArrayLength;j++){ | |
if(arr[i]>arr[j]){ | |
int temp = arr[j]; | |
arr[j]=arr[i]; | |
arr[i]=temp; | |
} | |
} | |
} | |
} | |
public void PrintArray(int arr[]){ | |
for(int i=0; i<arr.length; i++){ | |
System.out.println(arr[i]); | |
} | |
} | |
public static void main(String[] args) { | |
try { | |
Solution ob = new Solution(); | |
int arr[]={20,50,25,12,22,99,1,50}; | |
ob.sort(arr); | |
ob.PrintArray(arr); | |
} catch (Exception e) { | |
} | |
} | |
} |
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
int[] arr = {13, 7, 6, 45, 21, 9, 101, 102}; | |
Arrays.sort(nums); | |
System.out.printf("Modified arr[] : %s", | |
Arrays.toString(nums)); |
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
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.*; | |
/* | |
Given nums = [2, 7, 11, 15], target = 9, | |
Because nums[0] + nums[1] = 2 + 7 = 9, | |
return [0, 1]. | |
*/ | |
public class Solution { | |
public static int[] twoSum(int[] nums, int target) { | |
int[] returnArray = new int[2]; | |
HashMap<Integer, Integer> IntegerMap = new HashMap<Integer, Integer>(); | |
for(int i=0;i<nums.length;i++){ | |
System.out.println("***********************" + nums[i]); | |
if(IntegerMap.containsKey(nums[i])){ | |
returnArray=new int[] {IntegerMap.get(nums[i]),i}; | |
}else{ | |
IntegerMap.put(target-nums[i],i); | |
} | |
} | |
return returnArray; | |
} | |
public static void main(String[] args) { | |
try { | |
int[] nums={2, 7, 11, 15}; | |
int target = 9; | |
int[] returnArray = twoSum(nums,target); | |
System.out.println(returnArray[0]+"***********************" +returnArray[1] ); | |
} catch (Exception e) { | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment