16.11.10

来源:互联网 发布:c语言有一个函数 编辑:程序博客网 时间:2024/05/20 21:44

283. Move Zeroes

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
分析:将整型数组中的0移动到数组的末端

public class Solution {    public void moveZeroes(int[] nums) {        //用于统计0的个数以便判断将非0的数值向前移动的位数        int totalZeros=0;        for(int i=0;i<nums.length;i++){            if(nums[i]==0){                ++totalZeros;            }else{                //如果这个数不是0,将当前数向前移动一定的距离(距离等于0的个数)                nums[i-totalZeros]=nums[i];            }        }        //将后几位数都赋值为0        for(int i=nums.length-totalZeros;i<nums.length;i++){            nums[i]=0;        }    }}

290. Word Pattern

Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
pattern = “abba”, str = “dog cat cat dog” should return true.
pattern = “abba”, str = “dog cat cat fish” should return false.
pattern = “aaaa”, str = “dog cat cat dog” should return false.
pattern = “abba”, str = “dog dog dog dog” should return false.

Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
分析:根据给定的模式,判断字符串中的字符是否与给定的模式匹配

public class Solution {    public boolean wordPattern(String pattern, String str) {        Map<Character,String> map = new HashMap<Character,String>();        char[] charArray = pattern.toCharArray();        int flag=0;        //将字符串以空格划分        String[] splits = str.split("\\s");        int splitsLength=splits.length;        //获取模式的长度        int charLength=charArray.length;        //判断长度是否相等        if(splitsLength!=charLength)return false;        String temp;        for(int i=0;i<charLength;i++){            //将str中的单词和patternStr中对应的字符以键值对的形式保存            //如果map中的key值(str对应的单词)存在,则判断value是否与对应的patternStr中的字符是否相等            if(map.containsKey(charArray[i])){                //如果不相等,则匹配失败,跳出,返回false                if(!map.get(charArray[i]).equals(splits[i])){                    flag=1;                    break;                }            //如果在map的value中存在patternStr中的字符,则必定匹配失败            }else if(map.containsValue(splits[i])){                flag=1;                break;            }else{                //将str中的单词和模式中的字符以键值对的形式存储                map.put(charArray[i], splits[i]);            }        }        if(flag==1){            return false;        }else{            return true;        }    }}

303. Range Sum Query - Immutable

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Example:
Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
分析:根据给定的索引范围求和(动态规划)

public class NumArray {    //dp    int sum[];    public NumArray(int[] nums) {        //sum中存放nums数组中前i位数之和        sum=nums;        for(int i=1;i<nums.length;i++){            sum[i]+=sum[i-1];            System.out.println(sum[i]);        }    }    public int sumRange(int i, int j) {        if(i==0){            return sum[j];        }        return sum[j]-sum[i-1];    }}// Your NumArray object will be instantiated and called as such:// NumArray numArray = new NumArray(nums);// numArray.sumRange(0, 1);// numArray.sumRange(1, 2);
0 0
原创粉丝点击