Google OA

google

*友情提示:如果直接照炒代码,你可能会永远进不了google。
1. 给一个int,根据一定规则替换相邻的两个digit,返回替换完成的int。
1.1 replace two adjacent digits with the larger one, return min (e.g. 233614 -> 23364)
1.2 replace two adjacent digits with the round up average, return max (e.g. 623315 -> 63315)
1.3 choose a group of(at least two) identical adjacent and remove a single digit, return max (e.g.223336226 -> 23336226)

public class Solution {
//    1.1 replace two adjacent digits with the larger one, return min (e.g. 233614 -> 23364)
	public static int solution1(int X)
	{
		int min = Integer.MAX_VALUE;
		String s = String.valueOf(X);
		for(int i = 0; i < s.length() - 1; i++)
		{
			int tmp = Integer.MAX_VALUE;
			if(s.charAt(i) > s.charAt(i + 1)) tmp = Integer.parseInt(s.substring(0, i + 1) + s.substring(i + 2));
			else tmp = Integer.parseInt(s.substring(0, i) + s.substring(i + 1));
			min = Math.min(min, tmp);
		}
		return min;
	}
//    1.2 replace two adjacent digits with the round up average, return max (e.g. 623315 -> 63315)
	public static int solution2(int X)
	{
		int max = Integer.MIN_VALUE;
		String s = String.valueOf(X);
		for(int i = 0; i < s.length() - 1; i++)
		{
			int ave = ((s.charAt(i) - '0') + (s.charAt(i + 1) - '0') + 1) / 2;
			int tmp = Integer.parseInt(s.substring(0, i) + ave + s.substring(i + 2));
			max = Math.max(max, tmp);
		}
		return max;
	}
//    1.3 choose a group of(at least two) identical adjacent and remove a single digit, return max (e.g.223336226 -> 23336226)
	public static int solution3(int X)
	{
		int max = Integer.MIN_VALUE;
		String s = String.valueOf(X);
		for(int i = 0; i < s.length() - 1; i++)
		{
			if(s.charAt(i) == s.charAt(i + 1))
			{
				int tmp = Integer.parseInt(s.substring(0, i) + s.substring(i + 1));
				max = Math.max(max, tmp);
			}
		}
		return max;
	}
	public static void main(String[] args)
	{
//		System.out.println(solution1(233614));
		System.out.println(solution2(10));
		System.out.println(solution2(123456));
		System.out.println(solution2(54321));
		System.out.println(solution2(1000000000));
//		System.out.println(solution3(223336226));
	}
}

2. 给一个代表文件路径的string,根据具体题目要求,返回图片(.jpeg or .png or .gif)路径长度
e.g. Given String s = “dir1\n dir11\n dir12\n picture.jpeg\n dir121\n file1.txt\ndir2\n file2.gif”;
So image paths are /dir1/dir12/picture.jpeg and /dir2/file2.gif
1.1 return longest image path to root (return 11, /dir1/dir12)
1.2 return longest image path to imgae (return 24, /dir1/dir12/picture.jpeg)
1.3 return total image path to root (return 11 + 5 = 16, /dir1/dir12 + /dir2)
1.4 return toal image path to root (return 24 + 15 = 39, /dir1/dir12/picture.jpeg + /dir2/file2.gif)

import java.util.*;
public class Solution {
	public static int solution1(String s)
	{
//		1.1 return longest image path to root (return 11, /dir1/dir12)
		int max = 0;
		Stack<Integer> stack = new Stack<>();
		stack.push(0);
		for(String itr: s.split("\n"))
		{
			int k = itr.lastIndexOf(' ') + 1;
			while(stack.size() > k + 1) stack.pop();
			if(itr.endsWith(".jpeg") || itr.endsWith(".png") || itr.endsWith(".gif")) max = Math.max(max, stack.peek());
			else stack.push(itr.length() + stack.peek() - k + 1);
		}
		return max;
	}
	
	public static int solution2(String s)
	{
//		1.2 return longest image path to image (return 24, /dir1/dir12/picture.jpeg)
		int max = 0;
		Stack<Integer> stack = new Stack<>();
		stack.push(0);
		for(String str: s.split("\n"))
		{
			int k = str.lastIndexOf(' ') + 1;
			while(k + 1 < stack.size()) stack.pop();
			int len = str.length() + stack.peek() - k + 1;
			if(str.endsWith(".jpeg") || str.endsWith(".png") || str.endsWith(".gif")) max = Math.max(len, max);
			else stack.push(len);
		}
		return max;
	}
	
	public static int solution3(String s)
	{
//		1.3 return total image path to root (return 11 + 5 = 16, /dir1/dir12 + /dir2)
		int sum = 0;
		Stack<Integer> stack = new Stack<>();
		stack.push(0);
		for(String itr: s.split("\n"))
		{
			int k = itr.lastIndexOf(' ') + 1;
			while(stack.size() > k + 1) stack.pop();
			if(itr.endsWith(".jpeg") || itr.endsWith(".png") || itr.endsWith(".gif")) sum += stack.peek();
			else stack.push(itr.length() + stack.peek() - k + 1);
		}
		return sum;
	}
	
	public static int solution4(String s)
	{
//		1.4 return total image path to root (return 24 + 15 = 39, /dir1/dir12/picture.jpeg + /dir2/file2.gif)
		int sum = 0;
		Stack<Integer> stack = new Stack<>();
		stack.push(0);
		for(String str: s.split("\n"))
		{
			int k = str.lastIndexOf(' ') + 1;
			while(k + 1 < stack.size()) stack.pop();
			int len = str.length() + stack.peek() - k + 1;
			if(str.endsWith(".jpeg") || str.endsWith(".png") || str.endsWith(".gif")) sum += len;
			else stack.push(len);
		}
		return sum;
	}
	
	
	public static void main(String[] args)
	{
		String input = "dir1\n dir11\n dir12\n  picture.jpeg\n  dir121\n  file1.txt\ndir2\n file2.gif";
		System.out.println("solution1 = "+ solution1(input));
	}
}

11330total visits,24visits today