数组
旋转数组(向右移k位, 头条)
思路: 1 2 3 4 5 6 7 如果k = 3 的话, 会变成 5 6 7 1 2 3 4 1 2 3 4 5 6 7 middle = 7 - 3 = 4,分为左边 4个数字,右边 3个数字 4 3 2 1 7 6 5 分别把左右reverse 一下 5 6 7 1 2 3 4 把总数组reverse 一下就会得到答案 class Solution { public void rotate(int[] nums, int k) { int r = k %nums.length; int m = nums.length - r; change(nums,0,m-1); change(nums,m,nums.length-1); change(nums,0,nums.length-1); } void change(int[] nums, int l , int r){ while(l<r){ int temp = nums[l]; nums[l]=nums[r]; nums[r]=temp; l++; r--; } } }}
最长连续递增序列
class Solution { public int findLengthOfLCIS(int[] nums) { int max=0; for(int i=0;i<nums.length;i++){ int len=1; if(i<nums.length-1){ int j=i+1; int k =i; while(j<nums.length&&nums[k]<nums[j]){ len++; j++; k++; } } max = Math.max(max, len); } return max; } }
字符串
字符串中的第一个唯一字
class Solution { public int firstUniqChar(String s) { //解法1 // int[] c=new int[30]; // for(int i =0;i<s.length();i++){ // c[s.charAt(i)-'a']++; // } // int index=-1; // for(int i=0;i<s.length();i++){ // if(c[s.charAt(i)-'a']==1){ // index=i; // break; // } // } // return index; //解法2 Map<Character,Integer> map=new LinkedHashMap<>(); for(int i=0 ;i<s.length();i++){ map.put(s.charAt(i),map.containsKey(s.charAt(i))?map.get(s.charAt(i))+1:1); } int index=-1; for(Map.Entry<Character,Integer> e: map.entrySet()){ if(e.getValue()==1){ index = s.indexOf(e.getKey()); break; } } return index; } }
无重复最长子串
class Solution { public int lengthOfLongestSubstring(String s) { HashMap<Character,Integer> map=new HashMap<>(); int len=0,start=0,max=0; for(int i=0;i<s.length();i++){ if(map.containsKey(s.charAt(i))&&map.get(s.charAt(i))>=start){ start=map.get(s.charAt(i))+1; } len = i-start+1; map.put(s.charAt(i),i); max = Math.max(len,max); } return max; } }
大数相乘(腾讯二面)
class Solution { public String multiply(String num1, String num2) { if(num1.equals("0")||num2.equals("0")){ return "0"; } char[] nums1=num1.toCharArray(); char[] nums2=num2.toCharArray(); int[] result = new int[nums1.length+nums2.length]; for(int i=0;i<nums1.length;i++){ for(int j=0;j<nums2.length;j++){ result[i+j] += (nums1[i]-'0')*(nums2[j]-'0'); } } for(int i=result.length-1;i>0;i--){ result[i-1] += result[i]/10; result[i] = result[i]% 10; } StringBuffer sb=new StringBuffer(); for(int i=0;i<result.length-1;i++){ sb.append(result[i]); } return sb.toString(); } }}
树
层次遍历(头条一面)
public static void levelTravel(Node root){ if(root==)return; Queue<Node> q=new LinkedList<Node>(); q.add(root); while(!q.isEmpty()){ Node temp = q.poll(); System.out.println(temp.value); if(temp.left!=)q.add(temp.left); if(temp.right!=)q.add(temp.right); } } /* * 层序遍历 * 非递归 */ public void levelOrder1(BinaryNode<AnyType> Node) { if (Node == null) { return; } BinaryNode<AnyType> binaryNode; Queue<BinaryNode> queue = new LinkedList<>(); queue.add(Node); while (queue.size() != 0) { binaryNode = queue.poll(); System.out.print(binaryNode.element + " "); if (binaryNode.left != null) { queue.offer(binaryNode.left); } if (binaryNode.right != null) { queue.offer(binaryNode.right); } } }
二叉树的遍历 (非递归)
/** * 前序遍历 * 非递归 */ public void preOrder1(BinaryNode<AnyType> Node) { Stack<BinaryNode> stack = new Stack<>(); while(Node != null || !stack.empty()) { while(Node != null) { System.out.print(Node.element + " "); stack.push(Node); Node = Node.left; } if(!stack.empty()) { Node = stack.pop(); Node = Node.right; } } } /** * 中序遍历 * 非递归 */ public void midOrder1(BinaryNode<AnyType> Node) { Stack<BinaryNode> stack = new Stack<>(); while(Node != null || !stack.empty()) { while (Node != null) { stack.push(Node); Node = Node.left; } if(!stack.empty()) { Node = stack.pop(); System.out.print(Node.element + " "); Node = Node.right; } } } /** * 后序遍历 * 非递归 */ public void thePostOrderTraversal_Stack(Node root) { //后序遍历 Stack<Node> stack = new Stack<Node>(); Stack<Node> output = new Stack<Node>();//构造一个中间栈来存储逆后序遍历的结果 Node node = root; while (node != null || stack.size() > 0) { if (node != null) { output.push(node); stack.push(node); node = node.getRightNode(); } else { node = stack.pop(); node = node.getLeftNode(); } } System.out.println(output.size()); while (output.size() > 0) { printNode(output.pop()); //最后的输出 } } /* * 层序遍历 * 非递归 */ public void levelOrder1(BinaryNode<AnyType> Node) { if (Node == null) { return; } BinaryNode<AnyType> binaryNode; Queue<BinaryNode> queue = new LinkedList<>(); queue.add(Node); while (queue.size() != 0) { binaryNode = queue.poll(); System.out.print(binaryNode.element + " "); if (binaryNode.left != null) { queue.offer(binaryNode.left); } if (binaryNode.right != null) { queue.offer(binaryNode.right); } } }