逻辑思维编程

题目:

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

//从右上角开始查找target元素
    public boolean find(int [][]array, int target){
        //首先判断数组不为空,否则直接返回false
        if(array!=null && array.length > 0 && array[0].length > 0){
            int row = 0; //初始化行的值
            int col = array[0].length - 1; //初始化列的值
            //循环遍历判断,寻找target
            while(row <= array.length-1 && col >= 0){
                if(target == array[row][col]){
                    return true;
                }else if(target < array[row][col]){
                    col--;
                }else{
                    row++;
                }
            }
        }
        return false;
    }

题目

输入一个链表,反转链表后,输出新链表的表头。
public Node reverse(Node node) {
    Node prev = null;
    Node now = node;
    while (now != null) {
      Node next = now.next;
      now.next = prev;
      prev = now;
      now = next;
    }

    return prev;
  }

 题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。

public int  MoreThanHalfNum_Solution1(int [] array){
        HashMap<Integer,Integer> map=new HashMap<>();
        for(int i=0;i<array.length;i++){
            if(!map.containsKey(array[i])){
                map.put(array[i],1);
            }else{
                int count=map.get(array[i]);
                map.put(array[i],++count);
            }
            int time=map.get(array[i]);
            if(time>array.length>>1)
                return array[i];
        }
        return 0;
}

public int  MoreThanHalfNum_Solution2(int [] array){
        if(array.length<=0)  return 0;
        int result=array[0];
        int count=1;
        for(int i=1;i<array.length;i++){
            if(array[i]==result){
                count++;
            }else{
                count--;
            }
            if(count==0){
                result=array[i];
                count=1;
            }
        }
        int time=0;
        for(int i=0;i<array.length;i++){
            if(array[i]==result){
                time++;
            }
        }
        if(time>array.length/2){
            return result;
        }else{
            return 0;
        }
    }

题目描述

写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。

public class Solution {
    public int Add(int num1,int num2) {
        int c=(num1&num2)<<1;
        int d=num1^num2;
        int result=c^d;
        return result;
    }
}

题目描述

请实现一个函数,将一个字符串中的每个空格替换成“%20”。
 public String replaceSpace(StringBuffer str) {
          String str1=str.toString();  
          String str2=str1.replace(" ","%20");  
          return str2;  

    }
public String replaceSpace(StringBuffer str) {
        int spacenum = 0;
        for(int i=0;i<str.length();i++)
        {
        if(str.charAt(i)==' ')
            spacenum++;
        }
         int indexold = str.length()-1;
         int newlength = str.length()+spacenum*2;
         int indexnew = newlength-1;
         str.setLength(newlength);
         for(;indexold>=0 && indexold<newlength;indexold--)
         {
           if(str.charAt(indexold) == ' ')
           {
            str.setCharAt(indexnew--, '0');
            str.setCharAt(indexnew--, '2');
            str.setCharAt(indexnew--, '%');
           }
           else
                {
                str.setCharAt(indexnew--, str.charAt(indexold));
                }
          }
           return str.toString();
    }

猜你喜欢

转载自www.cnblogs.com/chenxibobo/p/9573991.html