2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
方法1
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode res = new ListNode(-1);//返回结果的头结点
ListNode p = res;
int sum=0;//对应位相加的和
int digit=0;//进位值
while(l1!=null||l2!=null||digit!=0)
{
int num1 = l1==null?0:l1.val;
int num2 = l2==null?0:l2.val;
sum = l1.val+l2.val+digit;
//因为一个节点只能存一位所以取个位存入结果 十位的数字存入digit作为进位的数字
p.next=new ListNode(sum%10);
p=p.next;
digit = sum/10;
l1= l1==null?null:l1.next;
l2= l2==null?null:l2.next;
}
return res.next;
}
方法2
public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
ListNode res = new ListNode(-1);//返回结果的头结点
ListNode p = res;
int sum=0;//对应位相加的和
while(l1!=null||l2!=null)
{
sum=sum/10;//上次运算的进位值
if(l1!=null)
{
sum+=l1.val;
l1=l1.next;
}
if(l2!=null)
{
sum+=l2.val;
l1=l1.next;
}
p.next = new ListNode(sum%10);
p=p.next;
}
if(sum/10==1)
{
p.next= new ListNode(sum/10);
p=p.next;
}
return res.next;
}
3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
找出最长且不带重复字符的字串长度
思路:创建一个Hash表用来存储字串
public int lengthOfLongestSubstring(String s) {
int left = 0;
int right = 0;
int len=0;
HashSet<Character> set = new HashSet<>();//存储字符用来判断字符有没有重复的
while(right<s.length())
{
if(!set.contains(s.charAt(right)))//没有重复则right右移跟新长度
{
set.add(s.charAt(right));
len = Math.max(len, right-left+1);
right++;
}
else
{
set.remove(s.charAt(left++));//如果有重复则删除left指针的字符left指针右移一下再重新判断直到没有重复字符right指针才移动
}
}
return len;
}
15. 3Sum
Given an array nums
of n integers, are there elements a, b, c in nums
such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> list = new ArrayList<>();
for(int i=0;i<nums.length-2;i++)
{
if(i==0||(i!=0&&nums[i]!=nums[i-1]))
{
int num = nums[i];
int left=i+1;
int right=nums.length-1;
while(left<right)
{
if(-num==(nums[left]+nums[right]))
{
list.add(Arrays.asList(nums[i],nums[left],nums[right]));
while(left<right&&(nums[left]==nums[left+1])) left++;
while(left<right&&(nums[right]==nums[right-1])) right--;
left++;
right--;
}
else if(-num>(nums[left]+nums[right]))
{
left++;
}
else
{
right--;
}
}
}
}
return list;
}
62. Unique Paths
机器人走m行 n列的棋盘 每次只能向下或向右问共有多少种走法
public int uniquePaths(int m, int n) {
int[][] map = new int[m][n];
for(int i=0;i<m;i++)
{
map[i][0]=1;
}
for(int i=0;i<n;i++)
{
map[0][i]=1;
}
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
map[i][j]=map[i][j-1]+map[i-1][j];
}
}
return map[m-1][n-1];
}
162. Find Peak Element
Example 1:
Input: nums = [1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
Example 2:
Input: nums = [
1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where the peak element is 2,
or index number 5 where the peak element is 6.
在数组中找任意一个拐点的索引
public int findPeakElementt(int[] nums) {
return getPeak(nums,0,nums.length);
}
public int getPeak(int[] nums,int left,int right)
{
if(left==right) return left;
if(right-left==1)
{
if(nums[right]>nums[left]) return right;
return left;
}
else
{
int mid = (left+right)/2;
if(nums[left]<nums[mid]&&nums[right]<nums[mid])
{
return nums[mid];
}
else if(nums[left]>nums[mid]&&nums[mid]>nums[right])
{
return getPeak(nums,left,mid-1);
}
else
{
return getPeak(nums,mid+1,right);
}
}
}
209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
Input:s = 7, nums = [2,3,1,2,4,3]
Output: 2 Explanation: the subarray[4,3]
has the minimal length under the problem constraint.
方法一
public int minSubArrayLen(int s, int[] nums) {
int left=0;
int right=0;
int sum=0;
int len=100000;
while(left<=right&&right<nums.length)
{
sum+=nums[right++];
if(sum>=s)
{
len=Math.min(len,right-left);
left++;
right=left;
sum=0;
}
}
return len==100000?0:len;
}
方法2
public int minSubArrayLen(int s, int[] nums) {
if(nums==null|nums.length==0) return 0;
int left=0;
int right=0;
int sum=0;
int len=Integer.MAX_VALUE;
while(right<nums.length)
{
sum+=nums[right++];
while(sum>=s)
{
len = Math.min(len, right-left);
sum-=nums[left++];
}
}
return len==Integer.MAX_VALUE?0:len;
}