hardcode版本 估计只能过一个吧
import java.util.*; public class NextServer { Map<Integer, Integer> serverCount = new HashMap<Integer, Integer>(); //serverNum uses this, so defined by myself, according to his input,wrong //int[] serverNums = {1,2,3}; int[] serverNums = {0}; int next_server_number(int[] serverNums) { int len = serverNums.length; Arrays.sort(serverNums); for (int i = 0; i < serverNums.length; i++) { if (!serverCount.containsKey(serverNums[i])) serverCount.put(serverNums[i], 1); else serverCount.put(serverNums[i], serverCount.get(serverNums[i]) + 1); } int max = serverNums[len - 1]; int result = max + 1; for (int j = 1; j < max; j++) { //System.out.println("j = " + j); if (!serverCount.containsKey(j)) { result = j; //System.out.println("result = " + result); break; } } return result; } String allocate(String hostType) { int serverNum = next_server_number(serverNums); if (serverCount.containsKey(serverNum) && serverCount.get(serverNum) > 1) serverCount.put(serverNum, serverCount.get(serverNum) - 1); if (serverCount.containsKey(serverNum) && serverCount.get(serverNum) == 1) serverCount.remove(serverNum, 1); //serverNum is wrong return hostType + String.valueOf(serverNum); } void deallocate(String hostType) { int serverNum = Integer.valueOf(hostType.charAt(hostType.length() - 1)); if (!serverCount.containsKey(serverNum)) serverCount.put(serverNum, 1); else serverCount.put(serverNum, serverCount.get(serverNum) + 1); } public static void main(String args[]) { NextServer ns = new NextServer(); String[] input = {"next 1 2 3","next","allocate banana","dealocate banana1"}; for (int i = 0; i < input.length; i++) { if (input[i].contains("next")) { if (input[i].length() == 4) System.out.println(1); else if (input[i].length() > 4) { String[] temp = input[i].split("\s+"); int[] numbers = new int[temp.length - 1]; for (int j = 1; j < temp.length; j++) { numbers[j - 1] = Integer.valueOf(temp[j]); } System.out.println(ns.next_server_number(numbers)); } }else if (input[i].contains("allocate")) { System.out.println(ns.allocate(input[i])); }else if (input[i].contains("deallocate")) { ns.deallocate(input[i]); } } } }
有点不对,暂时现就这样吧:
import java.util.*; public class NextServer { Map<Integer, Integer> serverCount = new HashMap<Integer, Integer>(); static int[] serverNums; public static void main(String args[]) { NextServer ns = new NextServer(); String[] input = {"next 1 2 3 4","next","allocate banana","dealocate banana1","allocate apple","allocate orange","allocate banana"}; for (int i = 0; i < input.length; i++) { if (input[i].contains("next")) { if (input[i].length() == 4) { serverNums = new int[1]; serverNums[0] = 0; System.out.println(ns.next_server_number(serverNums)); } else if (input[i].length() > 4) { String[] temp = input[i].split("\s+"); serverNums = new int[temp.length - 1]; for (int j = 1; j < temp.length; j++) { serverNums[j - 1] = Integer.valueOf(temp[j]); } System.out.println(ns.next_server_number(serverNums)); } }else if (input[i].contains("allocate")) { System.out.println(ns.allocate(input[i])); }else if (input[i].contains("deallocate")) { ns.deallocate(input[i]); } } } int next_server_number(int[] serverNums) { int len = serverNums.length; Arrays.sort(serverNums); for (int i = 0; i < serverNums.length; i++) { if (!serverCount.containsKey(serverNums[i])) serverCount.put(serverNums[i], 1); else serverCount.put(serverNums[i], serverCount.get(serverNums[i]) + 1); } int max = serverNums[len - 1]; int result = max + 1; if ((len - 1) == 1) return result = 1; for (int j = 1; j < max; j++) { if (!serverCount.containsKey(j)) { result = j; break; } } return result; } String allocate(String hostType) { int serverNum = next_server_number(serverNums); if (serverCount.containsKey(serverNum) && serverCount.get(serverNum) > 1) serverCount.put(serverNum, serverCount.get(serverNum) - 1); if (serverCount.containsKey(serverNum) && serverCount.get(serverNum) == 1) serverCount.remove(serverNum, 1); return hostType + String.valueOf(serverNum); } void deallocate(String hostType) { int serverNum = Integer.valueOf(hostType.charAt(hostType.length() - 1)); if (!serverCount.containsKey(serverNum)) serverCount.put(serverNum, 1); else serverCount.put(serverNum, serverCount.get(serverNum) + 1); } }
最长字符串
怎么返回空格原来的形式啊,关键是不知道怎么找到位置
暴力解法:
package lendinghome; public class longestPlr { //ini int lo, maxLength; public String longestPalindrome(String s) { //cc: s.length() < String[] words = s.split("\s+"); StringBuilder sb = new StringBuilder(); for (int i = 0; i < words.length; i++) { sb.append(words[i]); } String compressedStr = sb.toString(); System.out.println("compressedStr = " + compressedStr); if (s == "" || s.length() == 0 || s.length() == 1) return s; //for loop in two cases for (int i = 0; i < compressedStr.length() - 1; i++) { extendPalindrome(compressedStr, i, i); extendPalindrome(compressedStr, i, i + 1); } //return substring String compressedResult = compressedStr.substring(lo, lo + maxLength); System.out.println("compressedResult = " + compressedResult); String res = ""; //find the un compressed Result for (int i = 0; i < s.length(); i++) { for (int j = i; j < s.length(); j++) { String temp = s.substring(i, j + 1); String[] words1 = temp.split("\s+"); StringBuilder sb1 = new StringBuilder(); //if (s.charAt(0) == ' ') sb1.append(' '); for (int k = 0; k < words1.length; k++) { sb1.append(words1[k]); } String compressedStr1 = sb1.toString(); //System.out.println("compressedStr1 = " + compressedStr1); if (compressedStr1.equals(compressedResult)) { res = temp; //System.out.println("res = "+ res); //System.out.println("res.length() = "+ res.length()); } } } //System.out.println("res = "); if (s.charAt(0) == ' ') res = ' ' + res; return res; } public void extendPalindrome(String s, int k, int j) { //while loop //k should always < s.length() since the index is from 0 to n -1 while (j >= 0 && k < s.length() && s.charAt(j) == s.charAt(k)) { j--; k++; } //renew the lo, maxLength; if (k - 1 - j > maxLength) { maxLength = k - 1 - j; lo = j + 1; } } public static void main(String args[]) { longestPlr lpr = new longestPlr(); //String testStr = "I went to my gym yesterday."; //String testStr = " nurse srun "; String testStr = " aibohphobia "; System.out.println("testStr.length()" + testStr.length()); //String testStr = "abagcdcjkik";ok System.out.println(lpr.longestPalindrome(testStr)); } }