日常算法题(二)

给定两个字符串s和t,确定它们是否是同构的。
如果s中的字符可以替换为t,则两个字符串是同构的。
所有出现的字符必须替换为另一个字符,同时保留字符的顺序。没有两个字符可以映射到相同的字符,但字符可以映射到自身。

例1:
输入: s = “egg”, t =“add”
输出: true
例2:
输入: s = “foo”, t =“bar”
输出: false

可以使用HashMap,把字符t变成字符串

public boolean isIsomorphic(String s, String t) {
    Map m = new HashMap();
    for (Integer i=0; i<s.length(); ++i)
        if (m.put(s.charAt(i), i) != m.put(t.charAt(i)+"", i))
            return false;
    return true;
}

如果阵列单调递增或单调递减,则阵列是单调的。
数组A是单调的,如果所有的增加i <= j,A[i] <= A[j]。A如果对所有人来说i <= j,数组是单调减少的A[i] >= A[j]。
返回true当且仅当给定的数组A是单调的。

例1:
输入:[1,2,2,3]
输出:true
例2:
输入:[6,5,4,4]
输出:true

    public boolean isMonotonic(int[] A) {
        boolean in = true, de = true;
        for(int i = 0; i < A.length-1; i++) {
            if(A[i] > A[i+1]) in = false;
            else if(A[i] < A[i+1]) de = false;
        }
        return in || de;
    }

给定两个串 S 并T,返回,如果他们是平等的,当两者都输入到空的文本编辑器。#表示退格字符。

例1:
输入: S = “ab#c”,T = “ad#c”
输出:true
说明:S和T都变为“ac”。
例2:
输入: S = “ab ##”,T = “c#d#”
输出:true
说明:S和T都变为“”。

可以考虑使用堆栈来进行操作

   public boolean backspaceCompare(String S, String T) {
        for (int i = S.length() - 1, j = T.length() - 1;; i--, j--) {
            for (int b = 0; i >= 0 && (b > 0 || S.charAt(i) == '#'); --i) b += S.charAt(i) == '#' ? 1 : -1;
            for (int b = 0; j >= 0 && (b > 0 || T.charAt(j) == '#'); --j) b += T.charAt(j) == '#' ? 1 : -1;
            if (i < 0 || j < 0 || S.charAt(i) != T.charAt(j)) return i == -1 && j == -1;
        }
    }

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

public int maxSubArray(int [] nums) {
		
		if(nums == null||nums.length == 0)
			return 0;
		int current = nums[0],max = nums[0];
		
		for(int i = 1;i < nums.length;i++){
			
			current = Math.max(current + nums[i], nums[i]);
			max = Math.max(current, max);
		}
		return max;

编写一个程序判断给定的数是否为丑数。
丑数就是只包含质因数 2, 3, 5 的正整数。

示例 1:
输入: 64.
输出: true
解释: 6 = 2 × 3
示例 2:
输入: 8
输出: true
解释: 8 = 2 × 2 × 2
示例 3:
输入: 14
输出: false
解释: 14 不是丑数,因为它包含了另外一个质因数 7。
说明:
1 是丑数。输入不会超过 32 位有符号整数的范围: [−231, 231 − 1]。

public boolean isUgly(int num) {
		
		while(num % 2 == 0)
			num = num / 2;
		while(num % 3 == 0)
			num = num / 3;
		while(num % 5 == 0)
			num = num / 5;
		
		return (num == 1) ? false : true;

给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。

public int [] plusOne(int [] digits) {
		
		int len = digits.length;
		for(int i = len - 1;i >= 0;i--) {
			if(digits[i] < 9) {
			
				digits[i]++;
				return digits;
			}
			digits[i] = 0;
		}
		
		int [] nums = new int [len + 1];
		nums[0] = 1;
		return nums;
	}

给定已排序的链接列表,删除所有重复项,使每个元素只出现一次。

例1:
输入: 1-> 1-> 2
输出: 1-> 2
例2:
输入: 1-> 1-> 2-> 3-> 3
输出: 1-> 2-> 3

public ListNode deleteDuplicates(ListNode head) {
		
       if(head == null||head.next == null) return head;
       
       ListNode node = head;
       while(node.next != null) {
    	   
    	   if(node.val == node.next.val) node.next = node.next.next;
    	   
    	   else  node = node.next;
       }
       
       return head;

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素

public int singleNumber(int [] nums){
		
		int len = nums.length;
		int max = nums[0];
		int a = 0;
		for(int i = 1;i < len;i++){
			
			if(max < nums[i])
				max = nums[i];
		}
		
		int [] acg = new int [max];
		for(int i = 0;i < max + 1;i++)
			acg[i] = 0;
		
		for(int j = 0;j < len;j++) {
			
			acg[nums[j]]++;
		}
		
		for(int i = 0;i < max + 1;i++) {
			
			if(acg[i] == 1)
				a = i;
		}
		return a;
	}

猜你喜欢

转载自blog.csdn.net/qq_42014895/article/details/83040743