标签:
http://www.nowcoder.com/ta/huawei?query=&asc=true&order=&page=1
无聊刷华为的题 大部分比较简单
答案如下
package org.huawei.test; /** * Created by jonwinters1992 on 2016/10/3 0003. */ import java.util.*; import org.junit.Test; public class Huaweiexam { //@Test public void test() { int[] a = {2, 1, 323, 44, 233, 44432, 12321, 3231}; int max = 0; int temp = 0; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { temp = a[j] - a[i]; if (max < temp) { max = temp; } } } System.out.println(max); } //@Test public void getLastWordLength() { String str = "hellwo dfda dafd dafdf daf"; System.out.println(str.split(" ")[str.split(" ").length-1].length()); } //@Test public void getTotalCountOf(){ char a=‘A‘; String str="dfafAdDFADSFDAFsdfafsaffasffdas"; int total =0; for (int i=0;i<str.length();i++){ if (a == Character.toUpperCase(str.charAt(i)) ){ total+=1; } } System.out.println(total); } //@Test public void getRandomNumber(){ int [] array = {233,32,123,1231,123,22,33,1232,112,2,1321,223,32,23123,123}; Set<Integer> set = new HashSet<Integer>(); for (int a:array){ set.add(a); } List<Integer> list = new ArrayList<Integer>(set); Collections.sort(list); for(int a:list){ System.out.println(a+","); } } //@Test public void printArray(){ String str = "dafs dfasdf fdaf dfaf daf"; String str2 = str.replaceAll(" ", ""); for(int i=0;i< str2.length();i++){ if(i>0&&i%8==0) System.out.println(str2.substring(i-8,i)); } System.out.print(str2.substring(str2.length()-str2.length()%8, str2.length())); for(int i=0;i<8-str2.length()%8;i++){ System.out.print(0); } } public int switchByAlpha(char alpha){ switch(alpha){ case ‘a‘:return 10; case ‘b‘:return 11; case ‘c‘:return 12; case ‘d‘:return 13; case ‘e‘:return 14; case ‘f‘:return 15; default:return 0; } } //@Test public void HexToDecimal(){ String str = "0xdfa1"; int j =1; int total = 0; for (int i = str.length() - 1; i >= 0; i--) { if (str.toCharArray()[i] != ‘x‘) { if (Character.isDigit(str.toCharArray()[i])) { total += (Integer.valueOf(str.substring(i,i+1))) * j; } else { total += switchByAlpha(str.toCharArray()[i]) * j; } j *= 16; } else { break; } } System.out.println(total); } //@Test public void SetAndQueue(){ Queue<Character> queue = new LinkedList<Character>(); Set<Character> set = new HashSet<Character>(); String str ="12398731"; for (int i = str.length() -1;i>=0;i--){ if (!set.contains(str.toCharArray()[i])){ queue.add(str.toCharArray()[i]); set.add(str.toCharArray()[i]); } } for (char c : queue){ System.out.print(c); } } //@Test public void ReverseNumber(){ int num = 1323432; String str = String.valueOf(num); Stack<Character> stack = new Stack<Character>(); for(char c : str.toCharArray()){ stack.add(c); } for (int i =0 ; i<str.length();i++){ System.out.print(stack.pop()); } } //@Test public void ReverseString(){ String str = "dafsfasf"; Stack<Character> stack = new Stack<Character>(); for(char c: str.toCharArray()){ stack.push(c); } for (char c:str.toCharArray()){ System.out.print(stack.pop()); } } //@Test public void ReverseWord(){ String str="I am a boy"; Stack<String> stack = new Stack<String>(); for (String s:str.split(" ")){ stack.push(s); } for(String s:str.split(" ")){ System.out.print(stack.pop()+" "); } } //@Test public void StringSort(){ String str = "fdad saf dsaf da feij afhlf"; List<String> list = new ArrayList<String>(); for(String s:str.split(" ")){ list.add(s); } Collections.sort(list); for(String s:list){ System.out.println(s); } } //@Test public void DecimalToBinaryAndCountNumberOf1(){ int i = 25444445; int j = 0; while(i>=1){ if (i%2==1){ j++; } i /= 2; } System.out.println(j); } ////////////////////////////// public char encodeUpcaseChar(char encode){ encode = Character.toLowerCase(encode); if (encode == ‘z‘){ return ‘a‘; } return (char)(encode+1); } public int encodeLowerCase(char encode){ switch(encode){ case ‘a‘:return 2; case ‘b‘:return 2; case ‘c‘:return 2; case ‘d‘:return 3; case ‘e‘:return 3; case ‘f‘:return 3; case ‘g‘:return 4; case ‘h‘:return 4; case ‘i‘:return 4; case ‘j‘:return 5; case ‘k‘:return 5; case ‘l‘:return 5; case ‘m‘:return 6; case ‘n‘:return 6; case ‘o‘:return 6; case ‘p‘:return 7; case ‘q‘:return 7; case ‘r‘:return 7; case ‘s‘:return 7; case ‘t‘:return 8; case ‘u‘:return 8; case ‘v‘:return 8; case ‘w‘:return 9; case ‘x‘:return 9; case ‘y‘:return 9; case ‘z‘:return 9; } return 0; } ///////////////////////////// //@Test public void toEncodePassword(){ String pass = "YUANzhi1987";//test case char tmp; char[] chars = pass.toCharArray(); for (int i=0; i < chars.length;i++){ tmp = chars[i]; if (Character.isUpperCase(tmp)){ chars[i] = encodeUpcaseChar(tmp); } if (Character.isLowerCase(tmp)){ chars[i] = String.valueOf(encodeLowerCase(tmp)).toCharArray()[0]; } } System.out.println(new String(chars)); } //@Test public void emptyBol(){ int num = 21; int total = 0; while (num >= 3){ num = num /3; total+=num; } System.out.println(total+1); } //@Test public void deleteLowestFrequencyChar(){ String str = "abcdddefghjuyr"; char [] chars = str.toCharArray(); Map<Character,Integer> charMap = new HashMap<Character, Integer>(); for (char ch : chars){ if (charMap.containsKey(ch)){ charMap.put(ch,charMap.get(ch)+1); } else { charMap.put(ch,1); } } List<Integer> list = new ArrayList<Integer>(); for(Map.Entry entry : charMap.entrySet()){ list.add((Integer)entry.getValue()); //System.out.println(entry.getKey()+" "+entry.getValue()); } Collections.sort(list); // System.out.println(list.toArray()[0]); List<Character> lowFrequencyList = new ArrayList<Character>(); for(Map.Entry entry :charMap.entrySet()){ if (entry.getValue() == list.toArray()[0]){ lowFrequencyList.add((Character) entry.getKey()); } } for (int i = 0 ; i < chars.length ; i++){ for(char zh : lowFrequencyList){ if (chars[i] == zh){ chars[i] = ‘0‘;//mark } } } System.out.println(new String(chars)); System.out.println(new String(chars).replaceAll("0",""));//remove mark } }
标签:
原文地址:http://www.cnblogs.com/winters1992/p/5936046.html