blankj / awesome-java-leetcode Goto Github PK
View Code? Open in Web Editor NEW:crown: LeetCode of algorithms with java solution(updating).
:crown: LeetCode of algorithms with java solution(updating).
很用心,手动点赞
好屌的样子[比心]
你好,在算法004的题目中,既然是两个已经排序好的数组中,要取这两个数组的中位数,为什么不直接去第一个数组的中位数,再去第二个数组的中位数,最后取这两个中位数的平均值,不就是结果了吗?感觉看004的算法有点复杂。我这样的算法是否有什么问题,还望告知,谢谢!
不需要在循环里再判断p1,p2是否大于等0了,循环条件就是p1 >=0 && p2 >=0。
while (p1 >= 0 && p2 >= 0) {
//carry += p1 >= 0 ? a.charAt(p1--) - '0' : 0;
//carry += p2 >= 0 ? b.charAt(p2--) - '0' : 0;
carry += a.charAt(p1--) - '0';
carry += b.charAt(p2--) - '0';
sb.insert(0, (char) (carry % 2 + '0'));
carry >>= 1;
}
注释的这两行代码,真的困扰了我 ~~,我都去复习运算符的顺序了,汗。
题目本意应该是移除数组中的相同的元素,是不是弄错了?
System.out.println(solution.romanToInt("CCCXLVIII"));// 384 ->348
我的jdk是1.7版本,请问是JDK版本的问题吗?
例如Leetcode94题,输入: [1,null,2,3],调用TreeNode.print( )函数,3会丢失
设置了java sdk,但是设置不了main class,所以run不了。谢谢解答
class Solution {
public boolean isPalindrome(int x) {
if (x < 0 || (x != 0 && x % 10 == 0)) return false;
int halfReverseX = 0;
while (x > halfReverseX) {
halfReverseX = halfReverseX * 10 + x % 10;
x /= 10;
}
return halfReverseX == x || halfReverseX / 10 == x;
}
}
什么时候会走halfReverseX == x 这条return 路径呢??
我现在还是一名大学生,我希望自己以后能成为一名优秀的ACMer,谢谢楼主这个项目,会持续关注,感谢!
两个数相加,如果是Integer的情况,用一个方法处理该如何操作呢?
class Solution {
public String multiply(String num1, String num2) {
if (num1.equals("0") || num2.equals("0")) return "0";
int l1 = num1.length(), l2 = num2.length(), l = l1 + l2;
char[] ans = new char[l];
char[] c1 = num1.toCharArray();
char[] c2 = num2.toCharArray();
for (int i = l1 - 1; i >= 0; --i) {
int c = c1[i] - '0';
for (int j = l2 - 1; j >= 0; --j) {
ans[i + j + 1] += c * (c2[j] - '0');
}
}
for (int i = l - 1; i > 0; ++i) { // 这个地方写错了 应该是--
if (ans[i] > 9) {
ans[i - 1] += ans[i] / 10;
ans[i] %= 10;
}
}
StringBuilder sb = new StringBuilder();
int i = 0;
for (; ; ++i) if (ans[i] != 0) break;
for (; i < ans.length; ++i) sb.append((char) (ans[i] + '0'));
return sb.toString();
}
}
private static int lengthOfLongestSubstringMe(String s) {
HashMap<Character, Integer> map = new HashMap<>(128);
int max = 0;
int pre = 0;
char temp;
for (int i = 0; i < s.length(); i++) {
temp = s.charAt(i);
if (map.containsKey(temp)) {
max = Math.max(max, i - pre);
pre = Math.max(map.get(temp) + 1, pre);
}
map.put(s.charAt(i), i);
}
return Math.max(max, s.length() - pre);
}
class Solution
{
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode node = head.next;
head.next = swapPairs(node.next);
node.next = head;
return node;
}
}
为什么 if (head == null || head.next == null) 两个条件交换一下顺序就会报错呢??
我看了代码之后才知道是怎么回事,这样表述如何?
利用 HashMap 作为存储,键为目标值减去当前元素值,索引为值,比如 i = 0 时,此时首先要判断 nums[0] = 2 是否在 map的key 中,如果不存在,那么插入键值对 key = 9 - 2 = 7, value = 0,之后当 i = 1 时,此时判断 nums[1] = 7 已存在于 map的key 中,那么取出此时 value = 0 作为第一个返回值,当前 i 作为第二个返回值,具体代码如下所示。
还有虽然思路二只有一个循环,但是为什么我测试了一个发现思路二反而更慢?
public class P01TwoSum {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = {2, 7, 11, 15}; int target = 18;
//伪代码
long startTime=System.nanoTime(); //获取开始时间
int[] res = twoSum1(nums, target); //测试的代码段
long endTime=System.nanoTime(); //获取结束时间
System.out.println("程序运行时间: "+(endTime-startTime)+"ns");
long startTime1=System.nanoTime(); //获取开始时间
int[] res2 = twoSum2(nums, target); //测试的代码段
long endTime1=System.nanoTime(); //获取结束时间
System.out.println("程序运行时间: "+(endTime1-startTime1)+"ns");
}
public static int[] twoSum1(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j =i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[] {i,j};
}
}
}
return null;
}
public static int[] twoSum2(int[] nums, int target) {
int len = nums.length;
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < len; ++i) {
if (map.containsKey(nums[i])) {
return new int[]{map.get(nums[i]), i};
}
map.put(target - nums[i], i);
}
return null;
}
}
程序运行时间: 2356ns
程序运行时间: 36646ns
32位正整数最大值 2^31-1 = 2147483647 负整数最大值-2^31 = -2147483648,判断是否溢出时要区分符号,正数时tmp>7则溢出,负数时tmp>8 才溢出。同时也避免了一次取余操作。
public int strStr(String haystack, String needle) {
int l1 = haystack.length(), l2 = needle.length();
if (l1 < l2) return -1;
for (int i = 0; ; i++) {
//if(i+l2>l1)return -1;
for (int j = 0; ; j++) {
if (j == l2) return i;
**if (i + j == l1) return -1;**
if (haystack.charAt(i + j) != needle.charAt(j)) break;
}
}
}
使用第5行取代第8行,减少循环次数。
假如needle只有最后一个字符不同,原代码几乎需要把needle遍历完才进入第8行的分支
我们团队都是Java哈,也有cpp、ios和android,大家刷好题了就可以随时来碾压我们了。欢迎大家加入。
招聘, 微信:kerisle,
大家也可以直接发邮件
[email protected]
刚刚开始看leetcode,大家共勉。
第一题思路一
for (int i = 0; i < nums.length; ++i) {
是否应该是 i < nums.length - 1会更好
试了一下[2,4,3]和[5,6,7],得到了[7,0,1,1]
LZ你好,请问一下为什么最后返回的是head.next而不是temp.next?
如果有N个字符串,那就需要N个循环了啊
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.