Problem


Given an integer, write a function to determine if it is a power of three.


Follow up:


Could you do it without using any loop / recursion?


Code:


class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= 0) return 0;
int max_pow3 = log10(INT_MAX)/log10(3);
int max_pow3_val = pow(3, max_pow3);
return max_pow3_val % n == 0;
}
};

说明:


自己想了好一会,居然没想到对数,,汗,这个解决方法的巧在于int最大是INT_MAX,所以取log3(INT_MAX)得到3的最大次方,然后计算出来,对n取余即可


Java Code:


public class Solution {
public boolean isPowerOfThree(int n) {
double res = Math.log(n)/Math.log(3);
return Math.abs(res - Math.rint(res))< 0.0000000001;
}
}

说明:


其实和上面一样用的是对数,这就更加直接了,通过求log3(n)的结果,看与其最近的整数的差值满足几乎为0即可。

 
Problem:

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

Required:

You should try to do it in place. The program should run in O() space complexity and O(nodes) time complexity.

Example:

Given ->->->->->NULL, return ->->->->->NULL.

Note:

The relative order inside both the even and odd groups should remain as it was in the input. The first node is considered odd, the second node even and so on ...

Code:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
int count = ;
ListNode *p, *q, *head1, *r, *tail;
head1 = (struct ListNode*)malloc(sizeof(struct ListNode));
r = head1;
p = head;
q = p->next;
while (p != NULL && q != NULL) {
p->next = q->next;
q->next = NULL;
r->next = q;
r = q;
if (p->next == NULL) {
tail = p;
}
p = p->next;
if (p != NULL) {
if (p->next == NULL) {
tail = p;
}
q = p->next;
}
}
tail->next = head1->next;
return head;
}
};
说明: 一开始理解题目就错了,汗,看成交换奇偶了,想法是直接交换value;
指针初始化不会了,百度才知道的,汗;
没加tail指针,想直接用p作为head的最后一个指针,结果死循环了;
加了tail但是在q为NULL的时候没赋值,奇数个的时候会执行失败;
综上所述,太久没用c++了,都退化了呀。

实在是自己在写代码碰到瓶颈了,只能贴两道做过的题目了。

leetcode简单题目两道(2)的更多相关文章

  1. leetcode简单题目两道(4)

    心情还是有问题,保持每日更新,只能如此了. Problem Given a binary tree, return the level order traversal of its nodes' va ...

  2. leetcode简单题目两道(3)

    本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了. Problem Given an array nums, write a function to move all 's to ...

  3. leetcode简单题目两道(5)

    Problem Given an integer (signed bits), write a function to check whether it . Example: Given num = ...

  4. leetcode简单题目两道(1)

    Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the t ...

  5. CTF中关于XXE(XML外部实体注入)题目两道

    题目:UNCTF-Do you like xml? 链接:http://112.74.37.15:8008/ hint:weak password (弱密码) 1.观察后下载图片拖进WINHEX发现提 ...

  6. 两道面试题,带你解析Java类加载机制

    文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...

  7. 【转】两道面试题,带你解析Java类加载机制(类初始化方法 和 对象初始化方法)

    本文转自 https://www.cnblogs.com/chanshuyi/p/the_java_class_load_mechamism.html 关键语句 我们只知道有一个构造方法,但实际上Ja ...

  8. 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester

    这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...

  9. 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制

    你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...

随机推荐

  1. Docker在github上的站点

    https://github.com/docker (docker在github上的官方地址) https://github.com/dockerfile (docker官方镜像的Dockerfile ...

  2. cron和crontab

    crontab -l 列出目前的计划任务(时程表) crontab -e 编辑计划任务   计划任务的格式如下: f1 f2 f3 f4 f5 program 其中 f1 是表示分钟,f2 表示小时, ...

  3. asp.net 类头部描述

    这里教大家怎么在新建类的时候默认有头部描述,先看效果: 像这样的内容我们要怎么进行添加呢? 前方高能...... 找到VS的安装目录-->比如我自己的安装目录D:\VS2013\Common7\ ...

  4. WPF GridControl单元格值与过滤值相同时,改变单元格背景色

    DataTrigger的Value不可绑定,所以我们需要用MultiBinding来实现这个功能. <dxg:TableView.CellStyle> <Style TargetTy ...

  5. UWP多线程枚举安全的List

    最近在做windows runtime下APP开发的工作.在Service层请求返回后,往往会通过回调的形式来通知UI更新数据.多个线程操作经常出现foreach抛出异常:System.Invalid ...

  6. mac下wordpress环境搭建

    mac下本来就有apache和php,只需要配置以下+安装mysql 1.Apache 预装目录在  /etc/apache2: 默认的网站目录在 /Library/WebServer/Documen ...

  7. “全栈2019”Java第一百一十章:局部内部类与匿名内部类区别详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  8. Python面向对象(构造方法)

    day24 构造方法 特殊作用:在obj=classname()中1.创建对象,2.通过对象执行类中的一个特殊方法. class Bar: def __init__(self): ") de ...

  9. Linux 重命名

    例子:将目录A重命名为B mv A B 例子:将/a目录移动到/b下,并重命名为c mv /a /b/c 其实在文本模式中要重命名文件或目录,只需要使用mv命令就可以了,比如说要将一个名为abc的文件 ...

  10. Flask_WTF升级到最新版本

    用pip $ pip install -U Flask-WTF 看熊哥的后端管理系统时候报错ImportError: cannot import name FlaskForm. 那一行的代码:from ...