leetcode简单题目两道(2)
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)的更多相关文章
- leetcode简单题目两道(4)
心情还是有问题,保持每日更新,只能如此了. Problem Given a binary tree, return the level order traversal of its nodes' va ...
- leetcode简单题目两道(3)
本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了. Problem Given an array nums, write a function to move all 's to ...
- leetcode简单题目两道(5)
Problem Given an integer (signed bits), write a function to check whether it . Example: Given num = ...
- leetcode简单题目两道(1)
Problem: You are playing the following Nim Game with your friend: There is a heap of stones on the t ...
- CTF中关于XXE(XML外部实体注入)题目两道
题目:UNCTF-Do you like xml? 链接:http://112.74.37.15:8008/ hint:weak password (弱密码) 1.观察后下载图片拖进WINHEX发现提 ...
- 两道面试题,带你解析Java类加载机制
文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...
- 【转】两道面试题,带你解析Java类加载机制(类初始化方法 和 对象初始化方法)
本文转自 https://www.cnblogs.com/chanshuyi/p/the_java_class_load_mechamism.html 关键语句 我们只知道有一个构造方法,但实际上Ja ...
- 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester
这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...
- 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制
你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...
随机推荐
- 创建jdk8基础镜像
https://blog.csdn.net/qq_35981283/article/details/80738451
- 用.msi安装node时安装失败,出现rolling back action(转载)
转载地址:https://blog.csdn.net/qq_33295622/article/details/52956369在重装node时出现了上图所示情况,解决方法如下: 1.在官网下载稳定版本 ...
- 记录一下显示Map<String, ArrayList<String>>中的ArrayList里的数据的操作
1.有以下数据: ArrayList<Employee> emp = new ArrayList<>(); emp.add(new Employee("zhang&q ...
- SoundPool跑套图片
- shell、cmd、dos和脚本语言区别和联系
问题一:DOS与windows中cmd区别 在windows系统中,“开始-运行-cmd”可以打开“cmd.exe”,进行命令行操作. 操作系统可以分成核心(kernel)和Shell(外壳)两部 ...
- 六、linux目录结构知识
1.显示行号: cat -n 2.set nu 3.tail -f a.txt 查看文件的尾部变化 4.w 当前的登陆用户 5.yum包管理工具底层调用的还是 rpm -ivh 包名 ...
- [ActionScript 3.0] 通过as3操作web内容
package com.controls.utils { import flash.net.*; import flash.utils.ByteArray; import flash.external ...
- 如何在CentOS 7上使用vsftpd(FTP)的配置文件介绍
vsftpd.conf - vsftpd的配置文件. 描述 vsftpd.conf可用于控制vsftpd行为的各个方面. 默认情况下,vsftpd在/etc/vsftpd.conf位置查找此文件. 但 ...
- Welcome! This is the documentation for Python 3.6.8
The Zen of Python, by Tim Peters Beautiful is better than ugly.Explicit is better than implicit.Simp ...
- Windows运行常用命令(win+R)
Windows运行常用命令(win+R) 1.calc: 启动计算器 2.notepad: 打开记事本 3.write: 写字板 4.mspaint: 画图板 5.snippingtool:截图工具, ...