PIE Chapter 6--Java代码

【程序员面试攻略】一书中的第六章代码:


import java.util.Hashtable;
import java.lang.StringBuffer;


public class Ch6 {
	
	/*
	 * find the first non-repeated character in one string
	 * Input: one string
	 * Output: the first non-repeated character
	 */
	 
	public Character firstNonRepeated(String str){
		
		int length = str.length();
		Character ch;
		Integer intgr = null; 
		Hashtable<Character, Integer> charHash = new Hashtable<Character, Integer>();
		Integer numOne = new Integer(1);
		Integer numMoreThanOne = new Integer(2);
		
		//get all the character from the string
		for(int i = 0; i < length; i++){
			ch = new Character(str.charAt(i));
			intgr = (Integer) charHash.get(ch);
			if(intgr == null)
				charHash.put(ch, numOne);
			else
				charHash.put(ch, numMoreThanOne);
		}
		
		//check for the first non-rep char from the hashtable
		for (int i = 0; i < length; i++){
			ch = new Character(str.charAt(i));
			intgr = (Integer) charHash.get(ch);
			if(intgr == numOne)
				return ch;
		}
		
		return null;
	}
	
	 
	
	/*
	 * remove all the chars from a string 
	 * input: string str, and the string needs to be removed
	 * output: new string without the chars
	 */
	public String removeChars(String str, String remove){
		
		StringBuffer sb = new StringBuffer();
		
		for(char c : str.toCharArray()){
				if(remove.indexOf(c) == -1)
						 sb.append(c);
			}
		
		return sb.toString();
	}
	
	
	public String removeChar(String str, String remove){
		
		char[] s = str.toCharArray();
		char[] r = remove.toCharArray();
		boolean[] flags = new boolean[128];
		int len = s.length;
		int src, dst;
		
		for(src = 0; src < r.length; ++src){
			flags[r[src]] = true;
		}
		
		src = 0;
		dst = 0; 
		while(src < len){
		//for(src = 0, dst = 0; src < len; ++src){
			if( !flags[s[src]]){
				s[dst++] = s[src];
			}
			++src;
		}
		
		return new String(s,0,dst);
		
	}
	
	/*
	 * revert a given string.
	 * input: a string: Do or do not, there is no try.
	 * output: a reverted string: try. no is there not, do or Do
	 * the idea is to get each words in order, and output them reversely.
	 * we can do it by StringBuffer or Stack.
	 */
	public String revertString(String str){
		
		StringBuffer sb = new StringBuffer();
		String[] sa = str.split(" ");
		for(int i = sa.length -1; i >= 0; --i ){
			sb.append(sa[i].toString() + " ");
		}
		
		return sb.toString();
	}
	
	/*
	 * Another way to do reverseString: 
	 * The idea is to get the whole string reversely,
	 * and then reverse each word in order.
	 * eg.: in search of algorithmic elegance--> ecnagele cimhtiraglafo hcraes ni
	 * and then: elegance algorithmic of search in
	 */
	public char[] revertSentence(char[] str){
		
		int start = 0, end = 0, len;
		len = str.length;
		
		//reverse the whole string, will get: .yrt on si ereht ,ton od ro oD
		reverseStr(str, start, len-1);
		
		
		//get each word, and reverse it
		while(end < len){
			
			 if(str[end] != ' '){
				
				//get the new start for the next word in the string
				 start = end; 
				
				while(end < len && str[end] != ' ')
					end++;
				
				//back up to the end of the word
				end--;
				
				reverseStr(str, start, end);
			}
			 
			end++; //skip the empty space
			
		}
		
		return str;
	}
	
	private void reverseStr(char[] str, int start, int end){
		
		char temp;
		while(start < end){
			temp = str[start];
			str[start] = str[end];
			str[end] = temp;
			++start;
			--end;
		}
		
	}
	
	/*
	 * convert a string to an int
	 */
	public int strToInt(String str){
		int num = 0, len = str.length();
		boolean isNegative = false;
		int start = 0; 
		
		if(str.charAt(0)== '-'){
			isNegative = true;
			start = 1;
		}
		
		for(int i = start; i < len; ++i){
			int temp = str.charAt(i);
			num *= 10;   //two key steps: Horner rules
			num += (temp-'0');
		}
		
		 
		if(isNegative)
			num *= (-1);
		
		return num;
		
	}
	
	/*
	 * convert an int to a string
	 */
	public static final int MAX_DIGITS = 10;
	public String intToStr(int num){
		
		StringBuffer sb = new StringBuffer();
		boolean isNegative = false;
		char[] temp = new char[MAX_DIGITS + 1]; 
		int i = 0;
		
		if(num < 0){
			isNegative = true;
			num *= -1;
		}
		
		do{
			temp[i++] = (char)((num % 10) + '0'); 
			num /= 10; 
		}while( num != 0);
		
		if(isNegative)
			sb.append('-');
		
		while( i > 0)
			sb.append(temp[--i]);  //good! 
		
		return sb.toString();
		
	}
	
	
	
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
poi 版本 3.17 文件中包含 模板 测试main 数据类 需要调整下 路径 /** * @Description: * @Author: xsr * @date : 2018/7/22 9:41 */ public static void makePiePpt(List dataList) throws Exception { //打开模板ppt String mtemplateName = "E:/PIE/mtemplate/PIE" + dataList.size() + ".pptx"; String path ="E:/PIE/NewPPT/NewPIE" + dataList.size() + ".pptx"; XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(mtemplateName)); pptx.setPageSize(new Dimension(960, 580)); for (int i = 0; i < dataList.size(); i++) { makePiePpt(pptx, i, dataList); } //保存文件 OutputStream out = new FileOutputStream(path); pptx.write(out); out.close(); System.out.println("导出成功"); } /** * @Description: * @Author: xsr * @date : 2018/7/27 5:41 */ public static void makePiePpt(XMLSlideShow pptx, Integer pieNum, List dataList) throws Exception { //获取第一个ppt页面 XSLFSlide slide = pptx.getSlides().get(0); //遍历第一页元素找到图表 XSLFChart chart; List poixmlDocumentParts = new ArrayList(); for (POIXMLDocumentPart part : slide.getRelations()) { if (part instanceof XSLFChart) { chart = (XSLFChart) part; poixmlDocumentParts.add(chart); } } chart = (XSLFChart) poixmlDocumentParts.get(pieNum); POIXMLDocumentPart xlsPart = chart.getRelations().get(0); //把图表绑定到Excel workbook中 XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); CTChart ctChart = chart.getCTChart(); CTPlotArea plotArea = ctChart.getPlotArea(); CTPieChart pieChart = plotArea.getPieChartArray(0); // 获取图表的系列 CTPieSer ser = pieChart.getSerArray(0); XSSFRow row0 = sheet.createRow(0); // Series Text CTSerTx tx = ser.getTx(); tx.getStrRef().
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值