编程题训练1

本文精选了几个经典的编程题目,包括删除链表中指定值的节点、查找斐波纳契数列中的特定项、数字累加直到得到一位数、计算两个数组的交集以及移动数组中的零元素等,通过这些实例介绍了基本的数据结构操作和算法思想。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编程题训练1

删除链表中等于给定值val的所有节点。

样例

给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    /**
     * @param head a ListNode
     * @param val an integer
     * @return a ListNode
     */
    public ListNode removeElements(ListNode head, int val) {
        // Write your code here

        //处理头节点就等于 Val 的情况
        while ( head != null && head.val == val){   
            head = head.next;   //将头节点后移
        }

        //处理只有 null 节点的情况
        if(head==null) return head;  

        ListNode pre = head,nextNode = head.next;

        while( nextNode != null ){

            if( nextNode.val == val ){
                 pre.next = nextNode.next;   
                 nextNode = nextNode.next;

            }else{
                  pre = pre.next;   
                 nextNode = nextNode.next;
            }

        }

        return head;

    }
}

查找斐波纳契数列中第 N 个数。

  • 所谓的斐波纳契数列是指:前2个数是 0 和 1 。第 i 个数是第 i-1 个数和第i-2 个数的和。
class Solution {
    /**
     * @param n: an integer
     * @return an integer f(n)
     */
    public int fibonacci(int n) {
        // write your code here
        if(n==1)
            return 0;

        if(n==2)
            return 1;

        int f0 = 0;
        int f1 = 1;
        int i = 3;
        int f = 0;

        while(i<=n){
            f = f0 + f1;
            f0 = f1;
            f1 = f;
            i++;
        }
        return f;

    }
}

Add Digits 加数字

  • Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

样例

Given num = 38.
The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return 2.

public class Solution {
    /**
     * @param num a non-negative integer
     * @return one digit
     */
    public int addDigits(int num) {
        // Write your code here

       while (num / 10 > 0) {
            int sum = 0;

            while (num > 0) {
                sum += num % 10;
                num /= 10;
            }

            num = sum;
        }

        return num;
    }
}

//观察法
 public int addDigits(int num) {
        // Write your code here
      return (num - 1) % 9 + 1;
    }

计算两个数组的交1 (去重)

  • 样例
    nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].
public class Solution {
    /**
     * @param nums1 an integer array
     * @param nums2 an integer array
     * @return an integer array
     */
    public int[] intersection(int[] nums1, int[] nums2) {
         HashSet<Integer> set =new HashSet<Integer>();
            for (int i : nums2) {
                set.add(i);
            }
            HashSet<Integer> re = new HashSet<Integer>();
            for (int i : nums1) {   
                if(set.contains(i))
                {
                    re.add(i);
                }
            }
            Iterator<Integer> iterator=re.iterator();
            int[] reInt = new int[re.size()];
            int i =0;
            while(iterator.hasNext()){
                reInt[i++] = iterator.next();
            }
            return reInt;


    }
    //用 hashSet
    HashSet<Integer> set =new HashSet<Integer>();
            for (int i : nums2) {
                set.add(i);
            }
            HashSet<Integer> re = new HashSet<Integer>();
            for (int i : nums1) {   
                if(set.contains(i))
                {
                    re.add(i);
                }
            }
            Iterator<Integer> iterator=re.iterator();
            int[] reInt = new int[re.size()];
            int i =0;
            while(iterator.hasNext()){
                reInt[i++] = iterator.next();
            }
            return reInt;

}

计算两个数组的交2

  • 样例
    nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].
public class Solution {
    /**
     * @param nums1 an integer array
     * @param nums2 an integer array
     * @return an integer array
     */
    public int[] intersection(int[] nums1, int[] nums2) {
        // Write your code here
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        ArrayList<Integer> A = new ArrayList<Integer>();
        int i=0;
        int j=0;
        while(i<nums1.length && j<nums2.length ){
            if(nums1[i] == nums2[j]){
                A.add(nums1[i]);
                i++;
                j++;

            }else if(nums1[i] < nums2[j]){
                i++;
            }else{
                j++;
            }

        }
        int[] res = new int[A.size()];

        for( i=0;i<A.size();i++){
            res[i] = (int)A.get(i);
        }
        return res;

    }
}

移动零

  • 给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序
  • 注意事项: 1.必须在原数组上操作 2.最小化操作数
  • 思路:实际上就是将所有的非0数向前尽可能的压缩,最后把没压缩的那部分全置0就行了。比如103040,先压缩成134,剩余的3为全置为0。过程中需要一个指针记录压缩到的位置。
public class Solution {
    /**
     * @param nums an integer array
     * @return nothing, do this in-place
     */
    public void moveZeroes(int[] nums) {
             int cnt = 0, pos = 0;

        // 将非0数字都尽可能向前排
        for(int i = 0; i < nums.length; i++){

            if(nums[i] != 0){
                nums[pos]= nums[i];
                pos++;
            }
        }
        // 将剩余的都置0
        for(;pos<nums.length; pos++){
            nums[pos] = 0;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值