java and python学习——第一周leetcode刷题
来源:互联网 发布:买家怎么取消农村淘宝 编辑:程序博客网 时间:2024/06/02 06:42
因为是刚刚开始学习java和python语言,还不太熟悉应该怎么用这两种语言编程,所以就选择了leetcode上最简单的五道题来做。
344、Reverse String
这道题没有什么难度,是leetcode中最简单的一道,反转字符串。头和尾两两交换位置即可,开始将string转为char数组,最后再转换为string类型。
java版本:
public class Solution { public String reverseString(String s) { char[] ans=s.toCharArray(); int i=0; int j=s.length()-1; char t; while(i<j) { t=ans[i]; ans[i]=ans[j]; ans[j]=t; i++; j--; } String answer=new String(ans); return answer; }}
python版本:
利用分片,-1代表从反序来读该字符串,很轻松实现字符串的反转。
class Solution(object): def reverseString(self, s): """ :type s: str :rtype: str """ return s[::-1]
292. Nim Game
这道题在编程实现上没有什么难度,只是一个游戏中判断必胜的方法而已。假如n块石子能被4整除,那么先手一定输,否则后手输。
java版本:
public class Solution { public boolean canWinNim(int n) { if(n%4==0) return false; else return true; }}
python版本:
class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: bool """ a=n%4 if(a != 0): return True else : return False
371. Sum of Two Integers
这道题考察的是位元算,算法比较简单,但是我还是想了挺久的。用一个变量来保存加完之后不算进位的结果,另一个变量来保存进位,每次将这两个变量相异或得到新的不进位的结果,相与再向左移一位得到进位。如果进位为零的话,说明得到了结果,返回即可。
java版本:
public class Solution { public int getSum(int a, int b) { while(b!=0) { int i=a^b; int j=(a&b)<<1; a=i; b=j; } return a; }}
python版本:b
用python做的时候,总是通过不了。后来上网查了一下之后,发现还需要加一些操作。这些操作是为了保证最后得到的结果不会溢出,也是为了保证位数的稳定性。
class Solution(object): def getSum(self, a, b): """ :type a: int :type b: int :rtype: int """ while a!=0: i=((a&b)<<1)%0x100000000 j=(a^b)%0x100000000 a=i b=j if b <= 0x7FFFFFFF: return b else: return b | (~0x100000000+1)
136. Single Number
这道题就是考我们知识的灵活应用了,因为这道题如果用位运算来做的话是非常简单的。但是如果用平常的一位一位去比较的话,还需要另外一个庞大的数组,这样的空间开销式很大的。其实两个相同数字,只要做异或操作,最终得到的值一定为0。利用这一点,我们遍历整个数组中的元素,两两异或,得到的最终数字就是single number。
java版本:
public class Solution { public int singleNumber(int[] nums) { int tt=0; for(int i=0;i<nums.length;i++) tt=tt^nums[i]; return tt; }}
python版本:
class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ tt=0 for i in nums: tt=tt^i return tt
258. Add Digits
这道题没有什么难度,用两层循环就可以做完。第一层判断是否已经小于十,第二层计算各位数字的和就可以了。
java版本:
public class Solution { public int addDigits(int num) { int answer=num; while(answer>=10) { int ans=0; while(answer>0) { ans=ans+answer%10; answer=answer/10; } answer=ans; } return answer; } }
python版本:
class Solution(object): def addDigits(self, num): """ :type num: int :rtype: int """ answer=num; while answer>=10: ans=0 while answer>0: ans=ans+answer%10 answer=answer/10 answer=ans return answer
1 0
- java and python学习——第一周leetcode刷题
- java and python学习——第四周leetcode刷题
- java and python学习——第二周leetcode刷题
- java and python学习——第三周leetcode刷题
- leetcode-python 第一周
- 《Text Mining and Analytics》学习笔记——第一周
- Text Mining and Analytics—— 第一周学习笔记
- 学习python第一周
- python学习第一周
- 学习JAVA 第一周
- Java 学习第一周
- Python学习(第一周)
- Python学习第一周-知识点
- Python学习笔记[第一周]
- 程序设计入门—Python 第一周作业
- 第一周JAVA学习总结
- python学习第一周--需求分析
- Python学习第一周-添加库
- Python 函数
- Android 官方侧滑菜单
- 通过sysfs方式控制GPIO
- 类型转换
- 利用Eclipse自带的插件连接GIT的详解
- java and python学习——第一周leetcode刷题
- 解决Idea创建maven-archetype-webapp项目无java目录的问题
- java观察着observer模式---商品价格变动通知客户
- 关于python里面的对齐
- 短信发送器
- hdu 5877 Weak Pair dfs + 线段树(or树状数组)
- LeetCode 24. Swap Nodes in Pairs
- 手游服务端开发基础概念扫盲篇
- java多线程:创建、join方法