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. c++中的隐藏及重载、重写与隐藏的区别

    c/c++中的隐藏  举个栗子 class A { public : void fun1(int a, int b) { cout<<"abcd"<<end ...

  2. 大坑记录 - shell脚本删除操作

    背景 jenkins执行去执行shell命令,其中引用了一些jenkins的变量,如${WORKSPACE}这种,因为每次执行jenkins比较慢,于是想复制脚本出来想本地调试一下,直接复制了脚本过来 ...

  3. jQuery限制文本框的输入长度

    jQuery限制文本框输入,包含粘贴. //限制文本框的输入长度 $(function () { $(document).on("keypress", ".txt-val ...

  4. 精妙SQL语句大全

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...

  5. WP8.1StoreApp(WP8.1RT)---添加推送功能和获取系统信息

    添加推送通知 1:Package.appxmanifest中的声明添加后台任务的推送通知权限 2:var channel = await PushNotificationChannelManager. ...

  6. sqlite批量处理数据性能优化

    最近设计到sqlite数据库批量操作的,性能很是问题.于是一番研究(站在巨人肩膀)从网上整理出来相关性能优化方向.大体分三个级别,一般第一个阶段已足够. 1.sqlite每次插入数据(每调用一次sql ...

  7. Android ScrollView 子控件不占满的问题

    经常碰到很笨的 ScrollView的子控件无法占满 ScrollView 的空间的问题. 其实只需要加一行,android:fillViewport="true" 但不加上这行就 ...

  8. Android TextView 嵌套图片及其点击,TextView 部分文字点击,文字多颜色

    1. TextView 中嵌套图片的方法 TextView textView... textView.setText("..."); textView.append(Html.fr ...

  9. Oracle数据库02

    EXISTS子查询 特征:将主查询中的数据带到子查询中进行验证,如果验证成功则子查询返回true,当主查询接收到true的时候被验证的数据就显示,如果在子查询中验证失败则返回false,当主查询接收到 ...

  10. php中mvc框架总结1(7)

    1.代码结构的划分: 目前的目录结构: /站点根目录 /application/应用程序目录 Model/模型目录 View/视图目录 Back/后台 front/ test/测试平台 Control ...