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
static int binarySearchIterative(int[] a, int key) { | |
int low = 0; | |
int high = a.length - 1; | |
while(low <= high) { | |
int mid = (low + high) / 2; | |
if(key == a[mid]) | |
return mid; | |
else if (key < a[mid]) |
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
import java.util.Scanner; | |
class InPlaceRev{ | |
public static void main(String args[]){ | |
Scanner sc = new Scanner(System.in); | |
String str = sc.nextLine(); | |
StringBuilder sbAns = new StringBuilder(); | |
int i = 0; | |
while(i < str.length()){ | |
StringBuilder sbRev = new StringBuilder(); |
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
import java.util.HashMap; | |
import java.util.Random; | |
/** | |
* Created by shail on 03/04/17. | |
*/ | |
public class Roman { | |
public static int toArabic(String roman) { | |
HashMap<Character, Integer> hm = new HashMap(); | |
hm.put('M', 1000); |
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
/** | |
* Created by shail on 03/04/17. | |
*/ | |
public class Likes { | |
public static String likes1(String[] people){ | |
if(people.length == 0) | |
return "No one likes this"; | |
else if(people.length == 1) | |
return people[0] + " likes this"; |
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 int maxSubArray(int[] nums) { | |
if(nums.length == 0) return 0; | |
int localMax = nums[0]; | |
int globalMax = localMax; | |
for(int i = 1; i < nums.length; i++) { | |
localMax = Math.max(nums[i], localMax + nums[i]); | |
globalMax = Math.max(localMax, globalMax); | |
} |
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
/** | |
* Created by shail on 4/10/17. | |
*/ | |
class MatRotate { | |
static void toString(int[][]a, String message) { | |
System.out.println(message); | |
for(int i = 0; i < a.length; i++){ | |
for(int j = 0; j < a[0].length; j++) | |
System.out.print(a[i][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
/** | |
* Created by shail on 4/11/17. | |
*/ | |
public class Substring { | |
static boolean isStartsWith(int start, String s1, String s2){ | |
int n = s2.length(); | |
for(int i1 = start, i2 = 0; i2 < n; i1++, i2++) | |
if(s1.charAt(i1) != s2.charAt(i2)) | |
return false; |
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 WordReverse { | |
static String reverse(String str){ | |
char[] word = str.toCharArray(); | |
for(int i = 0; i < word.length/2; i++){ | |
char temp = word[i]; | |
word[i] = word[word.length-1-i]; | |
word[word.length-1-i] = temp; | |
} |
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
import java.util.Comparator; | |
import java.util.Arrays; | |
class Sorting { | |
public static void main(String[] args) { | |
Person p1 = new Person("Chagan", 145, 49); | |
Person p2 = new Person("Lagan", 150, 49); | |
Person p3 = new Person("Magan", 154, 22); | |
Person p4 = new Person("Gagan", 155, 34); | |
Person[] people = new Person[]{p1, p2, p3, p4}; |
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 Stairs { | |
static HashMap<Integer, Integer> hm; | |
public int climbStairs(int n) { | |
hm = new HashMap<>(); | |
hm.put(0, 1); | |
return helper(n); | |
} | |
OlderNewer