• 不少程序员都会遇到的三个问题


    面试问题如下:
    
    1. 求从10到100中能被3或5整除的数的和
    
    int sum = 0;
    for(int i = 10; i <= 100; i++)
    if( i % 3 == 0 || i % 5 == 0) sum += i;
    System.out.println(sum);
    2. 将一个字符串逆序,不要使用反转函数 String message = "he saw a racecar";
    StringBuilder rev = new StringBuilder();
    for(int i = message.length()-1; i >= 0; i--) rev.append(message.charAt(i));
    System.out.println(rev.toString()); 3. 反转一个栈 import java.util.*; public class MyProgram extends com.ktbyte.submit.Coder { public static void main(String[] args) { Stack items = new Stack(); items.push("he"); //he is at the bottom of the stack items.push("saw"); items.push("a"); items.push("racecar"); reverseStack(items); //now he is at the top //print in order pushed: while(items.size()>0) System.out.println(items.pop()); } public static void reverseStack(Stack stack) { Queue rev = new LinkedList(); while(stack.size()>0) rev.offer(stack.pop()); while(rev.size()>0) stack.push(rev.poll()); } }



    转自http://www.jfox.info/bu-shao-cheng-xu-yuan-du-hui-peng-dao-de-san-ge-mian-shi-ti
  • 相关阅读:
    MYSQL判断某个表是否已经存在
    百度、雅虎、谷歌搜索引擎接口调用注意事项
    Codeigniter整合Tank Auth权限类库的教程
    短链接的生成算法
    自定义String
    运算符和结合性
    字符串类封装
    运算符重载
    数组类封装
    友元
  • 原文地址:https://www.cnblogs.com/zjfjava/p/6069634.html
Copyright © 2020-2023  润新知