• Fizz Buzz

    class Solution {

    public List<String> fizzBuzz(int n) {

    List<String> list=new LinkedList<>();

    for (int i = 1; i <=n; i++) {

    if (i%3==0&&i%5==0) {

    list.add("FizzBuzz");

    continue;

    }else if (i%3==0) {

    list.add("Fizz");

    continue;

    }else if (i%5==0) {

    list.add("Buzz");

    continue;

    }

    list.add(String.valueOf(i));

    }

    return list;

    }

    }

  • 计数质数

  • 很快的方法

    class Solution {

    public int countPrimes(int n) {

    if (n<3) {

    return 0;

    }

    int res=0;

    boolean[] notPrime=new boolean[n];//默认都为false

    for (int i = 2; i < n; i++) {

    if (!notPrime[i]) {

    res++;

    for (int j = 2; i*j < n; j++) {

    notPrime[i*j]=true;

    }

    }

    }

    return res;

    }

    }

  • 我的解法:耗时间

    class Solution {

    public int countPrimes(int n) {

    if (n==0||n==1||n==2) {

    return 0;

    }

    int res=0;

    for (int i = 2; i < n; i++) {

    boolean isPrimes=true;

    for (int j = 2; j <=Math.sqrt(i); j++) {

    if (i%j==0) {

    isPrimes=false;

    break;

    }

    }

    if (isPrimes) {

    res++;

    }

    }

    return res;

    }

    }

  • 3的幂

在解这道题的时候。首先要明确地将整数划分为两大部分,即小于等于零(此时没有幂,log x 其中x>0)

和大于零的部分;

在大于零的部分又可以划分为1和不能被3整除(则不是3的幂次方)和能被3整除的!

  • 递归法

    class Solution {

    public boolean isPowerOfThree(int n) {

    if (n==0) {

    return false;

    }else if (n==1) {

    return true;

    }else if (n%3==0) {

    return isPowerOfThree(n/3);

    }else {

    return false;

    }

    }

    }

  • 循环法

    class Solution {

    public boolean isPowerOfThree(int n) {

    if (n<1) {

    return false;

    }

    while (n!=1) {

    if (n%3!=0) {

    return false;

    }else {

    n/=3;

    }

    }

    return true;

    }

    }

  • 数学方法log10

    class Solution {

    public boolean isPowerOfThree(int n) {

    if (n<1) {

    return false;

    }

    double d=Math.log10(n)/Math.log10(3);

    return (d-(int)d)==0?true:false;

    }

    }

  • 罗马数字转整数

  • 我的解法:

    有点暴力,虽然代码很多,但很好理解,写出这么长的代码我确实好菜!!!

    因为没看清题目,人家都说了这六种特殊情况是因为小的在大的左边 蠢到家!!

    class Solution {

    public int romanToInt(String s) {

    int res=0;

    char[] cs=s.toCharArray();

    int len=cs.length;

    int i=len-1;

    int f=len-2;

    StringBuffer st=new StringBuffer();

    StringBuffer sp=new StringBuffer();

    //现将特殊和单个分为两个数组

    while (i>=0||f>=0) {

    try {

    if (cs[i]=='V'&&cs[f]=='I') {//1

    st.append(cs[f]);

    st.append(cs[i]);

    i-=2;

    f-=2;

    }else if (cs[i]=='X'&&cs[f]=='I') {//2

    st.append(cs[f]);

    st.append(cs[i]);

    i-=2;

    f-=2;

    }else if (cs[i]=='L'&&cs[f]=='X') {//3

    st.append(cs[f]);

    st.append(cs[i]);

    i-=2;

    f-=2;

    }else if (cs[i]=='C'&&cs[f]=='X') {//4

    st.append(cs[f]);

    st.append(cs[i]);

    i-=2;

    f-=2;

    }else if (cs[i]=='D'&&cs[f]=='C') {//5

    st.append(cs[f]);

    st.append(cs[i]);

    i-=2;

    f-=2;

    }else if (cs[i]=='M'&&cs[f]=='C') {//6

    st.append(cs[f]);

    st.append(cs[i]);

    i-=2;

    f-=2;

    }

    else if (cs[i]=='I') {

    sp.append(cs[i]);

    i--;

    f--;

    }else if (cs[i]=='V') {

    sp.append(cs[i]);

    i--;

    f--;

    }else if (cs[i]=='X') {

    sp.append(cs[i]);

    i--;

    f--;

    }else if (cs[i]=='L') {

    sp.append(cs[i]);

    i--;

    f--;

    }else if (cs[i]=='C') {

    sp.append(cs[i]);

    i--;

    f--;

    }else if (cs[i]=='D') {

    sp.append(cs[i]);

    i--;

    f--;

    }else if (cs[i]=='M') {

    sp.append(cs[i]);

    i--;

    f--;

    }

    } catch (Exception e) {//这里还越界,通过抛出异常处理

    // TODO: handle exception

    sp.append(cs[i]);

    i--;

    }

    }

    char[] st1=st.toString().toCharArray();

    char[] sp1=sp.toString().toCharArray();

    int lenst=st1.length;

    int i1=lenst-1;

    int f1=lenst-2;

    //对特殊数组取值

    while (i1>=0||f1>=0) {

    if (st1[i1]=='V'&&st1[f1]=='I') {//1

    res+=4;

    i1-=2;

    f1-=2;

    }else if (st1[i1]=='X'&&st1[f1]=='I') {//2

    res+=9;

    i1-=2;

    f1-=2;

    }else if (st1[i1]=='L'&&st1[f1]=='X') {//3

    res+=40;

    i1-=2;

    f1-=2;

    }else if (st1[i1]=='C'&&st1[f1]=='X') {//4

    res+=90;

    i1-=2;

    f1-=2;

    }else if (st1[i1]=='D'&&st1[f1]=='C') {//5

    res+=400;

    i1-=2;

    f1-=2;

    }else if (st1[i1]=='M'&&st1[f1]=='C') {//6

    res+=900;

    i1-=2;

    f1-=2;

    }

    }

    int lensp=sp1.length;

    int i2=lensp-1;

    //对单个数组取值

    while (i2>=0) {

    if (sp1[i2]=='I') {

    res+=1;

    i2--;

    }else if (sp1[i2]=='V') {

    res+=5;

    i2--;

    }else if (sp1[i2]=='X') {

    res+=10;

    i2--;

    }else if (sp1[i2]=='L') {

    res+=50;

    i2--;

    }else if (sp1[i2]=='C') {

    res+=100;

    i2--;

    }else if (sp1[i2]=='D') {

    res+=500;

    i2--;

    }else if (sp1[i2]=='M') {

    res+=1000;

    i2--;

    }

    }

    return res;

    }

    }

  • 大神解法:利用map,挺好!

    class Solution {

    public int romanToInt(String s) {

    Map<Character, Integer> hashMap = new HashMap<>();

    // I 1

    // V 5

    // X 10

    // L 50

    // C 100

    // D 500

    // M 1000

    hashMap.put('I', 1);

    hashMap.put('V', 5);

    hashMap.put('X', 10);

    hashMap.put('L', 50);

    hashMap.put('C', 100);

    hashMap.put('D', 500);

    hashMap.put('M', 1000);

    int sum=0;

    char[] cs=s.toCharArray();

    for (int i = cs.length-1; i>=0; i--) {

    //处理六种特殊情况,即大的在右,小的在左

    if ((i-1)>=0&&hashMap.get(cs[i])>hashMap.get(cs[i-1])) {

    sum+=hashMap.get(cs[i])-hashMap.get(cs[i-1]);

    i--;

    }else {

    //处理单个的情况

    sum+=hashMap.get(cs[i]);

    }

    }

    return sum;

    }

    }

算法练习LeetCode初级算法之数学的更多相关文章

  1. 【LeetCode算法】LeetCode初级算法——字符串

      在LeetCode初级算法的字符串专题中,共给出了九道题目,分别为:反转字符串,整数反转,字符串中的第一个唯一字符,有效的字母异位词,验证回文字符串,字符串转换整数,实现strStr(),报数,最 ...

  2. 算法练习LeetCode初级算法之链表

    删除链表中的节点 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode ne ...

  3. 算法练习LeetCode初级算法之字符串

    反转字符串 我的解法比较low,利用集合的工具类Collections.reverse反转,用时过长 class Solution { public void reverseString(char[] ...

  4. 算法练习LeetCode初级算法之数组

    删除数组中的重复项 官方解答: 旋转数组 存在重复元素 只出现一次的数     官方解答:  同一个字符进行两次异或运算就会回到原来的值 两个数组的交集 II import java.util.Arr ...

  5. 算法练习LeetCode初级算法之其他

    位1的个数 解法一: class Solution { // you need to treat n as an unsigned value public int hammingWeight(int ...

  6. 算法练习LeetCode初级算法之设计问题

    打乱数组 不断的让第一个与后面随机选择的数交换 class Solution { private int[] nums; private int[] initnums; public Solution ...

  7. 算法练习LeetCode初级算法之动态规划

    爬楼梯:斐波那契数列 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 非递归解法 class S ...

  8. 算法练习LeetCode初级算法之排序和搜索

    合并两个有序数组 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { System.arrayco ...

  9. 算法练习LeetCode初级算法之树

    二叉树的前序遍历 我的解法:利用递归,自底向下逐步添加到list,返回最终的前序遍历list class Solution { public List<Integer> preorderT ...

随机推荐

  1. 2018/12/20 20:52:42 螺纹钢PTA豆粕

    如期向上,但是一点办法没有:没有好的入场位,不做不算错,面对诱惑不动如山也是一种修养,今晚看M5有没有3买,有的话可以看情况考虑要不要进场 PTA M30向下一笔过程中,等待M30当前一笔下跌结束,可 ...

  2. JAVA中通过Jaxp操作XML文件基础

    Java中有多种方式操作XML文件,目前讲一讲以SUN公司提供的DocumentBuilderFactory工厂类对象操作XML. 使用XML基本操作就是需要CRUD(增删改查),那么首先通过一个查询 ...

  3. 服务注册中心,Eureka比Zookeeper好在哪里?

    著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性).和P(分区容错性).由于分区容错性P在分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡. 因此: Zookeep ...

  4. 开发环境 pyenv

    pyeny githup地址:https://github.com/pyenv/pyenv/ 安装时使用pyeny提供安装工具进行安装 githup 地址:https://github.com/pye ...

  5. JQuery 中的选择器

    选择器:允许通过标签名,属性名或内容对DOM元素进行快速,准确的选择,浏览器兼容性很好. 普通选择器 选择器 功能 返回值 #id 根据给定的ID匹配一个元素 单个元素 element 根据给定的元素 ...

  6. 小程序navigateBack,子页面传值给父页面

    子页面 let page = getCurrentPages(); let prevPage = page[page.length - 2]; prevPage.setData({ lxr :item ...

  7. 常用LINUX命令汇总

    一.基本命令bash Bash(GNU Bourne-Again Shell)是许多Linux平台的内定Shellpwd 查看当前所在目录ls 查看目录内所有文件cd 进入目录cd .. 返回上一层p ...

  8. azkaban使用--指定executor

    PS:局限,虽然可以指定在一台节点上执行,但是失去了高可用的优势,如果有不同类型的任务建议用多套azkaban 假如二台executor,下图中的4,5,我想将任务运行在hadoop02上,因为只有这 ...

  9. docker php容器中简单添加seaslog拓展

    最近有个项目用到了seaslog,因为之前调试php的容器已经搭好了,不想再通过dockerfile重新搭建了,搜了半天没有东西可以装,就仿照着安装redis拓展操作了一顿 1.wget http:/ ...

  10. 剑指offer——包含min函数的栈

    题目:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度为O(1)) 该题是自己第一次采用编程的方式来实现Java中栈的功能,故直接借鉴了大牛的代码 import ...