来自lknny.com,欢迎交流学习!点击

tags: [leetcode,algorithm]

categories: algorithm

Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target,

where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

大意:*寻找一个序列中的2个数字,使其相加等于targe,并输出这2个数字的index*

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

思路:

数字和下标,首选hashmap;

选择数字作为key值,方面通过get()得到value即index

key值重复情况的影响,仅仅出现在2个key相等并正好加上等于targe

但是,把put()方法放到判断之后,就不会出现这种情况了。因为在存入hashmap之前,index已经知道~

		public int[] twoSum(int[] nums, int target)
{
HashMap<Integer,Integer> result=new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++)
{
if(result.containsKey(target-nums[i]))
{
return new int[]{result.get(target-nums[i])+1,i+1};
}
else
result.put(nums[i], i);
}
return null;
}

HashMap<V,T> 泛型,所以int等基础类型不可!

HashMap允许key值重复,实际上是对相同key值的value覆盖,hashmap.put(1,2);hashmap.put(1,3);那么1.value=3.

get();通过key得到value.

EOF 1;

Add Two Numbers

You are given two linked lists representing two non-negative numbers.

The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

大意:2个链表,逆序存放数字,求和。如下例子,342+465=807.结果也是逆序输出.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

思路:

其实看懂了题目,感觉还是挺人性化的题目=。=,要是正序输出,岂不是还要逆转链表..

题目把咱正常的加法运算,右侧相加,向左进位,变成左侧相加,向右进位

从链表最左侧相加,有进位就进1(相加最多进位1),把当前相加结果%10添加到输出链表,当前相加结果/10进位到下位相加

	public ListNode addTwoNumbers(ListNode l1, ListNode l2)
{
ListNode resultListNode=new ListNode(0);
>>>>pListNode指向resultListNode,做输出用
ListNode pListNode=resultListNode;
int flag=0;
while(l1!=null||l2!=null)
{
if(l1!=null)
{
flag+=l1.val;
l1=l1.next;
}
if (l2!=null)
{
flag+=l2.val;
l2=l2.next;
}
resultListNode.next=new ListNode(flag%10);
>>>>new完节点,然后指向之.....我写的时候忘了=!=..
resultListNode=resultListNode.next;
flag/=10;
}
>>>>while_loop完后,flag有可能还有进位,比如555+555,所以最后还要判断下!显然if(true)flag==1;
if (flag!=0)
{
resultListNode.next=new ListNode(flag);
}
return pListNode.next;
}

EOF 2;

Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc",which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

大意:寻找字符串中无重复字符的最长字串

思路:

最长字串肯定是连续的:也就是找2个相同字符之间的长度;还要注意abba这种情况,主游标,和副游标,必然不能回退!

	public int lengthOfLongestSubstring(String s) {
{
if (0==s.length()) return 0;
HashMap<Character, Integer> hashMap=new HashMap<Character, Integer>();
int max=0;
for(int i=0,j=0;i<s.length();i++)
{
if(hashMap.containsKey(s.charAt(i)))
{
>>防止副游标j回退,我又没想到....=。=<<
j=Math.max(hashMap.get(s.charAt(i))+1,j);
}
hashMap.put(s.charAt(i), i);
max=Math.max(max, i-j+1);
}
return max;
}

EOF 3;

Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321; Example2: x = -123, return -321

思路:

定于为Easy的题目,居然Test Case还包含了越界的情况,我也真是醉了....

		public int reverse(int x)
{
if (x==0) return 0;
long num=0;
while(x!=0)
{
num=num*10+x%10;
if (num>Integer.MAX_VALUE||num<Integer.MIN_VALUE) {
return 0;
}
x/=10;
}
return Integer.parseInt(num+"");
}

注意:

类型 Max Value 备注
int 2147483647 int最大21亿多点..10位数
long 9223372036854775807 long最大19位
double 1.7976931348623157E308 double好大

任何类型出现越界,不是error就是乱输出,这是常识!Integer.parseInt(num+"");屡试不爽!

我用了long类型,来判断越界情况,1234567899逆转妥妥越界,但是本身并不越界。这是本题需要考虑的case!

leetcode某大神,利用越界后int数字混乱(无规律),通过简单的运算,进行了判断!

        int tail = x % 10;
int newResult = result * 10 + tail;
if ((newResult - tail) / 10 != result)
{ return 0; }

EOF 7;

leetcode 1_2_3_7的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. springboot 整合Redis

    0.导入 maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...

  2. 迁移mysql数据到oracle上

    转自:http://www.cnblogs.com/Warmsunshine/p/4651283.html 我是生成的文件里面的master.sql里面的sql,一个一个拷出来的. 迁移mysql数据 ...

  3. Match:Oulipo(POJ 3461)

     Oulipo 题目大意:给你一个字符串,要你找到字符串包含指定子串的个数 只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧 #include < ...

  4. IOS-Gesture(手势识别)

    手势识别——Gesture Recognizer •iOS3.2版本之后,苹果推出了手势识别(Gesture Recognizer),其目的是: –简化开发者的开发难度 –统一用户体验 • •iOS目 ...

  5. js闭包原理

    一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. ****定义在函数中的函数,并且可在外部访问得到.(正常情况下我们是无法 ...

  6. Linux vmstat字段解析

    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...

  7. 解决eclipse ctrl+鼠标左键不能用

    选择[Window]菜单 Preferences ——>General——>Editors——>Text Editors——>Hyperlinking 把勾都点上,然后确定KE ...

  8. iOS小技巧总结,绝对有你想要的

    原文链接 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIV ...

  9. postgresql导入及导出

    导入命令 psql -d GAME -U postgres -f /root/plubic.sql 出现如下错误: psql: FATAL:  Peer authentication failed f ...

  10. php连接sql server

    这两天有个php连接sql server的项目,顺便学习学习sql server  说明: 1:PHP5.2.x本身有个php_mssql.dll的扩展用来连接Sql server,但是这个dll只是 ...