刷题—Palindrome Number

本文提供了一种不使用额外空间判断整数是否为回文的方法。通过将整数转换为字符串进行逐位比较(Java版),以及通过反转整数进行对比(Python版),实现了两种高效算法。

Determine whether an integer is a palindrome. Do this without extra space.

java:

通过int转换为string,看string是否首位与相应的位相等,判断是不是palindrome

public class Solution {
    public boolean isPalindrome(int x) {
        if (x == 0) return true;
        if (x < 0) return false;
        String s = String.valueOf(x);
        int i = 0;
        int j = s.length();
        while(i<j){
            if (!s.substring(i,i+1).equals(s.substring(j-1,j))){
                return false;
            }
            i++;
            j--;
        }
        return true;
        
        
    }
}

python:

思路:把x每次都除以10,余数乘以十叠加,看是否相等。

class Solution:
    # @return a boolean
    def isPalindrome(self, x):
        if x<0:
            return False
        ret = 0
        abc = x
        while x>0:
            ret = ret*10 +x%10
            x = x/10
        if ret == abc:
            return True
        else:
            return False

### 关于 LeetCode 规划与学习路径 #### 设定目标的重要性 设定清晰的目标对于制定有效的计划至关重要。例如,如果是为了准备技术面试,则应重点关注高频算法目以及数据结构基础[^1]。 #### 数据结构与算法的基础复习 在深入之前,建议先巩固基本的数据结构(数组、链表、栈、队列、哈希表、树、图等)和常见算法(排序、查找、动态规划、回溯法等)。这一步骤能够帮助提高解效率并加深理解[^2]。 #### 目分类练习 按照难度级别逐步推进是一个有效的方法。可以将目分为简单 (Easy),中等 (Medium),困难 (Hard) 三个层次,并按顺序完成。此外还可以依据主来划分,比如字符串处理、数组操作、二叉树遍历等等[^3]。 ```python def practice_plan(difficulty, topic=None): """ Generate a personalized leetcode practice plan based on difficulty and optional specific topics. Args: difficulty (str): The level of problem hardness ('easy', 'medium', or 'hard'). topic (str, optional): Specific area to focus such as 'string', 'array'. Defaults to None. Returns: list: A sequence of problems tailored according to given parameters. """ easy_problems = ["Two Sum", "Palindrome Number"] medium_problems = ["Merge Two Sorted Lists", "Valid Parentheses"] hard_problems = ["Word Break II", "Regular Expression Matching"] if not topic: return { "easy": easy_problems, "medium": medium_problems, "hard": hard_problems }.get(difficulty.lower(), []) else: # Example filtering by both difficulty & topic; actual implementation would require more details about each question's metadata. filtered_list = [] all_questions = {**dict.fromkeys(easy_problems), **dict.fromkeys(medium_problems), **dict.fromkeys(hard_problems)} for q in all_questions.keys(): tags = get_tags(q) # Hypothetical function that retrieves associated tags/topics with the question name if topic in tags and ((difficulty == 'easy' and q in easy_problems) \ or (difficulty == 'medium' and q in medium_problems)\ or (difficulty == 'hard' and q in hard_problems)): filtered_list.append(q) return filtered_list # Usage example print(practice_plan('medium')) ``` 上述代码片段展示了一个简单的函数 `practice_plan` ,用于生成基于难度和个人兴趣领域定制化的练习列表[^4]。 #### 时间管理技巧 合理安排每日/每周的时间投入量也很重要。一般推荐每天保持至少半小时到一个小时左右的持续训练时间;周末则可适当增加强度来进行专项突破或者模拟测试环境下的实战演练[^5]。 #### 反馈循环机制建立 定期回顾自己的错误记录本是非常有益的做法之一。通过分析错因找到薄弱环节进而针对性加强补足短板部分。同时也可以加入一些在线社区讨论组分享经验心得互相促进成长[^6]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值