leetcode题解日练--2016.6.30

六月最后一天,22岁最大一天。明天是新的开始,继续努力。
编程日记,尽量保证每天至少3道leetcode题,仅此记录学习的一些题目答案与思路,尽量用多种思路来分析解决问题,不足之处还望指出。标红题为之后还需要再看的题目。

今日题目:1、ZigZag变换;2、不用加减号的加法;3、判断合法回文字符串(带噪声)。

6. ZigZag Conversion | Difficulty: Easy

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P A H N
A P L S I I G
Y I R
And then read line by line: “PAHNAPLSIIGYIR”
Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.
题意:zigzag变换
思路:
1、首先列举一些数,从中找规律
参考https://leetcode.com/discuss/55208/if-you-are-confused-with-zigzag-pattern-come-and-see

代码:
C++

class Solution {
public:
    string convert(string s, int numRows) {
        string res = "";
        int len = s.length();
        if(numRows==1)  return s;
        for(int i = 0;i<numRows;i++)
        {
            int step1 = (numRows-i-1)*2;
            int step2 = (i)*2;
            int pos = i;
            if(pos<len)
            res+=s[pos];
            while(1)
            {
                if(pos>=len) break;
                pos+=step1;
                if(pos>=len)   break;
                if(step1)
                    res+=s[pos];
                pos+=step2;
                if(pos>=len) break;
                if(step2)
                res+=s[pos];
            }
        }
        return res;
    }
};

结果:16ms

371. Sum of Two Integers | Difficulty: Easy

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example:
Given a = 1 and b = 2, return 3.

题意:求两个整数之和,但是不能用+和-
思路:
开始考虑用循环,但是复杂度太高,而且大数会超时。后来查了下资料可以用位运算来代替加号。给定a、b的时候,如何来做加法呢?首先可以用异或来做不带进位的加法,得到sum。然后用与来找出进位,再左移一位代表这些进位的值b。如果进位的值b是0,那么不带进位的值就是结果,循环结束;如果进位值不等于0,那么是不是还需要将sum和b加起来呢?这个时候就是最初我们的问题,只不过是进行sum与b的相加。将sum赋给b继续进行运算。
代码:
C++

class Solution {
public:
    int getSum(int a, int b) {
        int sum = a;

        while (b != 0)
        {
            sum = a ^ b;//首先做异或操作,为1的位就是真实为1的位
            b = (a & b) << 1;//再做与操作,为1的位就是应该要进位的位,这个时候就将这些位左移一位,相当于进位
            a = sum;
        }

        return sum;
    }
};

结果:0ms

125. Valid Palindrome | Difficulty: Easy

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
“A man, a plan, a canal: Panama” is a palindrome.
“race a car” is not a palindrome.
题意:判断字符串是否回文,忽略大小写和标点
思路:
1、快两点半了困死了,先用python简单写了一个,效率不高,仅仅只是AC了,白天填坑。
python

class Solution(object):
    def isPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        s = s.lower()
        char = "abcdefghijklmnopqrstuvwxyz0123456789"
        s=filter(lambda x:x in char, s)
        if(len(s)==0 or len(s)==1):
            return True


        for i in range(len(s)+1/2):
            if(s[i]!=s[len(s)-1-i]):
                return False
        return True

结果:138ms
2、因为我们需要判断字母和数字是否为回文,不需要把那些噪声去掉,遇到就跳过逐个判断就行了。那么可以用到c++的isalnum函数判断是否字母或者数字。整个过程还是很简单的。
C++

class Solution {
public:
    bool isPalindrome(string s) {
        if(s.size()==0 || s.size()==1)      return true;
        for(int i=0,j=s.size()-1;i<j;)
        {
            if(!isalnum(s[i])) {i++;continue;}
            if(!isalnum(s[j]))  {j--;continue;}
            if(tolower(s[i])!=tolower(s[j]))    return false;
            i++;
            j--;
        }
        return true;
    }
};

结果:16ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值