CJ
BAN USER
public class Solution {
static public void main(String[] args) {
String a = "29023902397403472034", b = "8372283473748173";
String out = AddVeryLargeNumbers(a, b);
System.out.println(out);
}
static String AddVeryLargeNumbers(String a, String b) {
StringBuilder out = new StringBuilder();
int max_len = a.length() >= b.length()? a.length() : b.length();
int i = a.length() - 1, j = b.length()-1;
int carry = 0;
while (max_len > 0) {
int tmp = 0;
if (i >= 0)
tmp += (int) a.charAt(i) - '0';
if (j >= 0)
tmp += (int) b.charAt(j) - '0';
if (carry == 1) {
tmp += carry;
}
// handling carry
if (tmp >= 10) {
carry = 1;
out.insert(0, tmp-10);
} else {
carry = 0;
out.insert(0, tmp);
}
i--;
j--;
max_len--;
}
if (carry == 1)
out.insert(0, String.valueOf(1));
return out.toString();
}
}
public class Main {
public static void main(String[] args) {
// test set
// String[] arr = {"abcd", "cbad", "bacd"};
String[] arr = {"abcd", "acbd", "adcb", "cdba", "bcda", "badc"};
// convert ArrayList form as it is beneficial to handle
ArrayList<String> input = new ArrayList<String>();
for (int i = 0; i <arr.length; i++) {
input.add(i, arr[i]);
}
// main loop
for (int i = 0; i < input.size()-1; i++) {
for (int j = i+1; j < input.size(); j++) {
// Question here: any possibility that identical strings exist? (assuming no for now)
// check with swapEven
if (input.get(i).equals(swapEven(input.get(j)))) {
// remove if equivalent
input.remove(j);
j--;
// go back to loop
continue;
}
// check with swapOdd
if (input.get(i).equals(swapOdd(input.get(j)))) {
// remove if equivalent
input.remove(j);
j--;
// go back to loop (no need as this is end of loop)
//continue;
}
}
}
System.out.println("length: " + input.size());
}
// swap a character at even-numbered index
// assuming string length is 4
static String swapEven(String in) {
char data[] = {in.charAt(2), in.charAt(1), in.charAt(0), in.charAt(3)};
String out = new String(data);
return out;
}
// swap a character at odd-numbered index
// assuming string length is 4
static String swapOdd(String in) {
char data[] = {in.charAt(0), in.charAt(3), in.charAt(2), in.charAt(1)};
String out = new String(data);
return out;
}
}
public class AverageStream {
private long[] stream;
private double average = 0;
// constructor
AverageStream(long[] input) {
stream = input;
}
// calculate average
void setAverage() {
long sum = 0;
for (int i = 0; i < stream.length; i++) {
// in case of overflow, set zero
if (isOverflow(sum)) {
sum = 0;
average = 0;
return;
}
sum += stream[i];
}
average = (sum/stream.length);
}
double getAverage() {
return average;
}
boolean isOverflow(long in) {
if (in >= -0x7FFFFFFF && in <= 0x7FFFFFFF)
return false;
else
return true;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
// input setup
List<int[]> list = new ArrayList<int[]>();
list.add(new int[]{2,3});
list.add(new int[]{2,4});
list.add(new int[]{5,6});
list.add(new int[]{8,6});
list.add(new int[]{1,0});
// try TreeMap as it supports sort in ascending order
// distance and point
TreeMap<Integer, int[]> inputs = new TreeMap<Integer, int[]>();
// for distance, we only need relative length, so root is not required.
for (int i = 0; i < list.size(); i++) {
inputs.put(list.get(i)[0]*list.get(i)[0] + list.get(i)[1]*list.get(i)[1], list.get(i));
}
// for a given 'n', return relevant elements from the list
int n = 3;
ArrayList<int[]> out = new ArrayList<int[]>();
for (int i = 0; i < n; i++) {
out.add(list.get(i));
}
// check
for (int i = 0; i < out.size(); i++) {
System.out.println(out.get(i)[0] + ", " + out.get(i)[1]);
}
}
}
A code to support from 1 to 9999
public class ConvertInteger2EnglishPhrase {
public static void main(String[] args) {
String[] ones = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
String[] teens = {"", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen"};
String[] tens = {"", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninty"};
String[] hundreds = new String[10];
for (int i = 0; i < 10; i++) {
hundreds[i] = ones[i] + " " + "Hundred";
}
String[] thousands = new String[10];
for (int i = 0; i < 10; i++) {
thousands[i] = ones[i] + " " + "Thousand";
}
String[][] numbers = {ones, tens, hundreds, thousands};
int input = 117;
String out = "";
int len;
int divider = 1;
for (int j = 1; j <= 9999; j++) {
input = j;
out = "";
while (input > 0) {
if (input > 0 && input < 10) {
out += " " + ones[input % 10];
break;
} else if (input > 10 && input < 20) {
out += " " + teens[input % 10];
break;
} else {
// get # of digit
len = String.valueOf(input).length();
divider = 1;
for (int i = 0; i < (len - 1); i++)
divider *= 10;
int quotient = input / divider;
out += " " + numbers[len - 1][quotient];
input -= divider * quotient;
}
}
System.out.println(out);
}
}
}
public class FinaCelebrity {
public static void main(String[] args) {
// initialize Person instances assuming Tom is the celebrity
// John
Person john = new Person("John");
john.friendsList.add("Eric");
john.friendsList.add("Kevin");
john.friendsList.add("Tom");
john.friendsList.add("Joey");
// Eric
Person eric = new Person("Eric");
eric.friendsList.add("John");
eric.friendsList.add("BM");
eric.friendsList.add("Tom");
// Jeff
Person jeff = new Person("Jeff");
jeff.friendsList.add("Amy");
jeff.friendsList.add("Tom");
// Tom (the celebrity who knows no one in the room, so no friends)
Person tom = new Person("Tom");
// add persons into people
ArrayList<Person> people = new ArrayList<Person>();
people.add(john);
people.add(eric);
people.add(jeff);
// check if a person is the celebrity
String name = tom.name;
if (findCelebrity(people, john))
System.out.println(name + " is celebrity");
else
System.out.println(name + " is NOT celebrity");
}
// find if a person is celebrity among people
static public boolean findCelebrity(ArrayList<Person> people, Person person) {
// check if the person doesn't know any in the room
if (!person.friendsList.isEmpty())
return false;
// check if everyone know the person
int len = people.size();
for (int i = 0; i < len; i++) {
if (people.get(i).name != person.name) { // skip himself
if (!people.get(i).friendsList.contains(person.name)) // check if person is included in the friend list
return false;
}
}
return true;
}
static class Person {
ArrayList<String> friendsList = new ArrayList<String>();
String name;
public Person(String _name) {
name = _name;
}
public boolean knowSomeone(String person) {
if (friendsList.contains(person))
return true;
else
return false;
}
}
}
public class checkPalindrome {
public static void main(String[] args) {
String input = "L*&EVe)))l";
if (isPalindrome(input))
System.out.println("Palindrome");
else
System.out.println("NO Palindrome");
}
static public boolean isPalindrome(String input) {
String out = "";
// 1) remove special character
int len = input.length();
for (int i = 0; i < len; i++) {
char c = input.charAt(i);
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
out += c;
}
// 2) make it lowercase
out = out.toLowerCase();
// 3) check if palindrom.
// compare [i] vs [len-i-1], we dont need check the center element if it exists (in case of odd length)
len = out.length();
for (int i = 0; i < len/2; i++) {
if ((out.charAt(i) != out.charAt(len-i-1))) {
return false;
}
}
return true;
}
}
- CJ September 18, 2018