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. gvim 全屏 插件

    1.这里下载插件zip解压后,将fullscreen.dll放到gvim.exe同目录下 2.执行 :call libcallnr()//切换全屏模式 3.上面的命令非常麻烦,可以在_vimrc中ma ...

  2. Open XML操作Excel导入数据

    项目中发现使用OleDb(using System.Data.OleDb)相关对象处理Excel导入功能,不是很稳定经常出问题,需要把这个问题解决掉.项目组提出使用OpenXML来处理Excel的导入 ...

  3. ASP.NET Core IdentityServer4 新手上路

    OAuth2.0资料 今天看到一篇博主写了该系列文章,贴图和过程都比较详细,俗话说实践是检验真理的唯一标准(如果是按照参考文章复制粘贴,应该不会出现踩坑,但是我喜欢自己手动敲一遍),发现几个坑,因而总 ...

  4. autofac JSON文件配置

    autofac是比较简单易用的IOC容器.下面我们展示如何通过json配置文件,来进行控制反转. 需要用到以下程序集.可以通过nugget分别安装 Microsoft.Extensions.Confi ...

  5. Java50道经典习题-程序5 判断分数等级

    题目:利用三元运算符来完成此题:从键盘录入一个整型的分数,没有负分满分为100分,学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示.分析:三元运算符的格式为:逻 ...

  6. django系列6--Ajax03 ajax参数

    ajax的参数 data: 当前ajax请求要携带的数据,是一个json的object对象,ajax方法会默认的把它编码成某种格式 (urlencoded:?a=1&b=2)发送给服务端;此外 ...

  7. 【Oracle 12c】最新CUUG OCP-071考试题库(55题)

    55.(13-3) choose the best answer: Which statement is true regarding the SESSION_PRIVS dictionary vie ...

  8. Getting Started with Elastic Search in .NET

    I have been working on many application during my career.  Many if not all had some searching capabi ...

  9. mxonline实战12, 课程评论,相关课程推荐,课程视频页

    对应github地址:第12天   一. 课程评论   1. 创建URL, VIEW courses/views.py -> Course

  10. webpack+vue中安装使用vue-layer弹窗插件

    1.安装vue-layer插件 npm install vue-layer --save-dev 2.打包入口文件main.js中引入vue.vue-layer.并且将vue-layer添加到vue原 ...