剑指Offer之Java算法习题精讲求和篇

题目一 

 解法

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode node = new ListNode(-1);
        ListNode ans = node;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1+n2+carry;
            carry = 0;
            node.next = new ListNode(sum%10);
            node = node.next;
            if(sum/10>=1){
                carry = 1;
            }
            if(l1!=null){
                l1 = l1.next;
            }
            if(l2!=null){
                l2 = l2.next;
            }
        }
        if(carry>=1){
            node.next = new ListNode(1);
        }
        return ans.next;
    }
}

第二题

 解法

class Solution {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> occ = new HashSet<Character>();
        int rk = -1, ans = 0;
        for(int i = 0;i<s.length();i++){
            if (i != 0) {
                occ.remove(s.charAt(i - 1));
            }
            while(rk+1<s.length()&&!occ.contains(s.charAt(rk + 1))){
                occ.add(s.charAt(rk + 1));
                ++rk;
            }
            ans = Math.max(ans, rk - i + 1);
        }
        return ans;
    }
}

第三题

 解法

class Solution {
    public int sumOfUnique(int[] nums) {
        int sum = 0;
        int[] arr = new int[101];
        for(int i = 0;i<nums.length;i++){
            arr[nums[i]]+=1;
        }
        for(int i = 0;i<arr.length;i++){
            if(arr[i]==1){
                sum+=i;
            }
        }
        return sum;
    }
}

第四题

 解法

class Solution {
    public int maxAscendingSum(int[] nums) {
        if(nums.length==1) return nums[0];
        int sum = nums[0];
        int max = Integer.MIN_VALUE;
        for(int i =1;i<nums.length;i++){
            if(nums[i]>nums[i-1]){
                sum +=nums[i];
                max = Math.max(max,sum);
            }else{
                max = Math.max(max,sum);
                sum = nums[i];
            }
        }
        return max;
    }
}

到此这篇关于剑指Offer之Java算法习题精讲求和篇的文章就介绍到这了,更多相关Java 求和内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 剑指Offer之Java算法习题精讲链表专题篇

    题目一  解法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solut

  • 剑指Offer之Java算法习题精讲二叉树与斐波那契函数

    题目一 解法 class Solution { public int fib(int n) { int[] arr = new int[31]; arr[0] = 0; arr[1] = 1; for(int i = 2;i<=n;i++){ arr[i] = arr[i-2]+arr[i-1]; } return arr[n]; } } 题目二  解法 /** * Definition for a binary tree node. * public class TreeNode { * in

  • 剑指Offer之Java算法习题精讲二叉树与N叉树

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; *

  • 剑指Offer之Java算法习题精讲排列与N叉树

    题目一  解法 class Solution { LinkedList<List<Integer>> ans = new LinkedList<List<Integer>>(); public List<List<Integer>> permute(int[] nums) { LinkedList<Integer> list = new LinkedList<Integer>(); boolean[] bo =

  • 剑指Offer之Java算法习题精讲二叉树专项解析

    题目一 解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * t

  • 剑指Offer之Java算法习题精讲二叉树与链表

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isBalanced(TreeNode root) { if(root==null){ return true; }e

  • 剑指Offer之Java算法习题精讲二叉树专题篇上

    来和二叉树玩耍吧~ 题目一 解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val

  • 剑指Offer之Java算法习题精讲二叉树专题篇下

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; *

  • 剑指Offer之Java算法习题精讲求和篇

    题目一   解法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solu

  • 剑指Offer之Java算法习题精讲数组与字符和等差数列

    题目一  解法 class Solution { public int[] relativeSortArray(int[] arr1, int[] arr2) { int[] arr = new int[1001]; int[] ans = new int[arr1.length]; int index = 0; for(int i =0;i<arr1.length;i++){ arr[arr1[i]]+=1; } for(int i = 0;i<arr2.length;i++){ while

随机推荐