zoukankan      html  css  js  c++  java
  • 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(1) space complexity and O(nodes) time complexity.
    
    Example:
    
    Given 1->2->3->4->5->NULL, return 1->3->5->2->4->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 = 1;
            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++了,都退化了呀。

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

  • 相关阅读:
    CA认证
    浅谈个人融资秘诀
    外挂编写完全攻略
    字符串之单词原地逆转
    推荐系统(1)--splitting approaches for context-aware recommendation
    BZOJ 1122 POI2008 账本BBB 单调队列
    一起talk C栗子吧(第一百二十一回:C语言实例--线程知识体系图)
    几种内存数据的总结
    Android学习路线(十九)支持不同设备——支持不同(Android)平台版本号
    毕业课题之------------图像的形态学滤波
  • 原文地址:https://www.cnblogs.com/hawk-whu/p/6736577.html
Copyright © 2011-2022 走看看